Using RMI to implement distributed computing based on Java

Source: Internet
Author: User
Tags object serialization valid

The Java 2 Enterprise Edition (EE) Remote method call (the remote methods Invocation,rmi) framework allows you to create transparent, distributed services and applications. RMI-based applications consist of Java objects that invoke each other and ignore each other's location. In other words, a Java object can call a method of a Java object on another virtual machine, and the whole process is no different from calling a Java object on the same virtual machine. Objects residing on different virtual machines to get references to each other, you can use the RMI Lookup service, or take an object reference as a parameter or return value of a method call. Parameters and return values are marshaled by RMI by using the Java object Serialization mechanism.

Remote Objects and interfaces

Java provides an interface with a fully qualified name of Java.rmi.Remote. Any object that wants to participate in a remote session with another Java object must implement the interface directly or indirectly. It is particularly noteworthy that any object identified by the Java.rmi.Remote interface implies that its methods can be invoked from any other virtual machine. An object that implements the Java.rmi.Remote interface is often called a "remote object" and must declare its methods in the following ways:

Each method that supports a remote invocation must declare java.rmi.RemoteException in its throws clause.

For a remotely callable method, each of its nonprimitive parameters or return values must be declared directly or indirectly to implement the Java.io.Serializable interface.

In addition to implementing the Java.rmi.Remote interface and correctly declaring any remote method, the remote object must provide a parameterless constructor that can throw a java.rmi.RemoteException exception. This ensures that objects can be constructed remotely based on a serialized state.

The remote object must be exported to receive incoming remote method calls. To do this, you usually need to extend java.rmi.server.UnicastRemoteObject or java.rmi.activation.Activatable. By extending any of these classes, the remote object can be automatically exported when it is created.

The following interface definitions show the most typical usage of the Java.rmi.Remote interface:

import java.rmi.Remote;
import java.rmi.RemoteException;
public interface TimeKeeper extends Remote
{
public String currentDate() throws RemoteException;
public String currentTime() throws RemoteException;
}

Because the string class is declared to implement the Java.io.Serializable interface, string is a valid return type for the remote method.

The following code shows how to implement the Timekeeper interface to define a valid remote object:

import java.rmi.RemoteException;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class TimeKeeperImpl implements TimeKeeper
{
public TimeKeeperImpl()
throws RemoteException
{
}
public String currentDate() throws RemoteException
{
Calendar cal = new GregorianCalendar();
String retVal = (cal.get(Calendar.MONTH) + "/" +
cal.get(Calendar.DAY_OF_MONTH) + "/" +
cal.get(Calendar.YEAR));
return retVal;
}
public String currentTime() throws RemoteException
{
Calendar cal = new GregorianCalendar();
String retVal = (cal.get(Calendar.HOUR_OF_DAY) + ":" +
cal.get(Calendar.MINUTE) + ":" +
cal.get(Calendar.SECOND));
return retVal;
}
}

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.