RMI starts with Java1.1, and RMI enables Java applications that run on different JVMs, including different hosts, to talk to each other.
That is, a Java application in a JVM can invoke a method defined by an object on another JVM (a remote object).
Java RMI is of great significance. RMI has important applications in Java Network programming and advanced programming, such as EJB, Jini, and so on.
JAVA2 has done a lot of enhancements and improvements to RMI, such as security, dynamic code downloads, and more.
In this paper, a simple example is given to illustrate some of the basic principles. The feature of this paper is to pay attention to the actual development and real operation
Environment to simulate the real development and operation of RMI.
Implementing a remote interface, the remote interface tells the JVM that the object that implements the interface can be invoked remotely and which methods can be invoked.
The SayHello () is defined in this example. Because remote calls involve network traffic, these methods throw remoteexception.
remote interfaces and remote objects can be developed by a and bundled with remote interface (Hello) d to client side developer B.
Create F:\server directories and copy Hello.java and Helloimpl.java to the directory.
public class Helloimpl extends UnicastRemoteObject
Implements Hello {
Public Helloimpl () throws RemoteException {
Super ();
}
Public String SayHello () {
Return "Hello world!";
}
public static void Main (String args[]) {
Create and install a security manager
if (system.getsecuritymanager () = null) {
System.setsecuritymanager (New Rmisecuritymanager ());
}
try {
Hello obj = new Helloimpl ();
Bind This object instance to the name "HelloServer"
Naming.rebind ("HelloServer", obj);
System.out.println ("HelloServer Bound in Registry");
catch (Exception e) {
System.out.println ("Helloimpl err:" + e.getmessage ());
E.printstacktrace ();
}
}
}
Stub (stub) and frame (skeleton)
F:
CD \server
Javac-d. Hello.java
Javac-d. Helloimpl.java
Rmic-d. Jdeveloper.rmi.HelloImpl
Jar CVF Hello.jar Jdeveloper\rmi\hello.class distribute Hello.jar to client side developer B.
Stub (stub) Storage!
Stub is dynamically downloaded. The client communicates through the stub and the framework (skeleton) of the remote object to the client
Speaking is like manipulating a local object. In most cases, the downloadable code is placed in a directory on the HTTP server. This example is put under Http://hjc/rmi.
HJC: Machine name, a directory of rmi:http. If it is a stand-alone test, it can be placed in a directory such as F:\serverclasses.
This article will give you the steps to run (policy file and BAT file) for both of these methods.
public class Helloclient {
public static void Main (String args[]) throws exception{
System.setsecuritymanager (New Rmisecuritymanager ());
Hello remoteobj = (hello) naming.lookup ("//" + Args[0] + "/helloserver");
System.out.println (Remoteobj.sayhello ());
}
}
Create the F:\client directory and copy the Helloclient.java to the directory.
Create the F:\clientclasses directory and copy the Hello.jar to the directory.
F:
CD \client
Javac-classpath%classpath%;f:\clientclasses\hello.jar-d. Helloclient.java
3. Run the program
Start DOS window
Set classpath=
Start Rmiregistry
A. operate on a stand-alone basis
Set up F:\serverclasses\jdeveloper\rmi directory
F:
CD f:\serverclasses
Copy F:\server\hello.jar.
Copy F:\server\jdeveloper\rmi\HelloImpl_Stub.class f:\serverclasses\jdeveloper\rmi\
Jar XVF Hello.jar
Start a new DOS window
Put Starthelloserver.bat and Rmiserver.policy in F:\server\.
Run Starthelloserver
Starthelloserver.bat
SET Cp=f:\server;f:\serverclasses\hello.jar
echo using classpath:%cp%
Java-classpath%cp%-djava.rmi.server.codebase=file:/f:\serverclasses/
-djava.rmi.server.hostname=hjc-djava.security.policy=rmiserver.policy Jdeveloper.rmi.HelloImpl
Rmiserver.policy
Grant {
Permission Java.net.SocketPermission "*:1024-65535",
"Connect";
};
Start a new DOS window
Put Starthelloclient.bat and Rmiclient.policy in f:\client\.
Run Starthelloclient
Starthelloclient.bat
@echo off
Set Cp=f:\client;f:\clientclasses\hello.jar
echo using Classpath%cp%
@echo on
Java-classpath%cp%-djava.rmi.server.codebase=file:/f:\serverclasses/
-djava.security.policy=rmiclient.policy jdeveloper.rmi.HelloClient%1
Rmiclient.policy
Grant {
Permission Java.net.SocketPermission "*:1024-65535",
"Connect";
Permission Java.io.FilePermission
"f:\\serverclasses\\-", "read";
};
B. Run on a network basis
Set up Apache_path\htdocs\rmi\jdeveloper\rmi directory
CD Apache_path\htdocs\rmi
Copy F:\server\hello.jar.
Copy F:\server\jdeveloper\rmi\HelloImpl_Stub.class Apache_path\htdocs\rmi\jdeveloper\rmi
Jar XVF Hello.jar
Put Starthellohttpserver.bat and Rmihttpserver.policy in F:\server\.
Start a new DOS window
Run Starthellohttpserver
Starthellohttpserver.bat
SET Cp=f:\server;f:\serverclasses\hello.jar
echo using classpath:%cp%
Java-classpath%cp%-djava.rmi.server.codebase=http://hjc/rmi/
-djava.security.policy=rmihttpserver.policy Jdeveloper.rmi.HelloImpl
Rmihttpserver.policy
Grant {
Permission Java.net.SocketPermission "*:1099", "Accept, connect, listen, resolve";
Permission Java.net.SocketPermission "*:80", "Accept, connect, listen, resolve";
};
Start a new DOS window
Put Starthellohttpclient.bat and Rmihttpclient.policy in f:\client\.
Run Starthellohttpclient
Starthellohttpclient.bat
@echo off
Set Cp=f:\client;f:\clientclasses\hello.jar
echo using Classpath%cp%
@echo on
Java-classpath%cp%-djava.rmi.server.codebase=http://hjc/rmi/
-djava.security.policy=rmihttpclient.policy jdeveloper.rmi.HelloClient%1
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.