Spring Support for remote services

Source: Internet
Author: User

  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:

    1. Define an interface for client and server-side interaction, and the interface inherits the remote interface, and all methods are thrown remoteexception.
    2. Write the server-side implementation to implement the interface written in the first step.
    3. Write a registration class that registers the implementation of a server-side class based on an IP and Port ( By default , 1099) .
    4. Write a call to the client, based on the IP, Port, and registered name to find the corresponding class on the server side.
    5. 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

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.