the Java program has the following remote invocation technology options:
The Remote Procedure call (RPC) is synchronous and the client is blocked until the server returns the results.
The following scenarios apply to various technologies:
The typical RMI development process is as follows:
- Define an interface for client and server-side interaction, and the interface inherits the remote interface, and all methods are thrown remoteexception.
- Write the server-side implementation to implement the interface written in the first step.
- Write a registration class that registers the implementation of a server-side class based on an IP and Port ( By default , 1099) .
- Write a call to the client, based on the IP, Port, and registered name to find the corresponding class on the server side.
- RMI supports passing objects that require the object to implement the serializable interface.
Here is the spring support for RMI, and the configuration is simple:
One: Server-side
1. Interface of the service to expose:
Package Com.excellence.webservice; Import java.util.List; Public Interface Accountservice { publicvoid insertaccount; Public List getaccounts (String name);}
package Com.excellence.webservice; import java.util.List; public class Accountserviceimpl implements Accountservice { public void Insertaccount (account account) {System.out.println ( "inser!" ); public List getaccounts (String name) { System.out.println ( "Get" ); return null ; }}
3. Publish the interface to RMI in the configuration file
class class= "Org.springframework.remoting.rmi.RmiServiceExporter" > <property name= "ServiceName" Value= "Accountservice" ></property> <property name= "service" ref= "Accountservice" ></ property> <property name= "serviceinterface" value= "Com.excellence.webservice.AccountService" > </property> <property name= "Registryport" value= "1199" ></property></bean>
4. Run the RMI:
Public class Demo {publicstaticvoid main (string[] args) { new Filesystemxmlapplicationcontext ("Classpath:applicationContext.xml"); = (Rmiserviceexporter) ctx.getbean ("service");} }
Second, the client
1. Configure in the configuration file:
class= "Org.springframework.remoting.rmi.RmiProxyFactoryBean" > <property name= "serviceurl" Value= "Rmi://localhost:1199/accountservice" ></property> <property name= "Serviceinterface" Value= "Com.excellence.webservice.AccountService" ></property></bean>
Public Static void Main (string[] args) { new filesystemxmlapplicationcontext ("Classpath: Applicationcontext.xml "); = (Accountservice) ctx.getbean ("Accclient"); Service.insertaccount (new Account ()); Service.getaccounts ("DD");}
Spring Support for remote services