A simple example of the RMI programming of Java

zhaozj2021-02-16  59

Although we rarely use RMI to program, do the earliest distributed solution for Java, we still have a need to know, start with example (although it is a few years ago)!

Remote interface RMIHELLOREMOTEINTFC .JAVA

Import java.rmi. *;

Public interface rmihelloremoteintfc extends remote {string helloremoteobj (string client) throws remoteexception;}

Remote interface implementation class RMIHELLOREMOTEOBJ.JAVA

Import java.rmi.server. *; import java.rmi. *;

Public Class RmihelloremoteObj Extends UnicastremoteObject Implements RmihelloremoteInTfc {

Public rmihelloremoteobj () throws remoteException {super ();

Public String Helloremoteobj (String Client) THROWS RemoteException {Return "Hello World" Client;

}

Server RMIHELLOSERVER.JAVA

Import java.io. *; import java.rmi. *; import java.rmi.server. *; import sun.applet. *; import java.rmi.registry.locateRegistry;

Public class rmihelloserver {

Public rmihelloserver ()} public static void main (String [] args) {// creates and installs the security manager if (System.getSecurityManager () == null) {System.setSecurityManager (NEW RMISECURITYMANAGER ());}

Try {// Create a remote object RMIHELLOREMOTEOBJ TTT = new rmihelloremoteObj (); // Start registry locateRegistry.createRegistry (2222); // Award name Bind to Object Naming.rebind ("// fangm: ​​2222 / helloobj", ttt );

System.out.println ("RMI server is running ...");} catch (exception e) {E.printStackTrace ();

}

Server-side permission file RMIHELLOSERVER.POLICY

Grant CodeBase "File: / E: / Download / Rmitest /" {Permission Java.net.SocketPermission "*: 1000-9999", "Accept, Connect, Listen, Resolve";

Take a look at how the Client end is connected to the server and the call to the remote method.

Import java.rmi. *; import java.rmi.server. *;

Public class rmihelloclient {

Public rmihelloclient ()} public static void main (String [] args) {// creates and installs the security manager if (system.getsecuritymanager () == null) {system.setSecurityManager (NEW RMISECURITYMANAGER ());} Try { RmiHelloRemoteIntfc c1 = (RmiHelloRemoteIntfc) Naming.lookup ( "rmi: // Fangm: ​​2222 / helloObj"); System.out.println (c1.helloRemoteObj ( "Everyone"));} catch (Exception e) {e.printStackTrace ( } System.exit (0);

}

CLIENT End RMIHelloClient.Policy

Grant CodeBase "File: / E: / Download / Rmitest /" {Permission Java.net.SocketPermission "*: 1000-9999", "Accept, Connect, Listen, Resolve";

Ok, one of the simplest RMI examples will be safe, and the most important step is to compile and run. Let's take a batch of BAT or CMD to do it.

Take a look at RMIHELLO.BAT.

Javac RMIHelloremoteIntfc.java

Javac RMIHelloremoteobj.java

RMIC RMIHELLOREMOTEOBJ

Javac RMIHELLOSERVER.JAVA

Javac RMIHELLOCLIENT.JAVA

Echo launches the server in another window, please wait. . . Start java -djava.security.policy = rmihelloserver.policy RMIHELLOSERVER

Echo runs a client. . . Java -Djava.security.policy = rmihelloclient.policy rmihelloclient This file is simple! In the package, you will try it, although it is simple, but for beginners, it is still very good!

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

New Post(0)