Use of Spring RMI

Source: Internet
Author: User

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

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.