CLASSPATH
of both the client and the server
Service.java
public class ServiceImpl implements Service {...}
javac *.java
rmic
will generate stubs from an
implementation object
rmic ServiceImpl
rmic -v1.2 ServiceImpl
ServiceImpl_Stub
it must be copied to a directory where an httpd
server can deliver it to clients
cp ServiceImpl_Stub.class /var/httpd/html/classes/ServiceImpl_Stub.class
Remote
interface
RemoteException
interface RemoteFileClassifier extends java.rmi.Remote {
public String getMIMEType(String fileName)
throws java.rmi.RemoteException;
}
java.io.Serialisable
java.rmi.server.UnicastRemoteObject
import java.io.*;
import java.rmi.server.UnicastRemoteObject;
public class RemoteFileClassifierImpl extends UnicastRemoteObject
implements RemoteFileClassifier {
public RemoteFileClassifierImpl()
throws java.rmi.RemoteException {
}
public String getMIMEType(String fileName)
throws java.rmi.RemoteException {
// implementation code
if (fileName.endsWith(".gif")) {
return new String("image/gif");
} else if (fileName.endsWith(".jpeg")) {
return new String("image/jpeg");
} else
// fill in lots of other types,
// but eventually give up and
return null;
}
}
java.rmi.server.codebase
to a URL
import java.rmi.RemoteException;
import java.rmi.Naming;
public class Server {
public static void main(String[] args) {
RemoteFileClassifier classifier = null;
try {
classifier = new RemoteFileClassifierImpl();
} catch(RemoteException e) {
e.printStackTrace();
System.exit(1);
}
// make it public - advertise it
try {
Naming.rebind("rmi://localhost/FileClassifier", classifier);
} catch(RemoteException e) {
e.printStackTrace();
System.exit(1);
} catch(java.net.MalformedURLException e) {
System.out.println(e);
System.exit(1);
}
// main can now finish. An RMI thread keeps the server alive
}
}
import java.io.*;
import java.rmi.Naming;
public class Client {
public static void main(String[] args) {
RemoteFileClassifier classifier = null;
// find the object
System.setSecurityManager(new java.rmi.RMISecurityManager());
try {
classifier = (RemoteFileClassifier)
Naming.lookup("rmi://130.194.67.203/FileClassifier");
} catch(java.net.MalformedURLException e) {
System.out.println(e);
System.exit(1);
} catch(java.rmi.NotBoundException e) {
System.out.println(e);
System.exit(1);
} catch(java.rmi.RemoteException e) {
System.out.println(e);
System.exit(1);
}
// then call it
try {
System.out.println(classifier.getMIMEType("picture.gif"));
} catch(java.rmi.RemoteException e) {
System.out.println(e);
System.exit(1);
}
System.exit(0);
}
}
Naming.rebind(String advertisedName, Object obj);
rmi://host/advertisedName
(Note: the host should not be localhost
since that will break as soon as you use two or more
machines)
Object Naming.lookup(String url)
rmiregistry
RMISecurityManager
should be used when
dealing with remote code
interface RemoteFileClassifier extends java.rmi.Remote {
public String getMIMEType(String fileName)
throws java.rmi.RemoteException;
}
import java.io.*;
import java.rmi.server.UnicastRemoteObject;
public class RemoteFileClassifierImpl extends UnicastRemoteObject
implements RemoteFileClassifier {
public RemoteFileClassifierImpl()
throws java.rmi.RemoteException {
}
public String getMIMEType(String fileName)
throws java.rmi.RemoteException {
// implementation code
if (fileName.endsWith(".gif")) {
return new String("image/gif");
} else if (fileName.endsWith(".jpeg")) {
return new String("image/jpeg");
} else
// fill in lots of other types,
// but eventually give up and
return null;
}
}
import java.rmi.RemoteException;
import java.rmi.Naming;
public class Server {
public static void main(String[] args) {
RemoteFileClassifier classifier = null;
try {
classifier = new RemoteFileClassifierImpl();
} catch(RemoteException e) {
e.printStackTrace();
System.exit(1);
}
// make it public - advertise it
try {
Naming.rebind("rmi://localhost/FileClassifier", classifier);
} catch(RemoteException e) {
e.printStackTrace();
System.exit(1);
} catch(java.net.MalformedURLException e) {
System.out.println(e);
System.exit(1);
}
// main can now finish. An RMI thread keeps the server alive
}
}
import java.io.*;
import java.rmi.Naming;
public class Client {
public static void main(String[] args) {
RemoteFileClassifier classifier = null;
// find the object
System.setSecurityManager(new java.rmi.RMISecurityManager());
try {
classifier = (RemoteFileClassifier)
Naming.lookup("rmi://130.194.67.203/FileClassifier");
} catch(java.net.MalformedURLException e) {
System.out.println(e);
System.exit(1);
} catch(java.rmi.NotBoundException e) {
System.out.println(e);
System.exit(1);
} catch(java.rmi.RemoteException e) {
System.out.println(e);
System.exit(1);
}
// then call it
try {
System.out.println(classifier.getMIMEType("picture.gif"));
} catch(java.rmi.RemoteException e) {
System.out.println(e);
System.exit(1);
}
System.exit(0);
}
}
policy.all
)
grant {
permission java.security.AllPermission "", "";
};
rmiregistry
java -Djava.rmi.server.codebase=... Server
java -Djava.security.policy=policy.all Client
Serialisable
Serialisable
-
this is an error and will either throw an exception or
pass a null
object
Serialisable
A FireStation
may have a method
FireEngine raiseAlarm(FireEvent evt)
A home may get a stub for a fire station, to raise alarms when a fire event
occurs. In return, it gets a fire engine.
exportObject()
on it
Naming.rebind(Remote)
, the
stub is substituted and sent to the naming service
rmic
stub generator has options to create the RMI stub
which uses IIOP instead of JRMP