Java RMI Framework

Source: Internet
Author: User

Rmi(i.e.Remote Method InvokeRemote Method invocation). In theJava, as long as a classextendstheJava.rmi.Remoteinterface, you can become a remote object that exists on the server side for the client to access and provide some service. JavaDocDescription:The Remote interface is used to identify the interfaces whose methods can be called from a non-local virtual machine. Any remote object must implement this interface directly or indirectly. Only those methods specified in the remote interface ( the interface of the extended Java.rmi.Remote) are available for remote use . Note:extends the method in the Remote interface class or other interface if the declaration throws a remoteexception exception, it indicates that the method can be called by the client for remote access.   at the same time, the remote object must implement the Java.rmi.server.UniCastRemoteObject class so that when the client accesses the remote object, the remote object will take a copy of itself to Socket is transmitted to the client, at which point the client obtains the copy called the "stub", and the remote object that the server side itself already exists is called the "skeleton". In fact, the stub at this time is a client agent for communication with the server side, and the skeleton can also be considered a proxy on the server side, to receive the client's request after the call to the remote method to respond to the client's request.   The basic principle of the RMI framework is presumably that the proxy pattern is applied to encapsulate the details of the local stub communicating with the real remote object. a simple RMI application is given below , where the class diagram is as follows: where The IService interface is used to declare the services that the server must provide (that is, the service () method),theserviceimpl class is the specific service implementation class, and the server class is ultimately responsible for registering the server remote object, The skeleton proxy object exists on the server side to provide processing and response to the client's request. the source code for each class is as follows:IService Interface:
import java.rmi.Remote;
import java.rmi.RemoteException;
Public Interface IService extends Remote {
//Declare services that must be provided on the server side
String Service (string content) throws remoteexception;
}
Serviceimpl Implementation class:
ImportJava.rmi.RemoteException;
The //unicastremoteobject is used to export the remote object and obtains the stub that communicates with the remote object.
ImportJava.rmi.server.UnicastRemoteObject;

PublicclassServiceimplextendsUnicastRemoteObjectImplementsIService {

PrivateString name;

PublicServiceimpl (String name)throwsremoteexception {
This. name = name;
}
@Override
PublicString Service (string content) {
return"Server >>"+ content;
}
}
Server class:
/*
* The context interface represents a naming context that consists of a set of name-to-object bindings.
* It contains some methods for checking and updating these bindings.
*/

ImportJavax.naming.Context;
/*
* The InitialContext class is the initial context for performing a naming operation.
* The initial context implements the context interface and provides the starting point for resolving the name.
*/

ImportJavax.naming.InitialContext;
PublicclassServer {
PublicStaticvoidMain (string[] args) {
Try{
//Instantiate a remote service Serviceimpl object that implements the IService interface
IService SERVICE02 =NewServiceimpl ("SERVICE02");
//Initialize namespaces
Context NamingContext =NewInitialContext ();
//Bind the name to an object, that is, register the remote service object that has already been instantiated with the namespace
Namingcontext.rebind ("RMI://localhost/service02", SERVICE02);
} catch (Exception e) {
E.printstacktrace ();
}
System.out.println ("The server registered 1 Remote service objects to the named Table!") ");
}
}
Client class:
ImportJavax.naming.Context;
ImportJavax.naming.InitialContext;

PublicclassClient {
PublicStaticvoidMain (string[] args) {
String URL ="RMI://localhost/";
Try{
Context NamingContext =NewInitialContext ();
//retrieves the specified object. That is to find the server retransmitting corresponding service object stub
IService service02 = (iservice) namingcontext.lookup (URL
+"SERVICE02");
Class Stubclass = Service02.getclass ();
System.out.println (Service02 +"Yes"+ Stubclass.getname ()
+"for instance! ");
//Get the interface type that the background stub has implemented
Class[] interfaces = Stubclass.getinterfaces ();
for(Class c:interfaces) {
System.out.println (The stub class implements the+ c.getname () +"Interface!" ");
}
System.out.println (Service02.service ("Hello!" "));
}Catch(Exception e) {
E.printstacktrace ();
}
}
}
Save the above code in a directory, first run "start rmiregistry" to launch the JDK 's own registry program, which is used to save the Server The remote object is registered by the class and allows the remote client to request access, and then runs the server-side Server class, which is " start Java Server , the program registers the specific remote object with the registry, and finally runs the client program to find and obtain the server-side remote object stub, in order to use the stub object to communicate with the server, the command is " Java Client ". Note: the function of start in the command above is to reopen a DOS window. The results of the operation are as follows: in fact, the interaction Timing of each class in the entire simple RMI application is as follows:The above content is the study reference Sun Weichen Teacher's "Java Network programming Fine Solution" a book of the RMI chapter, plus their own personal understanding of the summary, hope to learn from each other and common progress!

This article is from the "Ant" Blog, please be sure to keep this source http://haolloyin.blog.51cto.com/1177454/332426

Java RMI Framework

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.