Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 15 Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

Similar presentations


Presentation on theme: "1 Lecture 15 Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology."— Presentation transcript:

1 1 Lecture 15 Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology

2 2 Overview RMI applications are often comprised of two separate programs: a server and a client. RMI provides the mechanism by which the server and the client communicate and pass information back and forth. Such an application is sometimes referred to as a distributed object application.

3 2 Distributed object applications Distributed object applications need to Locate remote objects: Communicate with remote objects: Load class bytecodes for objects that are passed around:

4 2 Distributed object applications Locate remote objects: –An application can register its remote objects with RMI's simple naming facility, the rmiregistry, or –the application can pass and return remote object references as part of its normal operation.

5 2 Distributed object applications Communicate with remote objects: –Details of communication between remote objects are handled by RMI; –to the programmer, remote communication looks like a standard Java method invocation.

6 2 Distributed object applications Load class bytecodes for objects that are passed around: –RMI allows a caller to pass objects to remote objects, –RMI provides the necessary mechanisms for loading an object's code, as well as for transmitting its data.

7 3 RMI 1.bind/rebind 2. lookup 3. call

8 Creating Distributed Applications 1. Design and implement the components of your distributed application. 2. Compile sources and generate stubs. 3. Make classes network accessible. 4. Start the application.

9 Creating Distributed Applications 1. Design and implement the application components: Job: decide on your application architecture and determine local objects and remote objects. –Defining the remote interfaces: A remote interface specifies the methods that can be invoked remotely by a client. –Implementing the remote objects: Remote objects must implement one or more remote interfaces.

10 Creating Distributed Applications 1. Design and implement the application components: –Implementing the clients: Clients that use remote objects can be implemented at any time after the remote interfaces are defined. Clients can be invoked after the remote objects have been deployed.

11 3 Creating Distributed Applications 2. Compile sources and generate stubs. –use the javac to compile the source files, which contain the implementation of the remote interfaces and implementations, the server classes, and the client classes. –use the rmic to create stubs for the remote objects. –RMI uses a remote object's stub class as a proxy in clients so that clients can communicate with a particular remote object.

12 3 Creating Distributed Applications 3. Make classes network accessible. –Make the class files associated with the remote interfaces, stubs, and other classes that need to be downloaded to clients accessible via a Web server. 4. Start the application. –Starting the application includes running the RMI remote object registry, the server, and the client

13 3 Simple RMI Example Client Site: 1. MyObject.java 2. RemoteClient1.java 3. RemoteClient2.java Server Site: 1. RemoteServer.java 2. RemoteInterface.java 3. MyObject.java

14 3 Writing an RMI Server Our Remote server accepts tasks from clients, runs the tasks, and returns any results. The server is comprised of an interface and a class. The interface provides the definition for the methods that can be called from the client. Essentially the interface defines the client's viewof the remote object. The class provides the implementation.

15 Designing a Remote Interface The interface provides the definition for the methods that can be called from the client. Client submit a task to Remote server. Result of the task is sent back to the client. Each of the interfaces contains a single method. See RemoteInterface.java Remote Client Remote Client Remote Server Remote Server Submit task Return result

16 Remote Interface /* RemoteInterface.java */ public interface RemoteInterface extends java.rmi.Remote { MyObject msgsend (MyObject message) throws java.rmi.RemoteException; } The RemoteInterface interface defines a single method, msgsend, which passes in a MyObject and returns a MyObject, and throws RemoteExceptions. See MyObject.java

17 public class MyObject implements java.io.Serializable { private int x; private String msg; public MyObject (String s) { msg = s; x = msg.length(); } public int lenMsg() { // length of msg return x; } public String getMsg() { // get msg return msg; } public void setMsg(String s) { // set msg msg = s; }

18 Implement a Remote Interface Declare the remote interfaces being implemented. (UnicastRemoteObject is a convenience class, defined in the RMI public API, that can be used as a superclass for remote object implementations. ) Define the constructor for the remote object Provide an implementation for each remote method in the remote interfaces Create and install a security manager Create one or more instances of a remote object Register at least one of the remote objects with the RMI remote object registry.

19 import java.rmi.*; import java.rmi.server.UnicastRemoteObject; public class RemoteServer extends UnicastRemoteObject implements RemoteInterface{ String name; public RemoteServer(String name) throws RemoteException{ super(); this.name = name; } public MyObject msgsend(MyObject message) throws RemoteException{ System.out.println("msg got:" + message.getMsg() + ", msg length:" + message.lenMsg()); message.setMsg("My Name is:" + name + ",thanks for your message:"+ message.getMsg()); return message; }

20 public static void main (String args[]){ System.out.println("Running Server..."); System.setSecurityManager (new RMISecurityManager()); try{ String myName = "ObjectServer Test"; RemoteServer theServer = new RemoteServer(myName); Naming.rebind(myName,theServer); } catch (Exception e){ System.out.println("An Exception occured..."); }

21 Creating A Client Program Remote Client Remote Server RMI Registry Naming.rebind Server.msgsend Naming.lookup

22 import java.rmi.*; public class RemoteClient1 { public static void main(String args[]) { System.out.println("Running Client 1..."); System.setSecurityManager(new RMISecurityManager()); try { RemoteInterface server = (RemoteInterface) Naming.lookup("rmi://" + args[0] + "/" + "ObjectServer Test"); MyObject msgObj = new MyObject("Hello There From Client1"); System.out.println("Sending MyObject to Server..."); MyObject retObj = server.msgsend(msgObj); System.out.println(”Server says :" + retObj.retMsg()); } catch (Exception e){ System.out.println("Error while performing RMI"); }

23 Compiling the Example Programs Building interface: javac RemoteInterface.java javac MyObject.java (Note that both Client and Server sites need these) Building Server programs: javac RemoteServer.java rmic RemoteServer (produce RemoteServer_Stub.class and RemoteServer_Skel.class) Building Client programs: javac RemoteClient1.java javac RemoteClient2.java

24 Running the Example Programs Start the server 1. $rmiregistry (unix) c:>start rmiregistry (Win) A. By default, the registry runs on port 1099. B. To start the registry on a different port, specify the port number on the command line. (Ex. $rmiregistry 1089) 2. $java RemoteServer

25 Running the Example Programs Start the client 1. $java RemoteClient IP-adderss


Download ppt "1 Lecture 15 Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology."

Similar presentations


Ads by Google