Distributed Objects and JAVA <ul><li>Overview of distributed object programming </li></ul><ul><li>In-depth discussion of J...
What is RMI? <ul><li>RMI is a powerful technology for developing networked ap p lications. </li></ul><ul><li>RMI transcend...
Limitations of RMI! <ul><li>Both client and server must be JAVA applications! </li></ul><ul><li>Not as general as CORBA ar...
Developing an RMI Application <ul><li>Create an interface that extends java.rmi.Remote interface. </li></ul><ul><li>Define...
Developing RMI Apps ... <ul><li>The (server) class that implements the Remote interface must be a subclass of java.rmi.ser...
Developing RMI Apps ... <ul><li>A (server) program creates an instance of the remote object. </li></ul><ul><li>Register th...
<ul><li>Compile the server program (javac).  </li></ul><ul><li>rmic -- generates stub and a skeleton for the remote object...
<ul><li>Stub then passes return value back to the client. </li></ul><ul><li>rmic tool hides the details of generating stub...
<ul><li>Client programs use the remote object exported by the server. </li></ul><ul><li>Clients obtain a reference to the ...
<ul><li>RMI uses the JAVA serialisation mechanism to transfer the stub object from the server to the client. </li></ul><ul...
RMI Hello World Example package message; import java.rmi.*; /* This interfaces defines the exported methods */ public inte...
package client; import java.rmi.*; import java.rmi.server.*; import message.*; public static class Client { public static ...
% javac HelloWorld.java % rmic -d . server.HelloWorld % rmiregistry & % java HelloWorld package server; import java.rmi.*;...
RMI and JAVA 1..2 <ul><li>JAVA 2 requires a (security ) policy file. </li></ul><ul><li>java -Djava.security.manager -Djava...
Policy file grant { permission java.net.SocketPermission “*:1024-65535”, “accept,  connect, listen, resolve”; } grant { pe...
Bi-directional Messaging <ul><li>Extend the ideas of  the previous example so that Clients are registered with the Server....
<ul><li>Client registers with the Server. </li></ul><ul><li>Server stores the Client object in a hash table. </li></ul><ul...
Message Package package message; import java.rmi.*; public interface interface MessageReceiver extends Remote { void print...
Server Package package server; import java.util.Enumeration;  import java.util.HashTable;  import java.rmi.*; import java....
public int register( Strin g  name, MessageReceiver m) { int retval = MessageServer.FAILURE; if((name != null) && (m != nu...
Client package package client; import java.awt.*; import java.awt.event.*;  import java.rmi.*; import java.rmi,.server.*; ...
public void actionPerformed(ActionEvent ae) { try { server.send(name,tf.getText()); }catch Exception e) { …}} public void ...
Class Loading <ul><li>Dynamically load class code. </li></ul><ul><li>java.rmi.server.codebase property. </li></ul><ul><li>...
Why do we need class loading? <ul><li>Not all class files are available on all hosts (machines). </li></ul><ul><li>If your...
Configuration for class loading <ul><li>RMI runtime includes an URL for marshalled data/objects. </li></ul><ul><li>Receivi...
<ul><li>If an RMI system attempts to unmarshal some data/object and the class is not found by the default loader. </li></u...
Garbage Collection <ul><li>Normally don’t have to worry! Automatic process. </li></ul><ul><li>Every server that contains R...
Garbage Collection <ul><li>When client is finished with the reference and allows the remote stub to go out of scope, or le...
<ul><li>Server may require notification when client exits/disconnects. </li></ul><ul><li>Remote objects can implement java...
RMI and FireWalls <ul><li>Firewalls are machines/program that prevent certain types of network connections. </li></ul><ul>...
Remote Object Activation <ul><li>UnicastRemoteObject allows the creation of a remote ob j ect that can be accessed from a ...
<ul><li>Java2 introduces the notion of activation. Managed by a small program called rmid that runs on the server. </li></...
Features of RMI Activation Service <ul><li>Ability to automatically create remote objects triggered by requests for these ...
Defining an activatable remote object <ul><li>Subclass your remote object implementation from the Activatable class provid...
RMI  Introduction  Summary <ul><li>RMI aims to support seamless remote method invocation on objects in different JVMs, als...
Upcoming SlideShare
Loading in …5
×

Distributed Objects and JAVA

1,518
-1

Published on

Published in: Technology, News & Politics
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,518
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
98
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Distributed Objects and JAVA

  1. 1. Distributed Objects and JAVA <ul><li>Overview of distributed object programming </li></ul><ul><li>In-depth discussion of JAVA’s remote method invocation package with examples. </li></ul><ul><li>Initially an introduction to RMI. </li></ul><ul><li>Examples, illustrate features of RMI. </li></ul>
  2. 2. What is RMI? <ul><li>RMI is a powerful technology for developing networked ap p lications. </li></ul><ul><li>RMI transcends client/server model of computing with the remote object model. </li></ul><ul><li>Servers define object that client s can use remotely. </li></ul><ul><li>Clients invoke methods of remote objects transparently (once located) . </li></ul><ul><li>Arguments/return values can be primitive values or any serializable object. </li></ul>
  3. 3. Limitations of RMI! <ul><li>Both client and server must be JAVA applications! </li></ul><ul><li>Not as general as CORBA architecture. </li></ul><ul><li>Limitations are being over come by RMI-IIOP infrastructure. </li></ul>
  4. 4. Developing an RMI Application <ul><li>Create an interface that extends java.rmi.Remote interface. </li></ul><ul><li>Defines the exported methods that a remote object implements. </li></ul><ul><li>Each method in the interface MUST be declared to throw java.rmi.RemoteException. </li></ul><ul><li>Necessary to handle errors with network connections and server problems. </li></ul>
  5. 5. Developing RMI Apps ... <ul><li>The (server) class that implements the Remote interface must be a subclass of java.rmi.server.UnicastRemoteObject. </li></ul><ul><li>Remote methods are declared to throw RemoteException objects. </li></ul><ul><li>UnicastRemoteObject and the RMI infrastructure handles everything else ... </li></ul>
  6. 6. Developing RMI Apps ... <ul><li>A (server) program creates an instance of the remote object. </li></ul><ul><li>Register the object by name with a registry service (this exports the object, making it available for use by clients), such as the java.rmi.Naming class and the rmiregistry program. </li></ul><ul><li>A server programs can act as its own registry server thro’ LocateRegistry class and the Registry interface of the java.rmi.registry package. </li></ul>
  7. 7. <ul><li>Compile the server program (javac). </li></ul><ul><li>rmic -- generates stub and a skeleton for the remote object. </li></ul><ul><li>Client’s reference to a remote object is implemented as an instance of a stub class. </li></ul><ul><li>Stub does the necessary networking to pass a method invocation onto a skeleton class on the server. </li></ul><ul><li>Skeleton translates the networked request into a method invocation on the server object and passes the return value back to the stub. </li></ul>
  8. 8. <ul><li>Stub then passes return value back to the client. </li></ul><ul><li>rmic tool hides the details of generating stubs and skeletons from the programmer (similar to rpcgen). </li></ul><ul><li>rmic generate classes with the suffixes __Stub and __Skel. </li></ul><ul><li>Servers using the default registry service (provided by the Naming class) require users to run the registry server (invoke rmiregistry program). </li></ul>
  9. 9. <ul><li>Client programs use the remote object exported by the server. </li></ul><ul><li>Clients obtain a reference to the remtoe object using the Naming class to look up the object by name. </li></ul><ul><li>Name is typically an rmi: URL. </li></ul><ul><li>The remote reference obtained is an instance of the Remote interface for the object (actually a stub object). </li></ul><ul><li>Client can then invoke methods on this remote reference transparently, except that all remote methods might throw RemoteException objects. </li></ul>
  10. 10. <ul><li>RMI uses the JAVA serialisation mechanism to transfer the stub object from the server to the client. </li></ul><ul><li>Security manager objects should be installed to prevent the loading of an untrusted stub object from causing harm (RMISecurityManager class is suitable). </li></ul><ul><li>Finally, start the server and run the client. </li></ul>
  11. 11. RMI Hello World Example package message; import java.rmi.*; /* This interfaces defines the exported methods */ public interface RemoteHelloWorld extends Remote { public String sayHello () throws RemoteException(); }
  12. 12. package client; import java.rmi.*; import java.rmi.server.*; import message.*; public static class Client { public static void main(String []args) { try{ System.setSecurityManager(new RMISecurityManager()); //Read a system property, specified on command line with -D // to determine host String url = System.getProperty(“myserver”,”rmi:///HELLO”); RemoteHelloWorld server = (RemoteHelloWorld) Naming.lookup(url); String value = server.sayHello(); System.out.println(value); }catch(RemoteException e) { System.err.println(e);} catch(Exception e) { System.err.println(e); System.err.println(“USAGE: java [-Dmyserver=url>]”); } }}
  13. 13. % javac HelloWorld.java % rmic -d . server.HelloWorld % rmiregistry & % java HelloWorld package server; import java.rmi.*; import java.rmi.server.*; import message.*; import java.rmi.registry.*; public class HelloWorld extends UnicastRemoteObject implements RemoteHelloWorld { String sayHello() throws RemoteException{ return “Hello World”;} public static void main(String []args) { try{ HelloWorld theObject= new HelloWorld(); Naming.rebind(String(“HELLO”),theObject); System.out.println(“HELLO is now up and running”); }catch(Exception e){ …} } }
  14. 14. RMI and JAVA 1..2 <ul><li>JAVA 2 requires a (security ) policy file. </li></ul><ul><li>java -Djava.security.manager -Djava.security.policy=policy server.HelloWorld </li></ul><ul><li>java -Djava.security.policy=policy client.Client </li></ul>
  15. 15. Policy file grant { permission java.net.SocketPermission “*:1024-65535”, “accept, connect, listen, resolve”; } grant { permission java.lang.RuntimePermission “createSecurityManager”; };
  16. 16. Bi-directional Messaging <ul><li>Extend the ideas of the previous example so that Clients are registered with the Server. </li></ul><ul><li>Means that the Server can call back to Clients. </li></ul><ul><li>Simple GUI is added to allow the use to type and see messages being typed by other Clients. </li></ul>
  17. 17. <ul><li>Client registers with the Server. </li></ul><ul><li>Server stores the Client object in a hash table. </li></ul><ul><li>Client sends a message to the Server that forwards the message to each object in the hash table. </li></ul><ul><li>Creates a simple networked chat program. </li></ul>
  18. 18. Message Package package message; import java.rmi.*; public interface interface MessageReceiver extends Remote { void print (String s) throws RemoteException; } public interface MessageServer extends Remote { static String SERVER_NAME = “MessageServer”; static int FAILURE = -1; static int SUCCESS = 0; void send(String name,String s) throws RemoteException; int register(String name,MessageReceiver m) throws RemoteException; }
  19. 19. Server Package package server; import java.util.Enumeration; import java.util.HashTable; import java.rmi.*; import java.rmi.server.*; import java.rmi.registry.*; import message.*; public class Server extends UnicastRemoteObject implements MessageServer { static HashTable receivers = new HashTable(5); public Server() throws RemoteException { super();} public void send(String name, String s) throws RemoteException { for(Enumeration e = receivers.elements(); e.hasMoreElements();) { MessageReceiver m = (MessageReceiver) e.nextElement(); m.print(name + “: “ + s); } }
  20. 20. public int register( Strin g name, MessageReceiver m) { int retval = MessageServer.FAILURE; if((name != null) && (m != null)) { if(receivers.get(name) == null) { receivers.put(name,m); System.out.println(“Added “ + name); retval = MessageServer.SUCCESS; }else { System.out.println(“Client not added because “ + name + “ already exists on Server”); } } return retval; } public static void main(String []args) { System.setSecurityManager(new RMISecurityManager()); try { MessageServer server = new Server(); // Bootstrap PRIVATE registry! Registry reg = LocateRegistry.createRegistry( Registry.REGISTRY_PORT); reg.rebind(MessageServer.SERVER_NAME,server); System.out.println(“Server bound and started”); } catch(Exception e) { …} } }
  21. 21. Client package package client; import java.awt.*; import java.awt.event.*; import java.rmi.*; import java.rmi,.server.*; import message.*; public class Client extends Frame implements MessageReceiver, ActionListener, WindowListener { TextField tf; TextArea ta; static String name; static MessageServer server; public Client() throws RemoteException { setTitle(name); setLayout(new BorderLayout()); tf = new TextField(30); tf.addActionListener(this); add(“South”,tf); ta = new TextArea(20,20); addWindowListener(new WindowCloser()); UnicastRemoteObject.exportObject(this); // accept remote calls! }
  22. 22. public void actionPerformed(ActionEvent ae) { try { server.send(name,tf.getText()); }catch Exception e) { …}} public void print(String s) { ta.append(s+” ”);} public static void main(String args[]) { if(args.length < 1) { System.out.println(“USAGE: java client.Client NAME”); System.exit(-1); } name = args[0]; System.setSecurityManager(new RMISecurityManager()); try { Client cf = new Client(); cf.pack();cf.show(); server =(MessageServer) Naming.lookup(MessageServer.SERVER_NAME); int s = server.register(args[0],cf); if(s == MessageServer.FAILURE) throw new Exception(“Could not Connect to SERVER”); } catch(Exception e) {… System.exit(-1);} }}
  23. 23. Class Loading <ul><li>Dynamically load class code. </li></ul><ul><li>java.rmi.server.codebase property. </li></ul><ul><li>Properties sysProps; sysprops = System.getProperties(); sysProps.put(“java.rmi.server.codebase”,aString);system.setProperties(sysProps); </li></ul><ul><li>grant { permission java.security.AllPermission;}; WARNING!!! </li></ul>
  24. 24. Why do we need class loading? <ul><li>Not all class files are available on all hosts (machines). </li></ul><ul><li>If your application involves remote agents running on hosts not under your control. </li></ul>
  25. 25. Configuration for class loading <ul><li>RMI runtime includes an URL for marshalled data/objects. </li></ul><ul><li>Receiving process then knows where to locate a marshalled object’s byte code (CLASS). </li></ul><ul><li>IF the class for the object being marshalled was loaded by a non default class loader, then the codebase of that particular loader is encoded in the marshalled stream. </li></ul><ul><li>ELSE the class were loaded using the default loader from the local CLASSPATH then the value of java.rmi.server.codebase is sent. </li></ul>
  26. 26. <ul><li>If an RMI system attempts to unmarshal some data/object and the class is not found by the default loader. </li></ul><ul><li>RMIClassLoader can use URL in stream to look for class bytecodes directly. </li></ul><ul><li>Unmarshal operation will generate an exception if the class cannot be found. </li></ul><ul><li>Must have a security manager installed for remote class loading! </li></ul><ul><li>permission java.net.SocketPermission “myhost.cs.tcd.ie”, “accept,connect”; </li></ul>
  27. 27. Garbage Collection <ul><li>Normally don’t have to worry! Automatic process. </li></ul><ul><li>Every server that contains RMI-exported objects automatically maintains a list of remote references to the objects it serves </li></ul><ul><li>List is maintained explicitly through registry/naming service, or implicitly as the result of a remote method call. </li></ul><ul><li>Each client is issued a remote object reference through the remote reference layer. </li></ul><ul><li>Record of this object is in the form of an expirable lease on the object. </li></ul>
  28. 28. Garbage Collection <ul><li>When client is finished with the reference and allows the remote stub to go out of scope, or lease expires. </li></ul><ul><li>Reference layer (hosts) then deletes the record of the remote reference and notifies the client’s reference layer that the remote reference has expired. </li></ul><ul><li>Concept of expirable leases is used to deal with situations where client-side or network failure prevents a client from notifying a server that is done with its reference to an object. </li></ul>
  29. 29. <ul><li>Server may require notification when client exits/disconnects. </li></ul><ul><li>Remote objects can implement java.rmi.server.Unreferenced interface. </li></ul><ul><li>Single method unreferenced. </li></ul><ul><li>unreferenced method is sent to the server when no more clients hold references to a remote object. </li></ul><ul><li>Default behaviour, unreferenced will be received when no clients have accessed a remote object for 10 mins, when all stub files are finalized. </li></ul><ul><li>Client timeout can be customised by setting java.rmi.dgc.leaseValue property to the desired number of milliseconds. </li></ul><ul><li>Clients can be programmed to initiate the finalize methods on their stub files (ensure notification happens ASAP). </li></ul>
  30. 30. RMI and FireWalls <ul><li>Firewalls are machines/program that prevent certain types of network connections. </li></ul><ul><li>RMI (default) attempts to make direct connections from client to server. </li></ul><ul><li>Firewalls often only allow HTTP connections for web browsing. </li></ul><ul><li>RMI supports use of HTTP. </li></ul><ul><li>Various work-arounds in case of firewalls. </li></ul>
  31. 31. Remote Object Activation <ul><li>UnicastRemoteObject allows the creation of a remote ob j ect that can be accessed from a server program. </li></ul><ul><li>Server program must be accessible at all time to allow client connection. </li></ul><ul><li>Performance hit can be large if many remote object servers must be active! </li></ul>
  32. 32. <ul><li>Java2 introduces the notion of activation. Managed by a small program called rmid that runs on the server. </li></ul><ul><li>Client request a service, rmid starts the remote object and communication starts normally. Many services need only then take up CPU time when the service is actually required! </li></ul><ul><li>rmid must be configured to know where to find your server programs and all remote objects. </li></ul>
  33. 33. Features of RMI Activation Service <ul><li>Ability to automatically create remote objects triggered by requests for these objects. </li></ul><ul><li>Support for activation groups, in which groups of activateable remote objects are executed in the same JVM,(auto-start JVM if necessary). </li></ul><ul><li>Ability to restart remote objects if they exit, or are destroyed. Fault-tolerance. </li></ul>
  34. 34. Defining an activatable remote object <ul><li>Subclass your remote object implementation from the Activatable class provided in java.rmi.activation package. </li></ul><ul><li>Provide activation contstructors in the server implementation. </li></ul><ul><li>Register the object and its activation method with the activation service. </li></ul><ul><li>Do the lab! </li></ul>
  35. 35. RMI Introduction Summary <ul><li>RMI aims to support seamless remote method invocation on objects in different JVMs, also callbacks from servers and applets. </li></ul><ul><li>Debugging: use the flag -Djava.rmi.server.logCalls=true </li></ul>

×