Integrate Spring with RMI for remote access

Source: Internet
Author: User
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:

  1. PackageOrg. shirdrn. spring. remote. rmi;
  2. Public InterfaceAccountService {
  3. IntQueryBalaNcE (STrIng mobileNo );
  4. String shooPingPayment (String inclueno,ByteProtoCol);
  5. }

Service implementation, as shown in the following example:

  1. PackageOrg. shirdrn. spring. remote. rmi;
  2. ImportOrg. apache. log4j. Logger;
  3. Public ClassMobileAccountServiceImplImplementsAccountService {
  4. Private StatIc FinalLogger LOG = Logger. getLogger (MobileAccountServiceImpl.Class);
  5. Public IntQueryBalance (String inclueno ){
  6. If(MobileNo! =Null)
  7. Return 100;
  8. Return 0;
  9. }
  10. PublicString shoopingPayment (String inclueno,ByteProtocol ){
  11. StringBuffer sb =NewStringBuffer (). append ("Your mobile number is /""). Append (
  12. MobileNo). append ("/", Protocol type is/""). Append (protocol)
  13. . Append ("/".");
  14. LOG.info ("Message is :"+ Sb. toString ());
  15. ReturnSb. toString ();
  16. }
  17. }

The server publishes a service for the client to call (remote method). Spring configures server. xml as follows:

  1. Xml Version="1.0" Encoding=UTF-8"?>
  2. <Beans XmLnS=Http://www.springframework.org/schema/beans"
  3. Xmlns: xsi=Http://www.w3.org/2001/XMLSchema-instance" Xmlns: p=Http://www.springframework.org/schema/p"
  4. Xsi: schemaLoCatIon=Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5. <Bean Id="ServiceExporter" Class="Org. springframework. remoting. rmi. RmiServiceExporter">
  6. <Property Name="ServiceName" Value="MobileAccountService" />
  7. <Property Name="Service" Ref="AccountService" />
  8. <Property Name="ServiceInterface"
  9. Value="Org. shirdrn. spring. remote. rmi. AccountService" />
  10. <Property Name="RegistryPort" Value="8080" />
  11. <Property Name="ServicePort" Value="8088" />
  12. Bean>
  13. <Bean Id="AccountService" Class="Org. shirdrn. spring. remote. rmi. MobileAccountServiceImpl" />
  14. 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:

  1. PackageOrg. shirdrn. spring. remote. rmi;
  2. ImportOrg. springframework. contExT.SuPport. ClassPathXmlApplicationContext;
  3. Public ClassRmiServer {
  4. Public Static VoidMain (String [] args)ThrowsInterruptEdException {
  5. NewClassPathXmlApplicationContext ("Org/shirdrn/spring/remote/rmi/server. xml");
  6. Object lock =NewObject ();
  7. SyncHronized(Lock ){
  8. Lock. wait ();
  9. }
  10. }
  11. }

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.