Javarmi remote method call

xiaoxiao2021-03-06  113

From: http://www.matrix.org.cn/Article/229.html For the code on other machines over the network, the traditional method is not only difficult to learn, but also error. The best way to solve this problem is: Some objects are just in another machine, we can send a message and get the return result, just like your own machine. Java Remote Method Call (RMI) Features Enables programs running on the client to invoke objects on the remote server. Remote Method Call Features Enables Java programmers to distribute operations in a network environment. Let's introduce the necessary steps to create your own RMI object. I. Remote Interface Concept: RMI has strong dependence on the interface. When you need to create a remote object, we hide the implementation details of the grassroots by passing an interface. So the customer gets a handle of the remote object exactly the same local root code, and the latter is responsible for communicating over the network. But we don't care about these things, send messages through your own interface handle. When you create a remote interface, you must follow the following rules: 1) The remote interface must be a public attribute (not "package access"; that is, he can't be "friendly"). Otherwise, once the customer tries to load a remote object that implements a remote interface, it will get an error. 2) The remote interface must expand the interface java.rmi.remote. 3) In addition to violations related to the application itself, each method in the remote interface must declare java.rmi.RemoteException.4) as a remote object passed as a parameter or return value (whether directly, Or embedding in the local object) must be declared as a remote interface and cannot be declared as a category. The following example is a remote interface, // PerfectTimeI.java // The PerfectTime remote interface package test; import java.rmi *;. Public interface PerfectTimeI extends Remote {long getPerfectTime (throws RemoteException);} on its surface similar to other interface It is only extended to Remote, and all methods will "throw" removeexception. Interfaces and methods are public. Compile PerfectTimei.java, generate PerfectTimei.class (Test is a package, pay attention to the path) G: / RMI> Javac test / perfectTIMEI.JAVA II, Remote Interface Implementation: The server must contain an extended UnicastRemoteObject class and implement remote interface . This class can also contain additional methods, but customers can only use methods in remote interfaces. Because the customer is a handle pointing to the interface, not which class it. The component must be defined for the remote object, even if only one default member device is defined, use it to call the underlying type member. It must be explicitly written because it must "throw" RemoteException violation. The fact that the remote interface PerfectTime is listed below: he represents the exact timing service //perfecttime.java // The Implectation of the PerfectTime Remote Object Package Test; import java.rmi. *; Import java.rmi .registry. *; import java.rmi.server. *; public class perfectTime Extends UnicastRemoteObject IMPLEments PerfectTimei {// Default member, but also "throw" RemoteException violation.

public PerfectTime () throws RemoteException {super ();} public long getPerfectTime () throws RemoteException {return System.currentTimeMillis ();} public static void main (String [] args) {/ * create and install a security manager, so It supports RMI. As part of the Java development package, the only one of RMI is the only one is RMISecurityManager. * / System.setSecurityManager (New RMISecurityManager ()); try {/ * Create one or more instances of the remote object, below is PerfectTime object * / PerfectTime Pt ​​= New PerfectTime (); / * Register at least one remote object to the RMI Remote Object Registration. A remote object has a method to generate a handle to another remote object, so that the customer visits once in the registry to get the first remote object. * / Naming.bind ("PerfectTime", PT); System. Out.println ("Ready to Do Time");} catch (Exception E) {E.PrintStackTrace ();}}} Compile PerfectTime.java, generate PerfectTime.class (TEST is package, pay attention to the path when compiling) G: / RMI> Javac Test / PerfectTime.java 3, create roots and dry: Create RemoteObject's backbone and framework. To do this, you can use the RMIC compiler, the RMIC compiler generates the stub and skeleton of the remote object. STUB is a remote object at the client's agent, which passes the RMI call to the server-side skeleton (the latter is responsible for passing the call to the actual remote method) as follows: g: / rmi> RMIC -D G: / RMI Test.PerfectTime executes this command, if the RMIC is successfully run, there will be more new classes in the TEST directory: PerfectTime_stub.class performttime_skel.class They correspond to the root (STUB) and dryTON. IV, use Remote objects: RMI all the purpose is to simplify the use of remote interface objects. The only extra thing to do in our client program is to find the remote interface from the server. Here is a program written in Java: sending messages to objects: //DisplayPerfectTime.java // Users remote object PerfectTime package test; import java.rmi *; import java.rmi.registry *; public class DisplayPerfectTime {/ **.. * DisplayPerfectTime constructor annotation.

转载请注明原文地址:https://www.9cbs.com/read-102871.html

New Post(0)