Get started with Java2 RMI

Source: Internet
Author: User
Getting started with Java2 Rmi by jdeveloper

RMI starts from java1.1. Rmi enables Java applications running on different JVMs (including different hosts) to communicate with each other.
That is, a Java application in one JVM can call the methods defined by an object (Remote Object) on another JVM.
Java RMI has important significance. RMI has important applications in Java Network Programming and advanced programming, such as EJB and Jini.
Java2 has made many enhancements and improvements to RMI, such as security and dynamic code download.
This article provides a simple example to illustrate some of the basic principles. This article focuses on actual development and real operation.
Environment, simulating the real development and running process of RMI.

1. implement remote interfaces to generate remote objects, stub and framework (skeleton)

The remote interface tells JVM that the object implementing this interface can be called remotely and what methods can be called.
This example defines sayhello (). Since remote calls involve network communication, RemoteException must be thrown for all these methods.
Remote interfaces and remote objects can be developed by a, and the remote interface (Hello) D is packaged and distributed to client developer B.

Create the F:/Server Directory and copy hello. Java and helloimpl. Java to the directory.

// Hello. Java
Package jdeveloper. RMI;

Import java. RMI. Remote;
Import java. RMI. RemoteException;

Public interface Hello extends remote {
String sayhello () throws RemoteException;
}

Generate a remote object.
// Helloimpl. Java
Package jdeveloper. RMI;

Import java. RMI. Naming;
Import java. RMI. RemoteException;
Import java. RMI. rmisecuritymanager;
Import java. RMI. server. unicastremoteobject;

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 and framework (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 delivers hello. jar to client-side developer B.

Store the stubs!
Stub is a dynamic download. The client communicates with the remote object framework (skeleton) through the stub
Just like operating a local object. In most cases, the downloadable code is stored in a directory on the HTTP server. This example is stored in http: // HJC/RMI.
HJC: Machine name, RMI: A Directory of HTTP. For standalone testing, you can put it in a directory such as F:/serverclasses.
This article provides the steps (policy file and BAT file) for both methods ).

2. implement client programs
// Helloclient. Java
Package jdeveloper. RMI;
Import java. RMI. rmisecuritymanager;
Import java. RMI. Naming;
Import java. RMI. RemoteException;
Import java. RMI. notboundexception;

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 helloclient. Java to the directory.
Create the F:/clientclasses directory and copy hello. jar to the directory.
F:
CD/client
Javac-classpath %; F:/clientclasses/Hello. jar-D. helloclient. Java

3. Run the program
Start DOS window
Set classpath =
Start rmiregistry

A. Run in standalone Mode
Create 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 to 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 to 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 in Network Mode
Create the 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 to 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 to 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

Rmihttpclient. Policy
Grant {
Permission java.net. socketpermission "*: 80", "Connect ";
Permission java.net. socketpermission "*: 1024-65535", "Connect ";
};

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.