CS 584 Lecture 18 Assignment Glenda assignment extended to the Java RMI Deadline No Java RMI Assignment Test Friday, Saturday, Monday
Java RMI
RMI Programming Steps Coding the source files Compilation and Deployment Running
Coding the Source Files There are at least 3 source files Interface definition Server Implementation Client Implementation HTML file if applet
Interface Definition Interface must be public Extends java.rmi.Remote Must declare java.rmi.RemoteException in throws clause
Interface Definition Example package examples.hello public interface Hello extends java.rmi.Remote { String sayHello() throws java.rmi.RemoteException; }
Server Implementation Specifies the remote interface Defines the constructor Implements methods Creates and Installs a security manager Creates instances of the remote object Registers the remote object with the RMI remote object registry
Server Implementation package examples.hello import java.rmi.*; import java.rmi.server.UnicastRemoteObject public class HelloImpl extends UnicastRemoteObject implements Hello { private String name;
Server Implementation public HelloImpl(String s) throws RemoteException { super(); name = s; } public String sayHello() throws RemoteException return "Hello World";
Server Implementation public static void main(String[] args) { System.setSecurityManager(new RMISecurityManager()); try { HelloImpl obj = new HelloImpl("HelloServer"); Naming.rebind("//myhost/HelloServer", obj); System.out.println("Hello Server bound"); catch(Exception e) { System.out.println("Err: " + e.getMessage); e.printStackTrace(); }
Client Implementation Obtain a reference to the "HelloServer" from the server's registry Invoke the method on the remote object Use the returned results
Client Implementation package examples.hello import java.awt.*; import java.rmi.*; public class HelloApplet extends java.applet.Applet { String message = " ";
Client Implementation public void init() { try { Hello obj = (Hello)Naming.lookup("//" + getCodeBase().getHost() + "/HelloServer"); message = obj.sayHello(); } catch (Exception e) { System.out.println("Err: " + e.getMessage); e.printStackTrace();
Client Implementation public void paint(Graphics g) { g.drawString(message, 25,50); }
Compilation Compile the source files using javac Generate Stub and Skeleton files client side and server side proxies generated using rmic on class files
Deployment RMI is based on packages RMI objects need to be in a visible place
Execution Start the registry on the server start rmiregistry Start the server using the java interpreter Run the applet
For More Information See Sun's website for a tutorial web3.javasoft.com:80/products/jdk/1.1/docs/guide/rmi/getstart.doc.html Remember the test!!!!!