Java2 RMI Getting Started

xiaoxiao2021-03-06  19

Java2 RMI entry Author: jdeveloper RMI from Java1.1 start, RMI makes run on different JVM (including different hosts) Java applications can call on each other. That is, the Java application in a JVM can call the method defined by the 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, JINI, etc. Java2 has made a lot of enhancements and improvements in RMI, such as security, dynamic code download, etc. This article gives a simplest example to illustrate some of the basic principles. This paper features a combination of actual development and real operating environments, and simulates RMI real development and operation processes. 1. Implement a remote interface, generate a remote object, stub, and frame (SKELETON) implementation remote interface, the remote interface tells the JVM: The object that implements the interface can be called remotely and what methods can be called. Sayhello () is defined in this example. Since the remote call involves network communication, these methods must throw RemoteException. Remote interfaces and remote objects can be developed by A and packed the remote interface (Hello) to the Client End Developer B. Build a F: / Server directory, copy hello.java and helloimpl.java to this directory.

// Hello.java package jdeveloper.rmi; import java.rmi.Remote; import java.rmi.RemoteException; public interface Hello extends Remote {String sayHello () throws RemoteException;} generated 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 () }}}}} 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 distributes Hello.jar to the developer B for Client. Storage (stub) storage! Stub is dynamically downloaded. Client communicates with the Skeleton of the stub and the remote object, just like a local object. In most cases, the downloadable code is placed in a directory of the HTTP server. This example is placed under http: // hjc / rmi. HJC: Machine name, RMI: a directory of HTTP. If only a single test, you can put it in a directory as F: / ServerClasses. This article will give the above two methods (Policy files and bat files).

2. Client-implemented program // 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 ()); hello remoteobj = (hello) naming.lookup ("//" args [0] "/ helloserver"); system.out .println (RemoteObj.SAYHELLO ());}} Sets the F: / Client directory to copy HelloClient.java to this directory. Build a F: / ClientClasses directory, copy the hello.jar to this directory.

f: cd / client javac -classpath% classpath%; f: /clientclasses/hello.jar -d. HelloClient.java 3. Run the program Launch DOS window set classpath = start rmiregistry A. Running 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 boot new The DOS window puts 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";}; launch new DOS window to 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.r mi.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. Running 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 rmihttpserver.policy into the starthellohttpserver.bat and f: / Server / Start the new DOS window Run StartHellohttpserver StartHellohttpserver.bat set cp = f: / server;

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

New Post(0)