The use of the RMI technology is demonstrated in the blog "RMI Remote Method invocation Technology" Using pure Java code, and this blog will show you how to use RMI technology in spring.
As the blog "RMI Remote Method invocation Technology" mentions, the application of--rmi technology is typically included in two separate applications: RMI server-side applications and RMI client applications. This blog also starts from these two aspects:
RMI Server-side applications:
1. Custom Remote Interface
The code is as follows:
Package com.ghj.packageofrmi;/** * Defines the remote interface. * * @author Gao Yingjie */public interface ihellowordservice{/** * Get information * * @author Gao Yingjie * * String Getmsg ();}
2. Custom remote Interface Implementation class
The code is as follows:
Package com.ghj.packageofrmi;/** * Remote interface implementation class. * * @author Gao Yingjie */public class Hellowordservice implements ihellowordservice{ /** * Get information * * @ Author Gao Yingjie * /Public String getmsg () { return "Hello world!"; }}
3. Configure RMI server in XML file
The code is as follows:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "Http://www.w3.org/2001/XMLSchema-instance" 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/context Http://www.springframework.org/schema/context /spring-context-2.5.xsd "> <!--RMI Service-and <!--RMI Service-Side remote interface implementation class--<bean id=" Hellowordservice "class=" Com.ghj.packageofrmi.HelloWordService "scope=" prototype "/> <bean class=" Org.springframework.remoting.rmi.RmiServiceExporter "><!--to set the remote interface implementation class object to the RMI service--><property name=" The service "ref=" Hellowordservice "/><!--set the RMI service name, laying the foundation for RMI clients to obtain a remote interface implementation class object reference based on this name--><property name=" ServiceName "value=" Helloword "/><!--set the remote interface to RMI service interface--><property name=" serviceinterface "value=" com. Ghj.packageofrmi.IHelloWordService "/><!--set port number for RMI Server remote object Registry--><property name=" Registryport "value= "9090"/><!--other properties to see Org.springframework.remoting.rmi.RmiServiceExporter's class and its subclasses get to--></bean> </ Beans>
4. Start RMI Server
The code is as follows:
Package Com.ghj.packageoftest;import org.springframework.context.support.classpathxmlapplicationcontext;/** * Start RMI Server * * @author Gao Yingjie */public class Startrmiserver {public static void main (string[] args) { new Classpathxmla Pplicationcontext ("Conf/spring/applicationcontext.xml"); SYSTEM.OUT.PRINTLN ("RMI Server start!!! "); }}
RMI Client application:
1. A custom RMI Client interface that contains an abstract method within the RMI server-side custom remote interface
The code is as follows:
Package com.ghj.packageofrmi;/** * Custom interface with RMI Server custom remote Interface name * * @author Gao Yingjie */public interface ihelloword{ /**< c2/>* Get information * * @author Gao Yingjie * /String getmsg ();}
2. Configure RMI server in XML file
The code is as follows:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" 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/context http://www.springframework.org/schema/context/spring-context-2.5.xsd "> <!--RMI Client -- <bean id= "Dataexchangeproxyservice" class= "Org.springframework.remoting.rmi.RmiProxyFactoryBean" > < Property Name= "serviceurl" value= "Rmi://localhost:9090/helloword"/> <property name= "Serviceinterface" Value= "Com.ghj.packageofrmi.IHelloWord"/> <!--If the connection fails, refresh the remote call, and <property name= " Refreshstubonconnectfailure "value=" true "/> </bean></beans>
3. Start RMI Client
The code is as follows:
Package Com.ghj.packageoftest;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import com.ghj.packageofrmi.ihelloword;/** * Start RMI Client * * @author Gao Yingjie */public class Rmiclient {public static void main (string[] args) {ApplicationContext ctx = new Classpathxmlapplicationcontext ("Conf/spring/applicationcontext.xml"); Ihelloword Helloword = (IHelloWord) Ctx.getbean ("Dataexchangeproxyservice"); System.out.println (Helloword.getmsg ());}}
are summarizing the finishing ...
"0 minutes Download Sample source code "
How to use RMI technology in the spring framework