Javarmi entry

xiaoxiao2021-03-06  105

(Source: http://ybwen.home.chinaren.com)

In order to perform code on other machines through the network, traditional methods are not only difficult to learn, but also error. The best way to solve this problem is: Some objects are just awkward.   强 苑 苑 ⑺ ⑺ 鸵 跸   ⒒ ⒒ 竦 祷 峁 峁 峁  裎 裎 裎 裎 裎 谧 谧 谧 谧 谧 谧 谧 谧Lying? Span language = "en-us"> Java Remote Method Call (RMI) Features Make programs running on the client to call 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 (no "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 the violations related to the application itself, each method in the remote interface must declare java.rmi.RemoteException in its own throws clause.

4) A remote object (whether it is direct or in direct or local objects) is required as a parameter or returned value, it must be declared as a remote interface.

Below is an example of a remote interface,

//Perfecttimei.java

// the PerfectTime Remote Interface

Package test;

Import java.rmi. *;

Public interface perfecttimei extends remote {

Long getPerfectTime () throws remoteException;

}

It is similar to other interfaces on its surface, just extension of Remote, and all methods will "throw" RemoteException. Interfaces and methods are PUBLIC.

Compile PerfectTimei.java, generate PerfectTimei.class (TEST is package, pay attention to the path when compiling)

G: / RMI> Javac test / perfecttimei.java

Second, the implementation of the remote interface:

The server must contain an extended UnicastRemoteObject class and implement a 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 precise timing service

//Perfecttime.java

// The importation of the perfecttime remote Object

Package test;

Import java.net. *;

Import java.rmi. *;

Import Java.rmi.Registry. *;

Import java.rmi.server. *;

Public Class PerfectTime Extends UnicastRemoteObject IMPLEments PerfectTimei

{

// The default member is also "throwing" 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 to support RMI. As part of the Java development package, the only one for RMICurityManager. * /

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. The method of a remote object has a handle 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 a package, pay attention to the path when compiling)

G: / RMI> Javac Test / PerfectTime.java

Third, create roots and dry:

Create the backbone and framework of RemoteObject. 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 servelet's skeleton, which is responsible for passing the call to the actual remote method as follows:

G: / RMI> RMIC -D G: / RMI Test.PerfectTime

Execute this command,

If the RMIC is successfully run, there will be two new classes in the Test directory:

PerfectTime_stub.class

PerfectTime_skel.class

They correspond to the root (STUB) and dryton.

Fourth, use the remote object:

All of RMI 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. The following is the written Java program: send the message to the object:

//Displayperfecttime.java

// Users Remote Object PerfectTime

Package test;

Import java.rmi. *;

Import Java.rmi.Registry. *;

Public class displayperfecttime {

/ *** DISPLAYPERFECTTIME constructor annotation. * /

Public DisplayPerfectTime () {

Super ();

}

Public static void main (String [] args) {

System.SetSecurityManager (New RMISecurityManager ());

Try {

PerfectTIMEI T = (PerfectTimei) Naming.lookup ("PerfectTime");

For (int i = 0; i <10; i ) {system.out.println ("PerfectTime: T. GetPerfectTime ());

}

} catch (exception e) {

E.PrintStackTrace ();

}

}

}

DISPLAYPERFECTTIME.JAVA compile.

G: / RMI> Javac Test / DisplayPerfectTime.java

5. Start registration and run the code:

Before running the PerfectTime class and the DisplayPectTime class, the user must first start the RMI registration (Registry) program on a computer to host PerfectTime, even if it is the same machine to run the PerfectTime's computer and running DisplayPerfectTime, this step is also necessary. The name of the registry server is RMIREGISTRY. In the 32-bit Windows environment, you can use: Start RMIREGISTRY makes it run in the background. Then open two different processes to run the Server end and the Client side: Start the registry server:

G: / RMI> Start RMIREGISTRY

Bind PerfectTime to register, run the server: Under Windows, enter the following command, start the PerfectTime program in the background:

G: / RMI> Java Test.PerfectTime

Ready to Do Time

Run the client program:

G: / RMI> Java Test.displayPerfectTime

PerfectTime: 961722589649

PerfectTime: 961722589669

PerfectTime: 961722589679

PerfectTime: 961722589679

PerfectTime: 961722589689

PerfectTime: 961722589689

PerfectTime: 961722589689

PerfectTime: 961722589699

PerfectTime: 961722589699

PerfectTime: 961722589699

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

New Post(0)