The principle of spring integrated RMI
The core of the client is Rmiproxyfactorybean, which contains the Serviceurl property and the Serviceinterface property.
Access the service through JRMP. JRMP Jrmp:java Remote Method Protocol,java is a unique, stream-based protocol.
Service-side exposure to remote services
Rmiserviceexporter exports any spring-managed beans to an RMI service. Work by wrapping the bean in an adapter class. The adapter class is bound to the RMI registry, and the request is proxied to the service class.
RMI Service-Side implementation class
PackageCom.impl;ImportCom.interfaces.IHelloWord;/*** Created by Lunhui.wei on 2014/11/7.*/ Public classHelloWorldImplementsihelloword{@Override PublicString HelloWorld () {return"Hello World"; } @Override Publicstring Sayhellotosomebody (string name) {returnName+ "say:" + "Hello World"; }}
Service-side RMI interface class
Public Interface Ihelloword { public String helloWorld (); Public string Sayhellotosomebody (string name);}
Service-Side Run class
Packagecom;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;/*** Created by Lunhui.wei on 2014/11/7.*/ Public classRun { Public Static voidMain (String args[]) {ApplicationContext ApplicationContext=NewClasspathxmlapplicationcontext ("Spring-config.xml"); System.out.println ("RMI Service started with spring start ..."); }}
Server-side spring-config.xml configuration file
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <BeanID= "HelloWorld"class= "Com.impl.HelloWorld"/> <BeanID= "Serviceexporter"class= "Org.springframework.remoting.rmi.RmiServiceExporter"> < Propertyname= "Service"ref= "HelloWorld"/> < Propertyname= "ServiceName"value= "Hello"/> < Propertyname= "Serviceinterface"value= "Com.interfaces.IHelloWord"/> < Propertyname= "Registryport"value= "8088"/> </Bean></Beans>
The Client interface class directly pastes the past using the interface class of the service side.
The running class for the client:
ImportCom.interfaces.IHelloWord;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;/*** Created by Lunhui.wei on 2014/11/7.*/ Public classRun { Public Static voidMain (String args[]) {ApplicationContext ApplicationContext=NewClasspathxmlapplicationcontext ("Spring-config.xml"); Ihelloword Helloword= (Ihelloword) applicationcontext.getbean ("HelloWorld"); System.out.println (Helloword.helloworld ()); System.out.println (Helloword.sayhellotosomebody ("Zhangsan")); }}
Configuration of Client Spring:
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > <BeanID= "HelloWorld"class= "Org.springframework.remoting.rmi.RmiProxyFactoryBean"> < Propertyname= "serviceurl"value= "Rmi://10.61.5.14:8088/hello"/> < Propertyname= "Serviceinterface"value= "Com.interfaces.IHelloWord"/> </Bean></Beans>
Use of Spring RMI