In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use RMI to achieve Java-based distributed computing. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
The Java 2 Enterprise Edition (J2EE) remote method invocation (Remote Method Invocation,RMI) framework allows you to create transparent, distributed services and applications. RMI-based applications are made up of Java objects that call each other while ignoring each other's location. In other words, a Java object can call the method of a Java object on another virtual machine, and the whole process is the same as calling a Java object on the same virtual machine. Objects that reside on different virtual machines can use RMI's lookup service or receive the object reference as a parameter or return value of a method call in order to get a reference to each other. Parameters and return values are marshaled by RMI using Java's 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. In particular, it is important to note that any object identified by the java.rmi.Remote interface implies that its methods can be called from any other virtual machine. An object that implements the java.rmi.Remote interface is often referred to as a "remote object" and its methods must be declared in the following ways:
? Every method that supports remote invocation 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 as implementing the java.io.Serializable interface.
In addition to implementing the java.rmi.Remote interface and properly declaring any remote methods, the remote object must provide a parameterless constructor that throws 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 at creation time.
The following interface definition shows the most typical use 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 remote methods.
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
}
}
This is the end of this article on "how to use RMI to achieve Java-based distributed computing". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.