Remote method calls with RMI

Source: Internet
Author: User
Tags extend stub

The remote method call (RMI) mechanism can further extend the object-oriented idea because you can invoke objects not only on this machine, but also on other hosts. This article simply introduces the RMI programming method.

First, introduce some simple RMI concepts.

1, servers and customers: In RMI, if there is an object that makes a remote method call, the object is called the client object, and the remote object is called the server object.

2, create server Object Server program: This program is used to create server objects, register this object so that customers can access the server object by registered name.

3, interface (interface), the interface allows the client to understand the server can do the work. 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 perform a call to the server function.

4, the Customer pile (stub), some books translated into a code stub, it gives the client program to provide a pile, the pile "tied" server objects. When a client needs to invoke a remote object, the pile is downloaded to the client (if the client has this class, it does not need to be downloaded). The client can then invoke the remote method as if it were a local method.

The customer's role is to the client's request to the server to encode, carry out transmission, the server to perform this call will return the results to the customer pile, customer pile decoding, the decoded results will be transferred to the client program. For programmers who write clients, he doesn't need to know the specifics of the process.

The client pile does not need to be written by itself, and its generation method is explained later. It implements the aforementioned interface (interface).

Here is an example to illustrate the process of writing.

1, write the server interface: This step is the most important part, because the interface is a key part of connecting the client with 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 here that the interface of the remote object must be extended (extend) Java.rmi the packet's remote interface. All of the methods in the interface are declared to throw remoteexception exceptions. This is because remote method calls are likely to fail due to the unreliability of network connections. If you do not declare an exception, the application will not end after the remote method call fails.

2, Write server objects:

Java has a server class--unicastremoteobject that can be used directly. It exists in the Java.rmi.server package. We can extend this class directly so that it implements the interface described above. This will enable the server to 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;
}

As you can see, the implementation of the RMI server is no different from any other method code.

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.