Java Learning Path-rmi Learning

Source: Internet
Author: User

Java remote method Invocation, Java invocation, is an application programming interface for implementing remote procedure calls in the Java programming language. It enables programs that are running on the client computer to invoke objects on the remote server. The Remote method invocation feature enables Java programmers to distribute operations in a network environment. The whole purpose of RMI is to simplify the use of remote interface objects as much as possible.

First, 6 steps to create an RMI program

1, define a remote interface interface, each method in the interface must declare that it will produce a remoteexception exception.

2. Define a class that implements the interface.

3. Create a service to publish the classes defined in 2.

4. Create a client program to make RMI calls.

Second, the detailed implementation of the procedure

1. First, we first create an entity class, which needs to implement the serializable interface for the transmission of information.  

1 Importjava.io.Serializable;3  Public classStudentImplementsSerializable {5   PrivateString name;7   Private intAge ;9    PublicString GetName () { One       returnname; -   } the    Public voidsetName (String name) { -        This. Name =name; +   } +    Public intGetage () { at       returnAge ; -   } -    Public voidSetage (intAge ) { in        This. Age =Age ; to   }      -}

2. Define an interface that needs to inherit the remote interface, and the method in this interface must declare the remoteexception exception.

  1  import   Java.rmi.Remote;   import   java.rmi.RemoteException;   import   java.util.List;   public  interface  Studentservice extends   Remote { 12  list<student> getList () throws   remoteexception;  14 } 

3. Create a class and implement the interface in step 2, but you also need to inherit the UnicastRemoteObject class and display the constructor that writes out the argument.

1 Importjava.rmi.RemoteException;3 ImportJava.rmi.server.UnicastRemoteObject;5 Importjava.util.ArrayList;7 Importjava.util.List;  One  Public classStudentserviceimplextendsUnicastRemoteObjectImplements - Studentservice { the    PublicStudentserviceimpl ()throwsRemoteException { -   } +    PublicList<student> getList ()throwsRemoteException { atList<student> list=NewArraylist<student>(); -Student s1=NewStudent (); -S1.setname ("Zhang San"); inS1.setage (15); toStudent s2=NewStudent (); -S2.setname ("John Doe"); *S2.setage (20);Panax Notoginseng List.add (S1); the list.add (S2); A       returnlist; +   } $}

4. Create the service and start the service

1 Importjava.rmi.Naming;2 ImportJava.rmi.registry.LocateRegistry;4  Public classSetservice {6      Public Static voidMain (string[] args) {8         Try {TenStudentservice studentservice=NewStudentserviceimpl (); ALocateregistry.createregistry (5008);//Define port Numbers -Naming.rebind ("Rmi://127.0.0.1:5008/studentservice", studentservice); -SYSTEM.OUT.PRINTLN ("service Started"); -}Catch(Exception e) { - e.printstacktrace (); A         } -     } -}

4. Create a client program to make RMI calls.

1 Importjava.rmi.Naming;3 Importjava.util.List;5  Public classGetService {9    Public Static voidMain (string[] args) { One       Try { -Studentservice studentservice= (Studentservice) naming.lookup ("Rmi://127.0.0.1:5008/studentservice"); thelist<student> list =studentservice.getlist (); -            for(Student s:list) { +System.out.println ("Name:" +s.getname () + ", Age:" +s.getage ()); +           } at}Catch(Exception e) {  - e.printstacktrace (); -       } in   } -}

5. Console Display Results

============= Console ============

Name: Zhang San, age: 15

Name: John Doe, Age: 20

===============================

Configuring RMI Services in Spring

Combining RMI with spring is much more convenient than implementing RMI services above.

1. First we define the interface, at which point the interface defined does not need to inherit the other interface, just a common interface

1  Package Service; 3 Import java.util.List; 5  Public Interface Studentservice {7      List<student> getList (); 9 }

2. Define a class, implement this interface, this class can only implement the interface defined by step one, do not need extra action

1  PackageService;4 Importjava.util.ArrayList;6 Importjava.util.List;9  Public classStudentserviceimplImplementsStudentservice { One   PublicList<student>getList () { -List<student> list=NewArraylist<student>(); theStudent s1=NewStudent (); -S1.setname ("Zhang San"); +S1.setage (15); +Student s2=NewStudent (); atS2.setname ("John Doe"); -S2.setage (20); - List.add (S1); in list.add (S2); to   returnlist; -  } *}

3. Next down in the Applicationcontext.xml configuration required information

A. Defining a service bean first 

<id= "Studentservice"  class= "service. Studentserviceimpl "></bean>

B. Defining an export service

      <class= "Org.springframework.remoting.rmi.RmiServiceExporter"  P: Service-ref= "Studentservice"  p:serviceinterface= "service. Studentservice "  p:servicename=" Studentservice "  p:registryport=" 5008 " /c11>/>      

You can also increase the P:registryhost property setting host

C. Define the bean to be served in the client's Applicationcontext.xml (the example here is to put the exported service bean and the client's bean in a applicationcontext.xml)

<id= "Getstudentservice"  class= " Org.springframework.remoting.rmi.RmiProxyFactoryBean "  p:serviceurl=" rmi://127.0.0.1:5008 /studentservice "  p:serviceinterface=" service. Studentservice "/>

D. The configuration of things so much, is not more convenient than the above reality! Now let's test it.

1  PackageService;2 Importjava.util.List;3 ImportOrg.springframework.context.ApplicationContext;4 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;5  Public classTest {6  Public Static voidMain (string[] args) {7ApplicationContext ctx=NewClasspathxmlapplicationcontext ("Applicationcontext.xml");8Studentservice studentservice= (Studentservice) Ctx.getbean ("Getstudentservice");9list<student> list =studentservice.getlist ();Ten    for(Student s:list) { OneSystem.out.println ("Name:" +s.getname () + ", Age:" +s.getage ()); A   } -  } -}

============= Console ============
Name: Zhang San, age: 15
Name: John Doe, Age: 20
=============================
The above Mian method operation may be error, should be Spring's jar less, own attention to add.

The first time to write a blog, there is no place please point out.

Java Learning Path-rmi Learning

Related Article

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.