Springrmi parsing 1-using examples

Source: Internet
Author: User

Java remote method Invocation, Javarmi (Java remote methods invocation), is an application programming interface for implementing remote procedure calls in the Java programming language. It enables programs that are running on the client computer to invoke objects on the remote server. The Remote method invocation feature enables Java programmers to distribute operations in a network environment. The whole purpose of RMI is to simplify the use of remote interface objects as much as possible.

JAVA RMI relies heavily on interfaces. When you need to create a remote object, the programmer hides the underlying implementation details by passing an interface. The client obtains a remote object handle that is directly linked to the local root code, which is responsible for communicating through the network. In this way, programmers simply care about how to send messages through their own interface handles.

RMI (Remote Method invocation) is an API feature that starts with JDK 1.1, which allows the client to use the services provided by the remote service as it does with local services, but the RMI must be used in a series of cumbersome procedures. such as a service interface must inherit the Java.rmi.Remote interface when it is defined, the service server must inherit the Java.rmi.UnicastRemoteObject class when it is implemented, must use rmic directives to generate stubs and skeleton, The procedure of setting up is complicated.

Spring RMI actually extends the implementation of the next Java RMI, and can use RMI using the bean's XML configuration. You can use the Org.springframework.remoting.rmi.RmiServiceExporter in spring to simplify the process of using RMI, to actually look at examples, to understand the use and simplification of spring on RMI.

First define a service interface

 Package org.spring;    Public Interface Rmiservice {      public  String doWork ();        Public int Add (intint  b);  }  

Service Interface Implementation

 Package org.spring;    Public class Implements rmiservice{      @Override      public  String doWork () {          return ' this Message return from server ";      }      @Override      publicint Add (intint  b) {            return A +b;      }  }  

Defined in the bean definition file, let Spring manage and generate the bean instance so you can register and start the RMI service

<?XML version= "1.0" encoding= "UTF-8"?>  <Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans- 2.5.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Http://www.springframework.org/schema/aop/spri Ng-aop-2.5.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/s Pring-tx-2.5.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/sch Ema/context/spring-context-2.5.xsd "Default-autowire= "ByName">      <BeanID= "Rmiservice"class= "Org.spring.RmiServiceImpl"/>      <BeanID= "Serviceexporter"class= "Org.springframework.remoting.rmi.RmiServiceExporter">          < Propertyname= "Service">              <refBean= "Rmiservice"/>          </ Property>          < Propertyname= "ServiceName">              <value>Rmiservice</value>          </ Property>          < Propertyname= "Serviceinterface">              <value>Org.spring.RmiService</value>          </ Property>              </Bean>  </Beans>  

Start RMI Service

 Packageorg.spring; ImportJava.io.BufferedReader; Importjava.io.IOException; ImportJava.io.InputStreamReader; ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;  Public classRmiserver { Public Static voidMain (string[] args)throwsIOException {NewClasspathxmlapplicationcontext ("Config/rmi-server.xml"); BufferedReader Reader=NewBufferedReader (NewInputStreamReader (system.in));  while(true) {              if(Reader.readline (). Equals ("Exit") {system.exit (0); }          }      }  }  

On the client side, the dependency interface corresponding to the jar package is ready, and then the spring configuration file is configured to the address of the service to be accessed and the corresponding interface name

<?XML version= "1.0" encoding= "UTF-8"?>  <Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans- 2.5.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Http://www.springframework.org/schema/aop/spri Ng-aop-2.5.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/s Pring-tx-2.5.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/sch Ema/context/spring-context-2.5.xsd "Default-autowire= "ByName">       <BeanID= "Rmiserviceproxy"class= "Org.springframework.remoting.rmi.RmiProxyFactoryBean">          < Propertyname= "serviceurl">              <value>Rmi://localhost/rmiservice</value>          </ Property>          < Propertyname= "Serviceinterface">              <value>Org.spring.RmiService</value>          </ Property>      </Bean>         </Beans>

Notice the setting of the "serviceurl" property, which starts with "rmi://", then specifies the service address and service name, and writes a simple client program to use the service on the RMI server

 Packageorg.spring; ImportOrg.springframework.context.ApplicationContext; ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;  Public classrmiclient { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Config/rmi-client.xml"); Rmiservice Service=(Rmiservice) Context.getbean ("Rmiserviceproxy"); String RESULT1=service.dowork ();          System.out.println (RESULT1); intRESULT2 = Service.add (1, 2);      System.out.println (RESULT2); }  }  

Executes the Rmiserver.java class, which is applied in the spring Web application as long as the configuration file is loaded into spring's load path. Then execute the Rmiclient.java class.

This makes it easy to simplify the RMI invocation of Java using Spring, and it is managed by spring to enable the client to rely only on interfaces and not on implementations in distributed applications.

Springrmi parsing 1-using examples

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.