Download presentation
Presentation is loading. Please wait.
1
Java RMI (more) CS-328 Internet Programming
2
Remote 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
3
Example package rmi.hello
public interface Hello extends java.rmi.Remote{ String sayHello() throws java.rmi.RemoteException
4
Remote 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
5
Example 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.RemoteException { 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); } } }
6
UnicastRemoteObject 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
7
Local 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
8
Example 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); }
9
HTML <html> <head> <title>Hello World</title>
<body> <p>The message from the Hello Server is: <applet codebase=“../..” code=“rmi.hello.HelloApplet” width=500 height=120> </applet> </html>
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.