Java Facilities .edu



Java RMI Implementation

Steps include:

1) Define the Interface class (e.g. MyInterface)

Define methods to be invoked remotely.

Must extend the java.rmi.Remote interface to define object as being remote

import java.rmi.Remote;

import java.rmi.RemoteException;

public interface MyInterface extends java.rmi.Remote

{

public abstract void method1(Object item) throws

RemoteException;

}

2) Define the Remote Object (Server) as an Interface Implementation class: (e.g. in MyInterfaceImpl.java).

Extend the java.rmi.server.UnicastRemoteObject interface and “implement” the Interface class defined above. This creates a single server.

import java.rmi.*;

import java.rmi.server.UnicastRemoteObject;

import java.util.*;

public class MyInterfaceImpl

extends java.rmi.server.UnicastRemoteObject

implements MyInterface {



Define a main() to:

Install a RMI Security Manager. Without a Security Manager only local procedure calls are supported.

Create an instance of the server and register it with the RMI registry using the Naming.rebind() method.

public static void main() {

if (System.getSecurityManager() == null)

System.setSecurityManager(new RMISecurityManager() );

try {

MyInterfaceImpl server = new MyInterfaceImpl();

// export server MyServer to RMI Registry at port NNNN

Naming.rebind(“//Hostname:NNNN/MyServer”,server);

} catch (Exception e) {System.out.println(“Rebind error: “ + e.getMessage()); }

Above System and Naming are the name of the class - a specific object is not required to be instantiated. The rebind() function parameters include the host name (e.g. //carrot.cs.uwp.edu) and the RMI Registry port separated by a colon (: ). The RMI Registry is a name service that translates object server names (e.g. “MyServer”) to object references and vice versa. It is important that every person in the class have their own RMI Registry port. Please set your RMI Registry port to the last 4 digits of your phone number or last 4 digits of your student ID, and hopefully we will have no conflicts. Also the port number should be over 3000.

A constructor must exist for the Server Object, and shall be minimally defined as follows:

public MyInterfaceImpl() throws RemoteException

{

super();

}

3) Define all Server methods as throwing a RemoteException:

public void method1(MyObject item) throws RemoteException { … }

If server methods include objects, the objects must be defined as Serializable.

import java.io.*;

class MyObject implements java.io.Serializable { … }

4) Create a client class.

In client class constructor, fetch the reference to the remote object using the lookup() method:

MyInterface object = (MyInterface) Naming.lookup(“//HostName:NNNN/MyServer”);

The lookup() call passes the host name and the service name. In this example the HostName and RMI Registry port are provided, separated by a colon (: ). The HostName is the name of the unix machine. The string passed in the client lookup() function shall be identical to that passed in the server bind() function. Import java.rmi.* so that Naming.lookup will compile properly, and catch any exceptions.

The client should create an object of type Interface and then can call the methods as follows:

try {

ojbect.method1(Object item);

} catch (Exception e) {

System.err.println(“Client method1(): “ + e.getMessage());

e.printStackTrace();

}

5) Create a Policy file (called ‘policy’ or whatever). The following policy file allows everyone access to the server. It should not be used commercially, but is o.k. for our implementation:

grant {

permission java.security.AllPermission;

};

6) Compile and run all programs (on UNIX):

javac Interface.java

javac IntefaceImpl.java

javac Client.java

Generate stub and skeleton:

rmic InterfaceImpl

This creates one or two files (depending): IntefaceImpl_Skel.class and/or InterfaceImpl_Stub.class.

Start the registry, which tracks the location of the objects. The registry and remote objects are created on the server node in background mode on UNIX:

rmiregistry NNNN &

or

grmiregistry NNNN &

where NNNN is the RMI Registry port number. The & puts the process in the background, and gives you a prompt again.

Start an instance of the remote object with:

java -Djava.security.policy=/home/student/myname/java/policy InterfaceImpl

The above indicates where the ‘policy’ file can be found. Alternately you can specify the policy filename only if everything is being run from the same (current) directory. If the server is running properly, the server will not return or print anything, but will simply appear to ‘hang’.

Start the client program (with whatever you named the client):

java ClientProgram

NOTE: If you change the interface to the file server, or if you change java programs resulting in a change of directory, it is necessary to kill the RMI Registry process and the remote object process.

To do that get a listing of the processes associated with your login ID as follows:

unix> ps -ef | grep

The second column of the output displays the ProcessID for each process. Once the processID is known, execute for both the RMI registry and your server object:

unix> kill -9

If you change your server software without changing its interface, it is important to kill the server process before starting up the revised server.

IT IS IMPERITIVE THAT YOU KILL YOUR SERVER AND RMI REGISTRY BEFORE LOGGING OFF.

Homework 1

Take the existing code from /home/student/Classes/Cs370/javaRMI, and implement the following a News Service. The News Service 1) collects news submitted by clients and 2) provides a copy when asked for the news.

$ mkdir javaRMI

$ cp /home/student/Classes/Cs370/javaRMI/* ~/javaRMI

(The above creates a new directory javaRMI in your directory, then copies all of my beginner files into it.)

Call the service “MyNews” with methods “submit(String news)” and “String readNews()”

Develop the following enhancements to the program:

• The server should have two methods, which need to be updated in both the Server and ServerImpl. The two methods include:

▪ A readNews() function to read from a file (with a name of your choosing).

▪ A submit function: submit(String news): This function appends the string sent as a parameter to your file. The string name provides the sender of the information and the news.

• The client should have a menu of two options: read and write news.

• For the write news option, the prompts shall be: “What is your name?” Then – “What news do you have to send?” The user is expected to enter a news item or favorite saying (e.g. “Be a good citizen and vote!”) The final prompt: “Where would you like to send this news?” The user should reply with a machine name (e.g. “carrot”). Then the concatenated string with name and news shall be sent to the requested machine, and should be stored there.

• For the read news option, only one prompt is required: “From what machine do you want to read the news?” The read() request shall be sent to that machine, and hopefully you will get a reply.

• Use port 5432 for the RMI registry, and call the server: “MyNews”.

Once you can send and receive from your client to your server, clear your file, and ask other students to send and receive to you.

Turn in a paper copy of your code, and also submit to the Submit directory:

$ submit 370 Client.java Server.java ServerImpl.java

(or whatever you have called them)

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download