Title: Java%20RMI%20(more)
1Java RMI (more)
- CS-328
- Internet Programming
2Remote Interface
- This really the interface for the servers methods
- Must be public
- must extend Remote
- each method in the interface must throw
RemoteException in addition to any application
specific exceptions
3Example
package rmi.hello public interface Hello extends
java.rmi.Remote String sayHello() throws
java.rmi.RemoteException
4Remote Object Implememtation
- Must specify the remote interfaces being
implemented - optionally extend a remote server
- extending UnicastRemoteObject indicates a single,
non-replicated remote object - define the default constructor
- must throw RemoteException
- provide an implementation for each remote method
defined in the interface - create and install a security manager either
RMISecurityManager or one you have defined - create one or more instances of remote object
- register at least one object with the RMI
Registry
5Example
package rmi.hello import java.rmi. import
java.rmi.server.UnicastRemoteObject public class
HelloImpl extends UnicastRemoteObject implements
Hello private String name public
HelloImpl(String s) throws java.rmi.RemoteExceptio
n super() name s public
String sayHello() throws RemoteException
return Hello World public static void
main (String args)
System.setSecurityManager(new RMISecurityManager()
) try HelloImpl obj
new HelloImpl(HELLOSERVER)
Naming.rebind(HELLOSERVER,obj)
System.out.println( HelloImpl created and bound
to registry as HELLOSERVER) catch
(Exception e) System.out.println
(Exception e.toString)
6UnicastRemoteObject
- A form of a remote server
- the server cannot be started remotely, must
already exist and the reference exists only as
long as the process - Uses TCP/IP ( the RMI specification is written
for network protocol independence) - An object stream is used to parameters and methos
invocations between client and server - Other types of servers will be made available as
they are developed - JDK 1.3 also provides Multicast
7Local Client
- look-up the remote object and get a reference to
its local stub - invoke one or more remote methods
- just like invoking a method on a local object
8Example
package rmi.hello import java.rmi. import
java.awt. public class HelloApplet extends
java.applet.Applet String message
public void init() try
Hello obj (Hello)Naming.lookup(//
getCodeBase().getHost()
/HELLOSERVER) message
obj.sayHello() catch (Exception e)
System.out.println(Exception
e.toString()) public void
paint(Graphics g) g.drawString(message, 25,
50)
9HTML
lthtmlgt ltheadgt lttitlegtHello Worldlt/titlegt lt/headgt lt
bodygt ltpgtThe message from the Hello Server
is ltapplet codebase../..
codermi.hello.HelloApplet
width500 height120gt lt/appletgt lt/htmlgt