Java -- Integration of Spring and RMI for remote access (episode)

Source: Internet
Author: User

Java -- Integration of Spring and RMI for remote access (episode)

 

With Spring's support for RMI, you can easily build your distributed applications. On the server side, you can use the org. springframework. remoting. rmi. rmiServiceExporter can expose your services. On the client. springframework. remoting. rmi. rmiProxyFactoryBean can use services exposed by the server, 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:

 

package org.shirdrn.spring.remote.rmi;/** * @author liuyazhuang */public interface AccountService {int queryBalance(String mobileNo);String shoopingPayment(String mobileNo, byte protocol);}
Service implementation, as shown in the following example:

 

 

package org.shirdrn.spring.remote.rmi;import org.apache.log4j.Logger;/** * @author liuyazhuang */public class MobileAccountServiceImpl implements AccountService {private static final Logger LOG = Logger.getLogger(MobileAccountServiceImpl.class);public int queryBalance(String mobileNo) {if (mobileNo != null)return 100;return 0;}public String shoopingPayment(String mobileNo, byte protocol) {StringBuffer sb = new StringBuffer().append(Your mobile number is /).append(mobileNo).append(/, protocol type is /).append(protocol).append(/.);LOG.info(Message is:  + sb.toString());return sb.toString();}}
The server publishes a service for the client to call (remote method). Spring configures server. xml as follows:

 

 

 

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:

package org.shirdrn.spring.remote.rmi;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * @author liuyazhuang */public class RmiServer {public static void main(String[] args) throws InterruptedException {new ClassPathXmlApplicationContext(org/shirdrn/spring/remote/rmi/server.xml);Object lock = new Object();synchronized (lock) {lock.wait();}}}
 

Client calls Service

 

The client configuration client. xml is as follows:

 
In configuration, inject a serviceUrl and serviceInterface to RmiProxyFactoryBean to call the remote method. The call example is as follows:

 

package org.shirdrn.spring.remote.rmi;import org.apache.log4j.Logger;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * @author liuyazhuang */public class RmiClient {private static final Logger LOG = Logger.getLogger(RmiClient.class);public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext(org/shirdrn/spring/remote/rmi/client.xml);AccountService accountService = (AccountService) ctx.getBean(mobileAccountService);String result = accountService.shoopingPayment(13800138000, (byte) 5);LOG.info(result);}}
It can be seen that remote access becomes very easy.


 

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.