Use RMI for distributed interaction and RMI for distributed interaction

Source: Internet
Author: User

Use RMI for distributed interaction and RMI for distributed interaction

Content Overview:

  • RMI example
  • RMI Architecture
  • RMI API

 

 

RMI example

Before learning about RMI, let's take an example:

// Service interface package com. fjn. java. rmi. quickstart. server; import java. rmi. remote; import java. rmi. remoteException; public interface Hello extends Remote {public String sayHello (String str) throws RemoteException;} // service implementation package com. fjn. java. rmi. quickstart. server; import java. io. serializable; import java. rmi. remoteException; public class HelloImpl implements Hello, Serializable {protected HelloImpl () throws RemoteException {super ();} private static final long serialVersionUID = response; @ Override public String sayHello (String str) {return "Hello," + str ;}// Server-side Publishing Service: package com. fjn. java. rmi. quickstart. server; import java.net. malformedURLException; import java. rmi. alreadyBoundException; import java. rmi. notBoundException; import java. rmi. remoteException; import java. rmi. registry. locateRegistry; import java. rmi. registry. registry; import java. rmi. server. secret; public class ServerTest {public static void main (String [] args) throws RemoteException, exceptions, InterruptedException, MalformedURLException, NotBoundException {// create Registry = LocateRegistry. createRegistry (9998); Hello stub = (Hello) UnicastRemoteObject. exportObject (new HelloImpl (), 0); // bind the ref to the registry. rebind ("Hello", stub); Hello stub2 = (Hello) registry. lookup ("Hello"); System. out. println (stub2); Thread. sleep (1000*60*60 );}}

Execution result:

Hello, zhang sanHello, Zhang san
RMI Architecture

 

The figure above shows the architecture of RMI. Design Description: The Server only needs to register a name (unique) for a remote object from a Registry ), then you can wait for the client to use it. Before the client calls a remote method, go to the Registry look up (by name), find the corresponding object (by referencing the object), and then call the method on the client. Because the client obtains the reference of a remote object, it is called directly.

In some cases, the client obtains the bytecode of this Class from the server, and the server also obtains the bytecode of some classes from the client. The premise is that the other party has a Web Server. URL protocol (e.g., HTTP, FTP, file, etc .).

 

RMI APIRemote

TheRemoteInterface serves to identify interfaces whose methods may be invoked from a non-local virtual machine. any object that is a remote object must directly or indirectly implement this interface. only those methods specified in a "remote interface", an interface that extendsjava.rmi.RemoteAre available remotely.

There is no method in this interface. This interface is only a tag to mark interfaces that can be used locally. That is to say, if this interface can be used by the RMI client, it must be extends or Remote sub-interface.

The objects that implement the Remote interface are called Remote objects. RMI is used in distributed programs, so remote objects are also called distributed objects.

 

Remote Interface Design Principles:

1) must inherit the Remote interface

2) The methods declared in the remote interface must meet the following requirements:

A: The method must throw java. rmi. RemoteException (IOException, Exception is also acceptable, because these two exceptions are the parent class of RemoteException ).

B: In the Declaration of the remote method: If the remote object contains a parameter or return value, you must use the remote interface itself.

3) if the remote interface inherits other non-remote interfaces, as long as the remote method meets the requirements in 2. As a Remote interface, to inherit the Remote interface, the methods directly declared in this Remote interface must meet the requirements in 2. Methods inherited from non-remote interfaces must also meet the requirements in 2.

4) the Serializable interface must be implemented to implement the interface.

 

RemoteException

To ensure the normal operation of the program during Remote calls, a RemoteException or its parent class: IOException and Exception must be thrown for each remote method.

 

When Will RemoteException be thrown?

1) Protocol Error

2) When the communication fails (the Server rejects the request or the connection fails)

3) problems with parameters or interfaces during the marshall and unmarshall Processes

 

Registry, LocateRegistry

 

We know that Registry is a Registry. Used to register the provided remote service.

The Registry class provides storage and remote object retrieval (during storage, the reference of the remote object is bound to a name, and the name is unique, so you can obtain it by name ).

 

The following methods are provided:

 

Bind, unbind, and rebind are used to bind and unbind a remote object from a specified name.

List: Used to list the names of bound remote objects. The returned result is an array.

In addition, it is very important to store the stub object in the Registry. The object is on the RMI Server.

When the client finds (loopup) through Naming or Registry, all objects are stub. Then, use the serialization and deserialization functions of Stub to communicate with the Server.

 

Relationship between LocateRegistry and Registry

LocateRegistry is used to locate the location of the Registry on the network. Knowing the location of the Registry on the network, you can obtain the Registry object. Based on the Registry object, you can find the remote object to be used, find the remote object and you can call the remote method.

 

Naming

 

The Naming class provides some methods to store and obtain reference of remote objects (stored in the Registry.

The following methods are provided:

 

 

The provided method has a name parameter, which is of the string type. It has a standard format (it is a URL, but the schema part in the URL is missing, and the schema is rmi ). Format:

// Host: port/name or rmi: // host: port/name

 

Host is the host of the Registry to be located, and the port is also.

Naming is a tool class that provides static methods, but the methods are indeed the same as those in Registry. So far, we have learned two ways to obtain and bind remote objects.

That is to say, Registry is available on both the client and server. Binding is used for the server and obtaining is used for the client.

 

Method 1: Use Registry to bind or obtain the stub of a remote object. Method 2: Use Naming directly to bind or obtain the stub of a remote object.

 

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.