RMI introduction !!

Source: Internet
Author: User

The remote method call (RMI) mechanism can further expand the object-oriented thinking, because the objects you can call can be not only on the local machine, but also on other hosts. This article briefly introduces the rmi programming method.

First, we will introduce some simple rmi concepts.

1. servers and customers: in rmi, if an object is called remotely, this object is called a client object, while a remote object is called a server object.

2. Create a server program for the server object: This program is used to create a Server Object and register this object so that the customer can access the server object through the registered name.

3. interfaces allow the client to understand what the server can do. More specifically, it lists all the methods that can be executed on the server. The client program must be able to find this class; otherwise, it cannot call server functions.

4. The client pile (stub). Some books translate it into code stubs, which provide a pile to the client program. The pile is bound with the server object. When the client program needs to call a remote object, the pile is downloaded to the client (if the client has this class, it does not need to be downloaded ). Then the customer can call a remote method just like calling a local method.

This client post is used to encode and transmit client requests to the server. After the server executes this call, the results are returned to the client post for decoding, send the decoded result to the client program. For a client programmer, he does not need to know the specific process.

The client node does not need to be written by yourself. The generation method is described later. It implements the aforementioned interface ).

The following is an example to illustrate the compilation process.

1. Compile the server interface: This step is the most important part, because the interface is a key part for connecting the client to the server. In this example, the interface is simple and the code is as follows:

Import java. rmi .*;
Public interface Product extends Remote
{
String getDescription () throws RemoteException;
}

It should be noted that the Remote interface of the Remote object must be extended (extend) Java. rmi package. At the same time, all methods in the interface must declare that the RemoteException is thrown. This is because the network connection is not reliable and remote method calls may fail. If no exception is declared, the application cannot end after the remote method call fails.
2. Compile the Server Object:

Java has a server class that can be used directly-UniCastRemoteObject. It exists in the Java. rmi. server package. We can directly extend this class to implement the aforementioned interface. In this way, the server can meet our needs.

Import java. rmi. server .*;
Import java. rmi .*;
Public class ProductImpl extends UnicastRemoteObject implements Product
{
Public ProductImpl (String name) throws RemoteException
{
Desc = name;
}
Public String getDescription () throws RemoteException
{
Return "This is" + Desc + "product ";
}
Private String Desc;
}

We can see that the implementation of the rmi server is no different from other method code.

3. Compile the server program for creating server objects:

Import java. rmi .*;
Public class ProductServer
{
Public static void main (String [] args)
{
Try
{
System. out. println ("Constructin Server implementations ....");
ProductImpl p1 = new ProductImpl ("toaster ");
ProductImpl p2 = new ProductImpl ("microwave ");
System. out. println ("Binding server implementations to registry ");
Naming. rebind ("toaster", p1 );
Naming. rebind ("microwave", p2 );
System. out. println ("waiting for clients ...");
} Catch (Exception e)
{
System. out. println ("Error" + e );
}
}
}

The Code shows that the server first creates two server objects. Then, use the Naming. rebind () method to associate the object with a name (BIND. This is the name used by the client to find the server object. Naming is a class in the java. rmi package. This class is used to create a naming mechanism for finding objects. It can be used to locate the objects bound to a specific name.
4. Compile the client code:

Import java. rmi .*;
Import java. rmi. server .*;
Public class ProductClient
{
Public static void main (String [] args)
{
System. out. println ("begin to invoke remote method ");
System. setSecurityManager (new RMISecurityManager ());
String url = "rmi: // 91.1.1.119: 1099 /";
Try
{
File: // search for remote objects
System. out. println ("1 ");
Product c1 = (Product) Naming. lookup (url + "toaster ");
Product c2 = (Product) Naming. lookup (url + "microwave ");
File: // call a remote method
System. out. println ("2 ");
System. out. println (c1.getDescription ());
System. out. println ("3 ");
System. out. println (c2.getDescription ());
} Catch (Exception ex)
{
System. out. println ("error" + ex );
}
}
}

In this Code, a string url is defined first. This string stores the protocol and address information for finding the remote server object. In rmi, the protocol used is rmi and the port number is 1099. In this example, my server object is stored on the host whose IP address is 91.1.1.119. Therefore, the value of this string is rmi: // 91.1.1.119: 1099 /.

Then, use the Naming. lookup () method to find the remote object. The parameter is the location information of the server and the name bound to the server object.

 

 

It should be noted that the lookup method is used to get the client pile downloaded to the client instead of the reference of the server object itself. However, this method produces the Object type. To use this Object, you must convert its type to the interface type implemented by the server.

Then, you can call a remote method just like calling a local method. In this example, the remote method is getDescription ().

Because this code is used to operate remote objects, it is put into a try... Catch Block to capture exceptions in the remote call process.
Finally, deploy the server and client to the machine.

1. compile all class files into class files. Then use

Rmic ProductImpl

You can generate a client pile named ProductImpl_Stub.class.

2. Copy the client code and interface code to the client machine.

3. Run the rmiregistry program and start the registration system so that the server can be registered on the machine for the customer to call.

4. Start the http service. Place the interface class and client pile class on the http server so that the customer can download them. Assume that the download directories of these two files are http: // 91.1.1.119/download/

5. Use start java-Djava. rmi. server. codebase = http: // 91.1.1.119/download/ProductServer

Run the program that creates the server object.

-Djava. rmi. server. codebase = http: // 91.1.1.119/download/specifies the address of the client program to download the client program.

6. Because rmi has security restrictions, you must create a policy file on the client. Assume the name is client. policy.

The file content is

Grant
{
Permission java.net. SocketPermission "91.1.1.119: 1024-65535", "connect ";
Permission java.net. SocketPermission "91.1.1.119: 80", "connect ";
};

Use start java-Djava. security. policy = client. policy ProductClient starts the client, and the client can connect port 80 (http port) and Port 1024-65535 (including the default rmi port 1099 ). Then you can see the execution result of the program.

The above is the basic process of remote method calling using rmi.

However, it should be noted that rmi has a large limit that it can only be used between objects written in java. If communication is required between objects written in different languages, so we need the help of CORBA.

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.