Java RMI Getting Started case

Source: Internet
Author: User

Java Remote method Invocation (Java RMI) is a Java API that performs calls to remote methods, which is equivalent to Procedure Calls (RPC). Java RMI supports the direct transfer of serialized Java classes, as well as distributed garbage collection.

Case overview

Implementing a simple Java RMI can be done in the following four steps:

1. Start Rmiregistry

2. Define the service interface, implement the service interface, create stub files with the Rmic tool

3. Implement the service-side function and start

4. Implement client functions and invoke service-side services

Specific implementation

1. Start Rmiregistry

2. Define the service interface, implement the service interface, create stub files with the Rmic tool

Define the Service interface:

 Package Service; Import Java.rmi.Remote; Import java.rmi.RemoteException;  Public Interface extends Remote {    throws  remoteexception;    }

Implement the Service Interface:

 PackageService;Importjava.rmi.RemoteException;ImportJava.rmi.server.UnicastRemoteObject; Public classGreetserviceimplextendsUnicastRemoteObjectImplementsGreetservice {Static Final LongSerialversionuid = 1L;  PublicGreetserviceimpl ()throwsRemoteException {Super(); } @Override PublicString SayHello (string name)throwsRemoteException {return"Hello-" +name; }}

Use Rmic to generate stub files based on compiled Greetserviceimpl

Grs:bin grs$ rmic Service. Greetserviceimpl

After execution, the directory is as follows

grs:bin grs$ tree.└──service    ├──greetservice.class    ├──greetserviceimpl.class    └──greetserviceimpl _stub.class35

3. Implement the service-side function and start

 Packageserver;Importjava.net.MalformedURLException;Importjava.rmi.AlreadyBoundException;Importjava.rmi.Naming;Importjava.rmi.RemoteException;ImportJava.rmi.registry.LocateRegistry;Importservice. Greetserviceimpl; Public classServer { Public Static voidMain (string[] args) {Try{locateregistry.createregistry (1098); Naming.bind ("Rmi://127.0.0.1:1098/greetservice",NewGreetserviceimpl ()); } Catch(RemoteException e) {e.printstacktrace (); } Catch(malformedurlexception e) {e.printstacktrace (); } Catch(alreadyboundexception e) {e.printstacktrace (); }    }}

After compiling, the server-side file directory structure is as follows:

grs:bin grs$ tree.├──server│└──server. class └──service    ├──greetservice. class     └──greetserviceimpl. class2 directories, 3

Start the service side

Grs:bin grs$ java-server Server. Server

4. Implement client functions and invoke service-side services

 Packageclient;Importjava.net.MalformedURLException;Importjava.rmi.Naming;Importjava.rmi.NotBoundException;Importjava.rmi.RemoteException;Importservice. Greetservice; Public classClient { Public Static voidMain (string[] args) {Try{String name= "Rmi://127.0.0.1:1098/greetservice"; Greetservice Greetservice=(Greetservice) naming.lookup (name); System.out.println (Greetservice.sayhello ("Thhhhhhh")); } Catch(malformedurlexception e) {e.printstacktrace (); } Catch(RemoteException e) {e.printstacktrace (); } Catch(notboundexception e) {e.printstacktrace (); }    }}

The post-compilation file directory structure is as follows

grs:bin grs$ tree.├──client│└──client.class└──service    ├──greetservice.class    └──greetserviceimpl_stub . class23

Client invoke Remote End method

Grs:bin grs$ Java--  

Resources:

RMI Introductory tutorial, DRY, Blog Park

Introduction to Java RMI

[Chapter 18] Distributed Computing:remote Deployment with RMI, Head first Java

RMI, the Java tutorials, Oracle

Java Remote Method Invocation, Wikipedia

Java RMI Getting Started case

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.