J2EE information system integration solution

Source: Internet
Author: User
In current Java development, web occupies a lot of space. At least 6 of the 10 Java programmers are engaged in web development, but whether it is c/s or B/S, there is only one final purpose of the software, that is, the integration of various services. with the development of software technology, the integration of EIS has seen two major trends: Sun's J2EE (javaee) solution and Ms.. Net solution. All they need to do is to integrate different services and then expose the unified interface to the client. For example, in J2EE, the integration of services in the distributed environment-RMI, etc, traditional CORBA, and. net remoting. Of course, we only discuss Java integration solutions here.
In J2EE integration, integration is generally divided into two parts: integration between Java systems, and integration between Java and information systems developed in other languages.
First, integration between Java systems: in this case, integration is relatively simple, as long as both parties provide service interfaces. For example, the service provider provides RMI remote interfaces or directly exposes interfaces to the client in the form of ejbs, in this case, the core part is RMI. It should be that EJB is also set up on the basis of RMI. The following briefly introduces the data flow principle of RMI and provides a development example:
RMI development is divided into two parts: server-side development and client-side development. The server-side provides remote service interfaces and Service implementation, the client needs to provide the remote service interface and the stub that it wants to match. It is worth noting that the remote client interface and the remote server interface must have the same serialuid, otherwise, they will not be able to communicate. Here I will detail the principles of Java object transmission over the network:
If an object needs to be transmitted over the network, it must be serializable. I think almost all Java programmers know how to implement Java. io. the serializable interface can be used, but the communication parties must use this class to receive the object while sending the object. What we need to do is to create two jar packages for this DTO, put one on the server side and the other on the client side so that they can communicate. Here we will make an experiment. When you compile for the first time (javac), you will hit the generated class into the jar package, then, in javac, package the class into another jar package, put the two jar packages on the client and server respectively, and run the program, you will find a style with inconsistent serial numbers. The reader will certainly ask why? The reason is that these two classes are different. For a class that can be listed according to Java specifications, a Private Static final long serialuid field must be provided. We can explicitly set the field in the program, without explicit settings, javac will automatically generate a file to be put into the class file when compiling the source code (do not believe it in javap), because the random serialuid generated by javac is inconsistent twice, this is why the above error has just occurred. Now there are two solutions. The first is to use javac only once and pack the same class twice to the client and server respectively, 2nd explicitly indicates that the serialuid is in the Code, so that the real-time javac does not matter N times .}
Now let's continue with RMI. When a user calls a remote client interface, such as naming. when Lookup ("RMI: // 192.168.0, 1 // servicename"), the JVM will notice that this is an Rmi request, and then it will take this route information (192.168.0.1/servicename) the notification is sent to stub, and stub is responsible for sending a request to the remote server's RMI Central Registrar. The Central Registrar calls the service through this service name (servicename) to find the registered object instance, at this time, this instance is called for processing. After the return value is returned in the original reverse direction, the acceptor performs the reverse sequence. (Note: the serialization and deserialization tasks are completed by the two-end stub and framework ). the following code describes how to complete the remote call process:
A. Create a remote interface: McLaren. Java
Package org. McLaren. Remote;
Import java. RMI. Remote;
Import java. RMI. RemoteException;
Public interface McLaren extends remote {
Public String sayhello (string name) throws RemoteException;
}

B. Create a service implementation: mclarenservice. Java
Package org. McLaren. Remote;
Import java. RMI. server. unicastremoteobject;
Import java. RMI. RemoteException;
Import java. RMI. Naming;
Public class mclarenservice extends unicastremoteobject implements McLaren {
Public mclarenservice () throws RemoteException {
Super ();
}
Public void registerservice (string servicename ){
Try {
// Bind the name and register it with the Central Registrar
Naming. rebind (name, this );
System. Out. println ("service started ");
} Catch (exception e ){
E. printstacktrace ();
}
}
Public String sayhello (string name) throws RemoteException {
Return "Hello:" + name + "this is processed by RMI service ":
}
}

C. Compile the Service Startup class: startup. Java
Package org. McLaren. Remote;
Import java. RMI. rmisecurirymanager;
Public class startup {
Public static void main (string [] ARGs ){
Try {
System. setsecuritymanager (New rmisecuritymanager ());
Mclarenservice McLaren = new mclarenservice ();
McLaren. registerservice ("// mclarenservice ");
} Catch (exception e ){
E. printstacktrace ();
}
}
}

D. Write the security policy file McLaren. Policy
Grant {
Permission java. Security. allpermission ("192.168.0.2, 192.168.0.3 ","");
};
Note: This section allows any port of the two machines 192.168.0.2 and 192.168.0.3 to use this service for anything. Of course, you can change it.

E. prepare for the service: Open the CMD or terminal (Linux) and run the rmiregistry command in the jdk_home/bin to start the RMI Central Registration machine (rmiregistry Syntax: rmiregistry <port>, default: 1099 ).
Run the command rmic org. McLaren. Remote. mclarenservice to generate the client stub, and then package stub and remote interfaces into the client. jar file:
F. Start Service: assume that the class output directory of our project is in: F:/rmitest/classes, and McLaren. Policy is in F:/rmitest. Then execute in cmd:
Java-classpath F:/rmitest/classes Org. mcLaren. remote. startup-djava. RMI. server. codebase = file: // F: // rmitest // classes/-djava. security. policy = file: // F: // rmitest // McLaren. policy
The output is displayed as follows: the service is started.

Now develop the client: Import client. Jar
Public class client {
Public static void main (string [] ARGs ){
Try {
McLaren = (McLaren) Naming. Lookup ("RMI: // 192.168.0.1: 1099 // mclarenservice ");
McLaren. sayhello ("McLaren ");
} Catch (exception e ){
E. printstacktrace ();
}
}
}

After execution, the following error occurs: Hello: McLaren this is processed by RMI service"
This is not so complicated for EJB. We read this book on our own. How can we integrate Java with C/C ++.
A. java calls C ++: When Java calls C ++, it will use the JNI interface. I will not talk nonsense about the concept of JNI, and I will go to so by myself. (in other aspects of China, find a bunch of concepts ). the following is an example:
(1) first create a Java file: McLaren. Java
Package org. McLaren. JNI;
Public class McLaren {
Static {
System. loadlibrary ("McLaren"); // load DLL or so
}
Public native void sayhello (string name );
}
(2) generate the header file: Enter javah org. McLaren. JNI. McLaren under the classes directory to generate a C header file org_mclaren_jni_mclaren.h
(3) create a VC project, add % java_home %/include and % java_home %/include/Win32 to include, or directly add JNI. H and jni_md.h can also be copied, and then create a CPP file McLaren. CPP (I use C ++ to write). In this way, your VC project directory contains four files: JNI. h, jni_md.h. org_mclaren_jni_mclaren.h, McLaren. CPP
The content of McLaren. cpp is:
# Include <iostream>
# Include "org_mclaren_jni_mclaren.h"
Using namespace STD;
Jniexport jnicall org_mclaren_jni_mclaren_sayhello (
Jnienv * ENV, jobject OBJ, jstring name ){
Char * STR = (char *) Name;
Cout <"C ++ output:" <STR <Endl;
Delete STR;
}

Package the above project into a DLL and place it anywhere on the hard disk. Suppose D: // lib // McLaren. DLL, we only need to put this DLL in the librarypath of the program runtime.
Write the following test classes:
Public class test {
Public static void main (string [] ARGs ){
McLaren = new McLaren ();
McLaren. sayhello ("McLaren ");
}
}

Assume that the classes directory of this project is: F: // jnij2c/classes
So: Java-classpath F: // jnij2c/classes test-djava. Library. Path = D: // lib // McLaren. dll

At this time, the output will be: C ++ output McLaren.


B. when C ++ calls Java: C ++ calls Java, it needs c ++ to start the Java Virtual Machine and load classes, next I will talk about it in Windows and Linux respectively:
(1) In Windows:
A. create a C ++ Project (MFC is not required), and set java_home/include/JNI. copy jni_md.h under H java_home/include/Win32 to the c ++ project directory, and then copy java_home/JRE/bin/client/JVM. DLL is written into the system path, and then java_home/lib/JVM. H is added to the project lib of VC.
B. Create McLaren. cpp with the following content:
# Include "JNI. H"
# Include <iostream>
Using namespace STD;
Int main (){
Jnienv * env;
JavaVM * JVM;
Javavmoption options [3];
Javavminitargs ARGs;
Memset (& ARGs, 0, sizeof (& ARGs ),)
Args. Version = jni_verion_1_4;
Args. noptions = 3;
Args. Option = options;
Args. ignoreunrecognized = jni_true;
Options [0] = "-djava. Class. Path = .";
Options [1] = "-djava. Library. Path = .";
Options [2] = "-verbose: JNI ";

Int ret = jni_createjavavm (& JVM, (void **) & ENV, & ARGs );
If (Ret <0 ){
Cout <"failed to start JVM" <Endl;
Return-1;
}
// We load the above RMI client class
Jclass linoleic = env-> findclass ("org/McLaren/remote/client ");
// Obtain the main method. The 3rd parameters are Java signatures.
Jmethodid init = env-> getstaticmethodid (linoleic, "Main", "([-ljava. Lang. String) V ");
Env-> callstaticmethod (linoleic, init, null );
JVM-> destroyjavavm ();
Return 0;
}

After the VC is compiled, the effect obtained by executing the command is the same as that started with java.exe.

(2) in Linux:
A. The Code remains unchanged, because the above uses the Standard C ++ Library
B. Set the environment variables java_home, classpath, and path.
C. append an entry in. bash_profile:
Export ld_lobrary_path = $ java_home/JRE/lib/i386/client
Enter the command in the directory where McLaren. cpp is located:
G ++-I $ java_home/include/Linux-L $ LD_LIBRARY_PATH-O McLaren. cpp-ljvm
Note: "-ljvm is lowercase L
In this way, an executable file of McLaren will be generated and executed. The effect is the same as above.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.