RMI's first experience!

xiaoxiao2021-03-06  40

RMI (Remote Method Invocation), meaning in Chinese: Remote function call. Allows the Java program to call the remote object, just as the method of using the local object.

RMI is a bit as follows:

1: Object-oriented, RMI can pass the full object as a parameter or return, not just a basic data type.

2: Safety, RMI uses Java's built-in security management mechanism to ensure the security of the download procedure.

3: Simple and easy to write, compared with the Socket mechanism, RMI blocks the implementation details of the underlying, and does not need to consider the transfer protocol when writing programs. At the same time, RMI can communicate with HTTP, so that remote communication can be acceded to the firewall and proxy server.

4: Cross the platform. RMI is part of Java, with the advantage of "Writing once, run everywhere".

5: Parallel calculation, RMI uses multi-threaded process, and servers utilize Java threads parallel to process client requests.

Client Server

Stubsskelton

Remote Reference Layer

TRANSPORT

In RMI, calling a remote object is called a client object (the remote object is called server object, and two special type objects, stub) and framework (Skelton) The stub is a client object representing the remote object, which has the same interface or method list. When the client calls the remote object, it is actually completed by the corresponding stub root object agent, and the stub is handled through the object. The stub will forward the request to the remote object through the RMI infrastructure, and finally there is a remote object execution request. At the server side, the frame object handles all the details of "distant", so actually remote objects don't have to worry about these details. That is to say, it is entirely possible to write a remote object like a local object. The framework is separated from the RMI infrastructure. At this point, we should have a preliminary understanding of the basic working principle of RMI, so let's start writing an actual RMI app! The writing of RMI applications is divided into a few steps! Step 1: Write a remote interface. To generate a remote interface, you must follow the following rules: 1. The distal interface must be declared as public, otherwise the client receives an error message when trying to "implement a remote interface" remote object. 2. The remote object must inherit the interface of java.rmi.Remote. 3. The function in each remote interface, in addition to the custom abnormality, Java.rmi.RemoteException.4 must be thrown. As the distal object of the extract or the return value, it must be declared as a remote interface, not the actual class. Now let's start writing a remote interface that gets the system time. As follows: package net.xiaobo.remoteserver; import java.util. *; Import java.rmi.remote; import java.rmi.RemoteException;

Public interface Helloremote Extends Remote {Public Date GetTime () THROWS RemoteException;} The above interface is in line with the above provisions. Step 2: Implement the distal interface. The server must contain a Class inherited from UnicastRemoteObject and implement a remote interface. This Class can also have additional functions, but only the functions defined in the remote interface can be called. As follows: package net.xiaobo.remotserver;

import java.rmi *;. import java.rmi.server *;. import java.rmi.registry *;. import java.util *;. public class HelloRemoteImp extends UnicastRemoteObject implements HelloRemote {public Date getTime () throws RemoteException {return new Date ();} public HelloRemoteImp () throws RemoteException {} public static void main (String args []) throws Exception {System.setSecurityManager (new RMISecurityManager ()); HelloRemoteImp hri = new HelloRemoteImp ();

Naming.bind ("/ helloremoteimp", hri; system.out.println ("succeed in binding, county to do time");}} The third step: (can be no, my own habits, everyone can Configure the permissions file in% java_home / jre / lib / security / java.policy), with the above file in the same directory. List Policy./* * Only Grant Permissions to the Local Class Path (The Current Directory). * / Grant CodeBase "File :." {

Permission java.net.socketpermission "*: 1024-", "connect, accept";

}; Step 4: Run the above file, the command is as follows: javac -d. Helloremote.java This command generates a directory to net / xiaobo / transserver / helloremote.class javac -d. HelloreMoteimp.java This command generates a directory to NET / XIAOBO / . remoteserver / HelloRemoteImp.class rmic -d HelloRemoteImp this command produces directory net / xiaobo / remoteserver / HelloRemoteImp_Stub.class net / xiaobo / remoteserver / HelloRemoteImp_Skelton.class step Five: start the server and set the registration thin bind. The command is as follows: RMIREGISTRY Java -djava.security.policy = policy net.xiaobo.remoteServer.HelloreMoteimp This command -Djava.security.policy = policy Specifies the Java Interpretation Program to set permissions with the specified Policy file, at the same time Start the server and make the binding of the remote object. That is, Naming.Bind ("/ HelloremoteImp", HRI) is executed; the code is binding the service name HelloremoteImp and object HRI. Easily query remote objects at the client. If the run is successful, a prompt will appear: Succeed in Binding, Now Ready to Do Time Step 6: Write the client code for testing. The code is as follows: package net.xiaobo.remoteclient; import net.xiaobo.remoteserver. *; Import java.rmi. *; Import java.rmi.registry. *;

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

New Post(0)