Java RMI Remote Invoke demo

Source: Internet
Author: User

Java remote method Invocation, Java RMI (Java remote methods invocation), an application programming interface for implementing remote procedure calls. 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. Java RMI relies heavily on interfaces. When you need to create a remote object, the programmer hides the underlying implementation details by passing an interface. The client gets a handle to the remote object that is directly connected to the local root code, which is responsible for communicating through the network. In this way, programmers simply care about how to send messages through their own interface handles.

/* New Interface on server: */public interface Rmitestinterface extends Remote{public String gettest () throws remoteexception;}

  

/* Implementation of the interface: */public class Rmitestimpl implements Rmitestinterface{public Rmitestimpl () throws remoteexception{}@ Overridepublic String Gettest () throws Remoteexception{return "Hello MM";} public static void Main (string[] args) throws RemoteException {Rmitestimpl t=new rmitestimpl (); Rmitestinterface tt= (Rmitestinterface) unicastremoteobject.exportobject (t, 0); Registry Registry=locateregistry.createregistry (2001); Registry.rebind ("Test", TT); SYSTEM.OUT.PRINTLN ("Server is Start");}}

  

/*client end of the main program */public class Client {public static void main (string[] args) {try {Registry registry= Locateregistry.getregistry ("localhost", 2001); Rmitestinterface t= (rmitestinterface) registry.lookup ("test"); System.out.println ("Client:" +t.gettest ());} catch (RemoteException e) {e.printstacktrace ();} catch (Notboundexception e) {e.printstacktrace ();}}}

  

RMI Remote Invoke demo

in Java

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.