Use RMI to implement Java-based Distributed Computing

Source: Internet
Author: User
Use RMI to implement Java-based Distributed Computing-general Linux technology-Linux programming and kernel information. The following is a detailed description. The Remote Method Invocation (RMI) Framework of Java 2 Enterprise Edition (J2EE) allows you to create transparent and distributed services and applications. RMI-based applications are composed of Java objects that call each other while ignoring the other's location. In other words, a Java object can call a Java object method on another virtual machine. The whole process is the same as calling a Java object on the same virtual machine. Objects residing on different virtual machines can use the RMI Lookup service to obtain reference from each other, or use the object reference as a parameter or return value for method calls. Parameters and return values are encapsulated by RMI using Java's object serialization mechanism.
Remote Object and Interface
Java provides an interface with a fully qualified name of java. rmi. Remote. To participate in a remote session with another Java object, any object must implement this interface directly or indirectly. In particular, any object identified by the java. rmi. Remote interface implies that its method can be called from any other virtual machine. Objects that implement the java. rmi. Remote interface are usually called "Remote objects" and must be declared in the following way:
? Every method that supports remote calls must declare java. rmi. RemoteException in its throws clause.
? For a method that can be called remotely, each nonprimitive parameter or return value 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 non-parameter constructor, which can cause a java. rmi. RemoteException. This ensures that the object can be remotely constructed based on a serialization state.
The remote object must be exported to receive incoming remote method calls. Therefore, you usually need to extend java. rmi. server. UnicastRemoteObject or java. rmi. activation. Activatable. By extending any of the classes, remote objects can be automatically exported at creation.

The following interface definitions demonstrate 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 declaration implements the java. io. Serializable interface, String is a valid return type for remote methods.
The following code implements 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;
}
}
Related Article

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.