The Implementation of the Arith interface
The Arith.java interface is implemented on the server. The
implementation must extend java.rmi.server.UnicastRemoteObject.
The constructor
must throw the RemoteException.
The implementation class needs to do the following:
-
Specify the remote interface being implemented
-
Define the constructor for the remote object
-
Implement the methods that can be invoked remotely
-
Create an instance of the security manager and install it.
-
Create one (or more) instance(s) of a remote object
-
Register one (or more) remote object(s) with the RMI registry.
package mahmoud.ch8;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
/**
* @(#) Server.java
* @author Qusay H. Mahmoud
*/
public class ArithImpl extends UnicastRemoteObject implements Arith {
private String name;
public ArithImpl(String s) throws RemoteException {
super();
name = s;
}
public int[] add(int a[], int b[]) throws RemoteException {
int c[] = new int[10];
for (int i=0; i<10; i++) {
c[i] = a[i] + b[i];
}
return c;
}
public static void main(String argv[]) {
System.setSecurityManager(new RMISecurityManager());
try {
ArithImpl obj = new ArithImpl("ArithServer");
Naming.rebind("//Gateway/ArithServer", obj);
System.out.println("ArithServer bound in registry");
} catch (Exception e) {
System.out.println("ArithImpl err: " + e.getMessage());
e.printStackTrace();
}
}
}
Points to Note
-
Extending the class java.rmi.server.UnicastRemoteObject
indicates that the ArithImpl class is used to create
a single, nonreplicated, remote object that uses RMI's
default socket-based transport layer for communication.
- Normally, you do not need to explicitly call the no-arg constructor
for a class. The parent no arg constructor is automatically
called. Here, however, the constructor throws an exception. Therefore,
you need to call the parent constructor explicitly with super().
This is a Java point, not an RMI point.
-
The class provides implementation for the only one method
in the interface. In general case, arguments to the remote method
and its return value can be of any serializable Java type.
- RMI does not load a security manager automatically (the way
an applet does). You must do it yourself. RMI provides the security
manager shown , which is good enough for most purposes. Without
a security manager loaded, RMI will not work.
The purpose of security manager is to protect the host from
malicious code sent from the client. Load the security manager before
registering.
-
The next line creates an instance of the remote object:
ArithImpl obj = new ArithImpl("ArithServer");
Once the instance is created, the server is ready
to listen for clients' requests.
-
In order for a client to be able to invoke a method on a remote object,
the client must first obtain a reference to this remote object.
Therefore, a remote object must be registered.
Naming.rebind() registers a name for the remote object with
the RMI URL-based registry server. Of course the registry must have been
started before you run this code! The client will look up the
name ArithServer on the host named Gateway.
The first arg to
rebind() is a URL which could be written as
rmi://Gateway
naming the RMI protocol explicitly.
Note also the following:
-
If the hostname is omitted from the URL,
the host defaults to the current host.
-
The RMI registry, by default, will run on port 1099.
However, the port number can be supplied in the URL --
for example,
//jupiter.scs.ryerson.ca:9876/ArithServer
where 9876 is the port number.