SpringRMI source code
Author: Li hongxuan. During reprinting, please specify the author's source QQ group number: JAVA high-level technical exchange (215597268)
1. Interpretation of RMI server Source Code
Spring RMI service is released through the RmiServiceExporter class. The result of the RmiServiceExporter class graph is as follows:
When the Spring container is started, the prepare method of RmiServiceExporter is called to release the Rmi service. The Code is interpreted as follows:
The prepare process is as follows:
1. Obtain the Register object of RMI to Register the port service.
2. Get the object getObjectToExport for publishing the service
Determine the Wrapper packaging or direct interface publishing based on the type of the published service interface.
3. Use UnicastRemoteObject to publish RMI service objects
The entire Spring RMI service release process is described above. The entire process is concise and easy to understand.
RmiServiceExporter attribute Configuration:
A) The registryPort service registers the port. The default value is 1099.
B) registryHost service registers IP addresses (it is recommended to specify IP addresses under multiple NICS). The default value is null, indicating that IP addresses are not specified.
C) serviceName service name, which cannot be blank
D) servicePort service port. The default value is 0, indicating that the anonymous port will be used when communicating with the service.
E) Whether or not alwaysCreateRegistry reuse the port registered by Registry. The default value is false, indicating reuse.
F) replaceExistingBinding whether to replace the published service. The default value is true, indicating replacement.
2. Interpretation of the source code of the RMI Client
Spring RMI service is released through the RmiProxyFactoryBean class. The result of the class diagram of RmiProxyFactoryBean is as follows:
When the Spring container is started, the afterPropertiesSet method of RmiProxyFactoryBean will be called,
To complete the proxy function of the Rmi service port. The Code is interpreted as follows:
1. Create a proxy object
2. The invoke method is triggered when the method of the contemporary object is called.
The getStub method is used to call the RMI service to obtain the RMI service object.
The doInvoke method is called through reflection of the obtained RMI service object.
The doInvoke method is used to determine the type of the RMI service object.
The following figure shows the Wrapper-based services in Spring.
The following figure shows the general RMI service calling time.
Descriptions of attribute configuration of RmiProxyFactoryBean:
A) Whether lookupStubOnStartup is connected to the RMI service when it is started. The default value is true, indicating that the client is connected immediately at startup. We recommend that you set this attribute to false to avoid service failure on the client when the server is unavailable.
B) cacheStub caches RMI service objects. The default value is true, indicating cache. Pay attention to the cached objects. When an exception occurs on the network, the Service may become unavailable.
C) refreshStubOnConnectFailure whether to reconnect when the service is unavailable. The default value is false, indicating no reconnection. We strongly recommend that you set this attribute to true.
Related Articles
Http://java.sun.com/developer/onlineTraining/Programming/JDCBook/rmi.html
Http://v1.dione.zcu.cz/java/docs/jdk1.1/docs/guide/rmi/spec/rmiTOC.doc.html
Http://static.springsource.org/spring/docs/2.5.x/reference/remoting.html#remoting-rmi