With Spring's support for RMI, you can easily build your distributed applications. On the server side, you can use org. springframework. remoting. rmi. RmiServiceExporter of Spring to expose your services. on the client side, you can use org. springframework. remoting. rmi. RmiProxyFactoryBean exposed by the server.
With Spring's support for RMI, you can easily build your distributed applications. On the server side, you can use org. springframework. remoting.RmI. RmiServiceExporter canExpose your services. On the client, you can use the services exposed by the server through org. springframework. remoting. rmi. RmiProxyFactoryBean, which is very convenient. This access method of the C/S model can eliminate the complexity of RMI, such as the processing details of server Skeleton and client Stub. For service developers and service users, they are transparent and focus on developing your business logic without excessive attention.
The following example shows how to integrate RMI through Spring.
Server Publishing Service
We have defined a service interface. The server implements this service interface to complete its complex logic. The client can call the service exposed by the server through this interface, as shown below:
- PackageOrg. shirdrn. spring. remote. rmi;
- Public InterfaceAccountService {
- IntQueryBalaNcE (STrIng mobileNo );
- String shooPingPayment (String inclueno,ByteProtoCol);
- }
Service implementation, as shown in the following example:
- PackageOrg. shirdrn. spring. remote. rmi;
- ImportOrg. apache. log4j. Logger;
- Public ClassMobileAccountServiceImplImplementsAccountService {
- Private StatIc FinalLogger LOG = Logger. getLogger (MobileAccountServiceImpl.Class);
- Public IntQueryBalance (String inclueno ){
- If(MobileNo! =Null)
- Return 100;
- Return 0;
- }
- PublicString shoopingPayment (String inclueno,ByteProtocol ){
- StringBuffer sb =NewStringBuffer (). append ("Your mobile number is /""). Append (
- MobileNo). append ("/", Protocol type is/""). Append (protocol)
- . Append ("/".");
- LOG.info ("Message is :"+ Sb. toString ());
- ReturnSb. toString ();
- }
- }
The server publishes a service for the client to call (remote method). Spring configures server. xml 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: p=Http://www.springframework.org/schema/p"
- Xsi: schemaLoCatIon=Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <Bean Id="ServiceExporter" Class="Org. springframework. remoting. rmi. RmiServiceExporter">
- <Property Name="ServiceName" Value="MobileAccountService" />
- <Property Name="Service" Ref="AccountService" />
- <Property Name="ServiceInterface"
- Value="Org. shirdrn. spring. remote. rmi. AccountService" />
- <Property Name="RegistryPort" Value="8080" />
- <Property Name="ServicePort" Value="8088" />
- Bean>
- <Bean Id="AccountService" Class="Org. shirdrn. spring. remote. rmi. MobileAccountServiceImpl" />
- Beans>
In the preceding configuration, the name of the exposed service is specified and injected to RmiServiceExporter through the serviceName attribute. The service name is MobileAccountService, and the client can call the service by using this service name.
Start the server and publish the Service as follows:
- PackageOrg. shirdrn. spring. remote. rmi;
- ImportOrg. springframework. contExT.SuPport. ClassPathXmlApplicationContext;
- Public ClassRmiServer {
- Public Static VoidMain (String [] args)ThrowsInterruptEdException {
- NewClassPathXmlApplicationContext ("Org/shirdrn/spring/remote/rmi/server. xml");
- Object lock =NewObject ();
- SyncHronized(Lock ){
- Lock. wait ();
- }
- }
- }