In Java, use language and platform feature to improve RMI distribution computing frame Tomhornson (@) Hotmail.com in Lushan Abstract: RMI is an excellent Java-based distribution calculation framework for Sun design, using a classic like Corba, DCOM. STUB / SKELETON design. This paper is based on the actual needs of the RMI enterprise-level distribution calculation, using Java's characteristics, improve RMI, and designs an excellent intercepto (interceptor) -RMI framework.
Keywords: Java; RMI; Stub / Skelton; Serialization; DynamiCProxy; DynamicClassLoading
Abstract:
Keyword:
1 Introduction
1.1JAVA Overview
Strictly speaking, Java refers to JavaPlatform, including JVM (JavaVirtualMachine, Java language, Java core class library and extended class libraries and all applications based on this platform, which is created and leaded by SunMicrosystems. With the "Writeonce, Runeverywhere" and garbage collection, the Java language itself design, the powerful and efficient Java class library, the appropriate open and constraints of Sunmicrosystems, Java undoubtedly become the most popular software technology in today's most popular technology, manufacturing in chip , Smart home appliances, hand-held intelligent business equipment, wireless communications, personal computer applications, enterprise application system architecture, and even search engine development, military system development, aerospace control system development, etc. It can be seen in the field of development and other fields. There is no specification, and the Java in the following refers to the Java language. Following the "NetWorkisComputer" and "WemakeThenetwork" concept, Sun is positioned in the "Language of the Network Age", so it is visible to NetWork everywhere in Java. For example, strict data type definitions, object serialization, code mobility, powerful, and convenient network API, complete security architecture, etc. These features completely subvert the traditional network programming model, everything is simple and powerful.
1.2 Classic Stub / Skeleton Computing Architecture and RMI (RemoteMethodInvocation)
So far, almost all distributed computing frameworks use stub / skelton design, such as OMG CORBA,
Microsoft's DCOM, SunMicrosystems RMI, with "Tongming" remote call. The action sequence is generally as follows: The client's remote call "agent" gives stub, stub and client distribution computing engine interaction, client-side engine communication, transfer call information, server-side distribution computing engine with Skeleton, Skeleon Forward to the remote object implementation. Then, then the opposite path back the result. figure 1
RMI (RemoteMethodInvocation) is Sun Designed based on Java-based lightweight distribution object computing solution.
RMI is not only very good in its own architecture, but also uses and inherits a few unique characteristics of the Java platform, such as platform-independent, (distributed) garbage collection, safety, code mobility, class dynamic loading, etc. It is the distribution characteristic support technology of EJB and JINI.
2 RMI programming model and essence
2.1RMI programming model
2.1.1 Defining a remote interface
PublicinterfaceHelloWorldExtendsRemote {PUBLICSTRINGRETURNGREETING (STRINGPARAM) THROWSREMOTEEXCEPTION;
} This interface defines a remote object behavior, and the RMI specification specifies that the remote interface needs to be directly or indirectly extended the Remote interface and the method signature needs to throw RemoteException. 2.1.2 Realizing remote interface
PublicclassHelloWorldimplextendsunicastRemoteObjectImplementsHelloWorld {
Publichelloworldimpl () throwsRemoteException {super ();}
PUBLICSTRINGRETURNGREETING (STRINGPARAM) {
Return "Hello" Param;
}
}
2.1.3 Server (main.java) generates a remote object and uses Registry to bind it with a certain name.
/ * ............ * /
HelloWorldimplhwi = newhelloworldImpl ();
Naming.bind ("HelloWorld", HWI);
/ * ............ * /
2.1.4 Creating a client (Client.java)
/ * ............ * /
System.setSecurityManager (NewrmiseCurityManager ());
HelloWorldhw = (HelloWorld) Naming.lookup ("RMI: // LocalHost / HelloWorld");
System.out.println (HW.RETURNGREETING ("world");
/ * ............ * /
2.1.5 Compilation Source Code
Javachelloworld.javahelloWorldImpl.javaMain.javaclient.java
Generate file helloworld.class, helloworldimpl.class, main.class, client.class,
HelloWorldImpl_stub.class, helloworldimpl_skel.class.
2.1.6 Operation System
Server side first profile HelloWorld.class, HelloWorldImpl.class, main.class,
HelloWorldImpl_stub.class, helloworldimpl_skel.class file, then execute
JavarimRegistry
JavaMain
The client first profiles helloworld.class, helloWorldImpl_stub.class and execute
JavaClient
2.2RMI essence
Here we describe the examples used in the programming model to describe the operation of tracking the RMI, thus exploring the nature.
2.2.3 In the server (main.java),
HelloWorldimplhwi = newhelloworldImpl ();
Naming.bind ("HelloWorld", HWI);
The above two sentences have realized the export of the remote object (export) and is bound to the name HelloWorld.
Here we need to discuss in-depth discussions, direct essence, because some of our improvements are here. Because HelloWorldImp inherits UnicastRemoteObject, and UnicastRemoteObject passes in the constructor.
ExportoJbet exports objects to specific ports to make objects can be remotely referenced. ExportObject places a remote object into an ObjectTable and loads HelloWorldImpl_stub and binds Stub with the name. Instead of generally think of a remote object. The purpose of this is mainly to take into account the remote object as a parameter or return value, can only be passed to STUB, and cannot move remote object entities to meet the needs of distributed environment. 2.2.2 in Client.java, HelloWorldhw =
(HelloWorld) Naming.lookup ("RMI: // localhost / helloworld") parsing. Nature, use remote objects
Before you need to get a remote object reference. This step is achieved by Registry, and Registry is actually RMI.
Name service tool. It is also a remote object, the default operation is in 2009 port, or you can use
LocateRegistry.createREGISTRY () exports it at a specific port. The client's lookup operation will first take the service first
Remote references of the server-side Registry, then use the remote reference to Query HelloWorldImpl, from the remote reference from the server, actually a stub, so our HW is actually RemoteObject on behalf of Stub. To this end, we have achieved remote references for remote objects.
2.2.3 HW.RETURNGREETING ("World") in Client.java. The client uses a remote reference call method. As the classic step of Stub / Skeleton, the client's call "Agent" gives Stub, you can see ref.invoke in HelloWorldimp_stub.java (this, _fld $ method_hit_0, null,
0x9339F0D6BC98FE87L), then STUB is interacting with the JRMP engine, by the JRMP engine to group (nature is naturally the row) remote reference, call method, method parameters, etc., and communicate with server-side JRMP, transfer call related information. The server-side JRMP engine will be submitted to Skeleton, Skeleton, and get a remote object from ObjectTable, will call it "Distribution".
PublicvoidDispatch (RemoteRemote, RemoteCallRemoteCall, INTI, LONGL) can be found in HelloWorldimp_skel.java, and then the group returns the result, and the reverse path is back to the client.
The core step has been basically described above. Can refer to Figure 2.
3 Several Java Language Characteristics
3.1 Java's serialization feature
The Serialization feature is incremented in JDK1.1, an object as long as IMPLEMENTSSERIALIZABLE,
"Persistence" can be achieved. Persistence capability represents an object to write a medium, and then read out from the medium and restore the original state in memory. This capability of objects is very important for web applications. Because it can support objects in the blocking system during the network transmission. We are concerned about the Any-Access-ModifierObjectReadreadReSolve () throwsObjectStreamException method in the interface, which is used to implement the original object when you want to replace the original object with other objects.
3.2 Java's DynamicClassLoading features
This is the so-called "code move" basis. It is one of Java's largest features. It allows code from the network
Load execution. Classic application is a famous applet. Inherited trees in the Java class library: java.lang.Object-> java.lang.classloader-> java.security.secureclassloader-> java.net.urlclassloader and
Java.lang.Object-> java.rmi.server.rmiclassloader. ClassLoader provides a variety of formal class loading features, especially based on the network to get byte streams, definition and load from the new map class locally. UrlclassLoader provides class load based on the network URL set. RMiclassLoader provides a STATCI functional set, typically used by RMiruntime to implement annotation class path information when the RMI group is united, and the network-based load and other characteristics.
3.3 Java's DynamicProxy features
The DynamicProxy feature is added to the JDK1.3, which follows the idea of Proxy mode. A Dynamci
Proxy can "proxy" a set of interface implementations, and call the actual method to send a proxy
InvocationHandler Implementation. With this feature, we can easily achieve many classic applications, such as one
Group objects provide an entry to achieve new features or interception without changing class design
Wait, etc.
4 Improve the RMI model implementation with the above Java characteristics
As far as it is, RMI solutions are generally based on the foundation of the company's distribution architecture. Of course, in enterprise applications, security, transactional, system diary, etc., then we envisure whether these features can be implemented in the RMI layer, thus establishing design better, more powerful, and more Strong, more convenient enterprise system. After exploration, we realized this idea with the above Java feature and designed an interceptor (interceptor) -RMI framework. image 3
The core issues that need to be resolved and the corresponding solutions are as follows:
1. For the client from the server from the server side to download STUB and install the interceptor, we need to replace the server-side default
The class loader is replaced with DynamicClassLoader.
PublicclassDynamicclassloaderExtendsurlclassLoader {
/ * ............ * /
ProtectedClassFindClass (StringName) throwsclassNotFoundException {
IF (name.endswith ("_ stub")) {
Name = name.substring (0, Name.length () - 5);
Classcl = loadingclass (name);
CurrentClass.Set (Cl.GetInterfaces () [0]);
ReturndynamicRemotestub.class;
} else {returnsuper.findclass (name);
}
In this way, we can use our own ClassLoader when loading a remote object, to dynamically replace STUB.
2. What is DynamicRemotestub?
PublicclassdyNamicRemotestubExtendSremotestub {
/ * ............ * /
ObjectReadResolve () throwsObjectStreamException
{
IF (cl == null) returnthis;
DynamicsTubHandlerstubhandler = NewdyNamicsTubhandler ();
ObjectProxy = proxy.newproxyinstance (Cl.getClassLoader (),
Newclass [] {cl},
StubHandler); Stubhandler.SetProxy (this);
CL = NULL;
ReturnProxy;
}
}
Here we need to pay attention to ReadResolve, which is called when the object serialization is called, implemented a certain method, replacing the original descending object with other objects. We implemented this behavior here returned to the agent, where the interceptor is added to the client.
3. What is the processor invocationhandler of the agent? It is DynamicsTubHandler:
PublicclassdyNamicsTubhandlerImplements
InvocationHandler, java.io.serializable {
/ * ............ * /
// InvocationHandlerImplementation
PublicObjectInvoke (ObjectProxy,
MethodMethod,
Object [] args) throwsthrowable {
ReturnStub.getRef (). Invoke (stub, method, args, gethash (method);
}
}
/ * ............ * /
We can see ReturnStub.getRef (). Invoke (stub, method, args,
GetHash (Method), by it, the client is finally invoked by the remote object after the client is called after any stopper.
4. The interceptor of the server is implemented.
HelloWorldhw = newhelloWorldImpl ();
UnicastRemoteObject.exportObject (HW);
HW = (helloworld) proxy.newproxyinstance (HelloWorld.class.GetclassLoader (),
Newclass [] {helloworld.class},
NewLogProxy (HW));
UnicastRemoteObject.exportObject (HW);
/ * ............ * /
HW = (helloworld) proxy.newproxyinstance (hw.getclass (). getClassLoader (),
Newclass [] {helloworld.class},
NewReconnectProxy (Server, HelloWorld.Name);
HW = (helloworld) proxy.newproxyinstance (HelloWorld.getClass (). getClassLoader (),
Newclass [] {helloworld.class},
NewperFormanceProxy (HW));
Naming.bind (hellotWorld.name, Server);
Only examples are only examples, we are intended to add a logProxy in the server side, add it on the client
ReconnectProxy and PerformanceProxy. Determine whether it is a client interceptor whether to use UnicastRemoteObject.ExportObject export a remote object, because the RMI specification specification does not bind the object to the name, when the remote reference is used, the remote object itself directly into the client, Instead of its stub, a trick is implemented here.
5. We can see that a large number of interceptors are used above. In fact, the main function of the interceptor is to implement special
Demand, and transfer the call. The most basic requirements, they need to inherit the InvocationHandler, and
Implementsserializable.
LogProxy example:
PublicclassLogproxyImplementsInvocationHandler, Java.io.Serializable, LinkedProxy {
/ * ............ * /
PublicObjectInvoke (ObjectProxy,
MethodMethod,
Object [] args) throwsthrowable {
/ **
Omitted: the specific logProxy interceptor behavior is implemented here
** /
Try {
ReturnMethod.Invoke (Next, Args);
} catch (invocationtargetexceptione) {
Throwe.gettargetexception ();
/ ** omitted: Next related operations ** /
}}}
5 conclusion: improvement and application
RMI is one of the most excellent distributed computing frameworks, which is very wide, especially an enterprise infrastructure.
The transaction characteristics, diary, performance evaluation, excellent extensive extensibility, and convenience of network management management are necessary. The Interceptor-RMI framework is designed to accommodate this demand. It provides an excellent reference for the enterprise distribution calculation infrastructure.
references
[1] Sunmicrosystems, Inc.rmispecification [R]. Sunmicrosystems, 2003.
[2] W. KeitHedwards.Jini core technology. Beijing: Machinery Industry Press, 2000.
[3] EDROMAN, Scottambler. Proficient EJB [M]. Beijing: Electronic Industry Press, 2002.
[4] MarkoBoger.java and Distributed System [M]. Beijing: Machinery Industry Press, 2003.
[5] Rickardöberg. Excellent RMI-Java and EJB enterprise-class application development. Beijing: Machinery Industry Press, 2003.
[6] Zhu Gang et al. LINUX network programming. Beijing: Science Press, 2000.
[7] Donbox.com Nature Theory. Beijing: China Electric Press, 2002.
[8] He Yanxiang, Chen Yumeng. Design and application of the Agent system. Wuhan: Wuhan University Press, 2001.