Remote Method Invokation

Slides:



Advertisements
Similar presentations
Distributed Objects Remote Method Invokation: Conceptual model.
Advertisements

What is RMI? Remote Method Invocation –A true distributed computing application interface for Java, written to provide easy access to objects existing.
Copyright © 2001 Qusay H. Mahmoud RMI – Remote Method Invocation Introduction What is RMI? RMI System Architecture How does RMI work? Distributed Garbage.
15-May-15 RMI Remote Method Invocation. 2 “The network is the computer” Consider the following program organization: If the network is the computer, we.
Java Remote Method Invocation (RMI) In Java we implement object systems: O1O2 O3 thread 1thread 2 execution scheme JVM 1JVM 2 distribution scheme.
Java Remote Object Invocation (RMI) Overview of RMI Java RMI allowed programmer to execute remote function class using the same semantics as local functions.
Remote Method Invocation
Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.
1 HANDOUT 14 Remote Method Invocation (RMI) BY GEORGE KOUTSOGIANNAKIS THIS DOCUMENT CAN NOT BE REPRODUCED OR DISTRIBUTED WITHOUT TH E WRITTEN PERMISSION.
Sockets  Defined as an “endpoint for communication.”  Concatenation of IP address + port.  Used for server-client communication.  Server waits for.
EEC-681/781 Distributed Computing Systems Lecture 5 Wenbing Zhao Department of Electrical and Computer Engineering Cleveland State University
Introduction to Remote Method Invocation (RMI)
Java Remote Object Invocation (RMI) Overview of RMI Java RMI allowed programmer to execute remote function class using the same semantics as local functions.
How Does Remote Method Invocation Work? –Systems that use RMI for communication typically are divided into two categories: clients and servers. A server.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
Java RMI RMI = Remote Method Invocation. Allows Java programs to invoke methods of remote objects. Only between Java programs. Several versions (JDK-1.1,
1 Java Programming II Java Network II (Distributed Objects in Java)
CS 584 Lecture 18 l Assignment » Glenda assignment extended to the Java RMI Deadline » No Java RMI Assignment l Test » Friday, Saturday, Monday.
+ A Short Java RMI Tutorial Usman Saleem
Presentation: RMI Continued 2 Using The Registry & Callbacks.
1 Java RMI G53ACC Chris Greenhalgh. 2 Contents l Java RMI overview l A Java RMI example –Overview –Walk-through l Implementation notes –Argument passing.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 RMI.
Silberschatz, Galvin, and Gagne  1999 Applied Operating System Concepts Chapter 15: Distributed Communication Sockets Remote Procedure Calls (RPCs) Remote.
Java Remote Method Invocation RMI. Idea If objects communicate with each other on one JVM why not do the same on several JVM’s? If objects communicate.
Remote Method Invocation onlineTraining/rmi/RMI.html.
RMI Continued IS Outline  Review of RMI  Programming example.
RMI remote method invocation. Traditional network programming The client program sends data to the server in some intermediary format and the server has.
RMI Remote Method Invocation Distributed Object-based System and RPC Together 2-Jun-16.
Presentation: RMI Continued 2 Using The Registry & Callbacks.
Java Remote Object Invocation (RMI) Overview of RMI Java RMI allowed programmer to execute remote function class using the same semantics as local functions.
 Remote Method Invocation  A true distributed computing application interface for Java, written to provide easy access to objects existing on remote.
Fall 2007cs4251 Distributed Computing Umar Kalim Dept. of Communication Systems Engineering 17/10/2007.
Li Tak Sing COMPS311F. RMI callbacks In previous example, only the client can initiate a communication with the server. The server can only response to.
Java Remote Method Invocation (RMI) Overview of RMI Java RMI allowed programmer to execute remote function class using the same semantics as local functions.
Presentation: RMI Continued 2 Using The Registry & Callbacks.
 Java RMI Distributed Systems IT332. Outline  Introduction to RMI  RMI Architecture  RMI Programming and a Sample Example:  Server-Side RMI programming.
Remote Method Invocation A Client Server Approach.
UMBC Distributed Computing with Objects RMI/Corba CMSC 432 Shon Vick.
Remote Method Invocation RMI architecture stubs and skeletons for remote services RMI server and client in Java Creating an RMI Application step-by- step.
1 RMI Russell Johnston Communications II. 2 What is RMI? Remote Method Invocation.
Khoa CNTT 1/37 PHẠM VĂN TÍNH   Java RMI (Remote Method Invocation)
1 Lecture 15 Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
CSC 480 Software Engineering Lab 6 – RMI Nov 8, 2002.
Netprog Java RMI1 Remote Method Invocation.
Using RMI The Example of A Remote Calculator 1. Parts of A Working RMI System A working RMI system is composed of several parts. –Interface definitions.
RMI1 Remote Method Invocation Adapted from “Core Java 2” by Cay Horstmann.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 29 Remote Method.
Principles of Software Development
Java Remote Method Invocation (RMI)
RMI Packages Overview java.rmi java.rmi.server java.rmi.registry
Remote Method Invocation
What is RMI? Remote Method Invocation
Remote Method Invocation
Lesson 3 Remote Method Invocation (RMI) Mixing RMI and sockets
Knowledge Byte In this section, you will learn about:
Network and Distributed Programming in Java
Creating a Distributed System with RMI
Chapter 40 Remote Method Invocation
Creating a Distributed System with RMI
Remote Method Invocation
Creating a Distributed System with RMI
Chapter 46 Remote Method Invocation
Using RMI -The Example of A Remote Calculator
Chapter 46 Remote Method Invocation
CS 584 Lecture 18 Assignment Glenda assignment extended to the Java RMI Deadline No Java RMI Assignment Test Friday, Saturday, Monday.
Java Remote Method Invocation
Remote Method Invokation: Conceptual model
Creating a Distributed System with RMI
Java Chapter 5 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Remote Method Invokation Distributed Objects Remote Method Invokation

Distributed Systems

Object Oriented Paradigm invoke method Object 1 Object 2 respond

Distributed Object Oriented Paradigm Client Host/Process Server Host/Process invoke method Object 1 Object 2 respond

The RMI model

Distributed object applications need to: Locate remote objects. Applications can use various mechanisms to obtain references to remote objects. For example, an application can register its remote objects with RMI's simple naming facility, the RMI registry. Alternatively, an application can pass and return remote object references as part of other remote invocations. Communicate with remote objects. Details of communication between remote objects are handled by RMI. To the programmer, remote communication looks similar to regular Java method invocations. Load class definitions for objects that are passed around. Because RMI enables objects to be passed back and forth, it provides mechanisms for loading an object's class definitions as well as for transmitting an object's data.

Remote Method Invokation: How does it work? The conceptual model Distributed Objects Remote Method Invokation: How does it work? The conceptual model

Distributed Object Oriented: implementation Local – Client Host/Process Remote - Server Host/Process Object 1 Object 2 f() f() “Post Office” “Post Office” socket interaction

Distributed Object Oriented: RMI paradigm Local – Client Host/Process Remote- Server Host/Process Object 1 Object 2 f() f() Stub of Object 2 Skeleton of Object 2 magic

Distributed Object Oriented: RMI paradigm Local – Client Host/Process Remote- Server Host/Process Interface . Object 1 Object 2 f() f() f() Stub of Object 2 Skeleton of Object 2 magic

A “do it yourself” implementation Distributed Objects A “do it yourself” implementation

A “do it yourself” implementation 1. Person: the interface package distributedobjectdemo; public interface Person { public int getAge() throws Throwable; public String getName() throws Throwable; }

A “do it yourself” implementation 2. Person: The class package distributedobjectdemo; public class PersonServer implements Person{ int age; String name; public PersonServer(String name,int age){ this.age=age; this.name=name; } public int getAge(){ return age; public String getName(){ return name; public static void main(String a[]) { PersonServer person = new PersonServer("Marko", 45); Person_Skeleton skel = new Person_Skeleton(person); skel.start(); System.out.println("server started");

A “do it yourself” implementation 3. Person: the skeleton package distributedobjectdemo; import java.net.Socket; import java.net.ServerSocket; import java.io.*; public class Person_Skeleton extends Thread { PersonServer myServer; int port=9000; public Person_Skeleton(PersonServer server) { this.myServer=server; } // la classe continua…

A “do it yourself” implementation 3. Person: the skeleton public void run(){ Socket socket = null; ServerSocket serverSocket=null; try { serverSocket=new ServerSocket(port); } catch (IOException ex) { System.err.println("error while creating serverSocket"); ex.printStackTrace(System.err); System.exit(1); while (true) { socket=serverSocket.accept(); System.out.println("Client opened connection"); System.err.println("error accepting on serverSocket"); // il metodo continua…

A “do it yourself” implementation 3. Person: the skeleton try { while (socket!=null){ ObjectInputStream instream= new ObjectInputStream(socket.getInputStream()); String method=(String)instream.readObject(); if (method.equals("age")) { int age=myServer.getAge(); ObjectOutputStream outstream= new ObjectOutputStream(socket.getOutputStream()); outstream.writeInt(age); outstream.flush(); } else if (method.equals("name")) { String name=myServer.getName(); outstream.writeObject(name); } //prosegue con il catch…

A “do it yourself” implementation 3. Person: the skeleton } catch (IOException ex) { if (ex.getMessage().equals("Connection reset")) { System.out.println("Client closed connection"); } else { System.err.println("error on the network"); ex.printStackTrace(System.err); System.exit(2); } } catch (ClassNotFoundException ex) { System.err.println("error while reading object from the net"); ex.printStackTrace(System.err); System.exit(3); }//fine del ciclo while(true) } //fine del metodo run } //fine della classe

A “do it yourself” implementation 4. Person: the stub package distributedobjectdemo; import java.net.Socket; import java.io.*; public class Person_Stub implements Person { Socket socket; String machine="localhost"; int port=9000; public Person_Stub() throws Throwable { socket=new Socket(machine,port); } protected void finalize(){ System.err.println("closing"); try { socket.close(); } catch (IOException ex) {ex.printStackTrace(System.err); } // la classe continua…

A “do it yourself” implementation 4. Person: the stub public int getAge() throws Throwable { ObjectOutputStream outstream= new ObjectOutputStream(socket.getOutputStream()); outstream.writeObject("age"); outstream.flush(); ObjectInputStream instream= new ObjectInputStream(socket.getInputStream()); return instream.readInt(); } public String getName() throws Throwable { ObjectOutputStream outstream=new ObjectOutputStream(socket.getOutputStream()); outstream.writeObject("name"); return (String)instream.readObject(); } // fine della classe

A “do it yourself” implementation 5. Person: the client package distributedobjectdemo; public class Client { public Client() { try { Person person=new Person_Stub(); int age=person.getAge(); String name=person.getName(); System.out.println(name+" is "+age+" years old"); } catch (Throwable ex) { ex.printStackTrace(System.err); public static void main(String[] args) { Client client1 = new Client();

Open issues multiple instances Automatic stub and skeleton generation on demand server dentification on demand remote class activation Client Broker Server Registry

An RMI basic implementation Distributed Objects An RMI basic implementation (example taken from https://www.mkyong.com)

Remote Interface 1. Define the common interface package it.unitn.rmiinterface; import java.rmi.Remote; import java.rmi.RemoteException; public interface RMIInterface extends Remote { public String helloTo(String name) throws RemoteException; }

The Server 2. Implement the service package it.unitn.rmiserver; import java.rmi.Naming; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; import it.unitn.RMIInterface; The Server 2. Implement the service public class ServerOperation extends UnicastRemoteObject implements RMIInterface{ private static final long serialVersionUID = 1L; protected ServerOperation() throws RemoteException { super(); } @Override public String helloTo(String name) throws RemoteException{ System.err.println(name + " is trying to contact!"); return "Server says hello to " + name;

The Server 4. Register yourself 3. Create Registry public static void main(String[] args){ try { Naming.rebind("//localhost/MyServer", new ServerOperation()); System.err.println("Server ready"); } catch (Exception e) { System.err.println("Server exception: " + e.toString()); e.printStackTrace(); } 4. Register yourself 3. Create Registry import java.rmi.registry.*; LocateRegistry.createRegistry(1099);

The Server 4. Register yourself public static void main(String[] args){ try { Naming.rebind("//localhost/MyServer", new ServerOperation()); System.err.println("Server ready"); } catch (Exception e) { System.err.println("Server exception: " + e.toString()); e.printStackTrace(); } 4. Register yourself

The client 5. Lookup The Service 6. Use Service package it.unitn.rmiclient; import java.net.MalformedURLException; import java.rmi.Naming; import java.rmi.NotBoundException; import java.rmi.RemoteException; import javax.swing.JOptionPane; import it.unitn.RMIInterface; The client public class ClientOperation { private static RMIInterface remoteObj; public static void main(String[] args) throws MalformedURLException, RemoteException, NotBoundException { remoteObj = (RMIInterface) Naming.lookup("// localhost/MyServer"); String txt = JOptionPane.showInputDialog("What is your name?"); String response = remoteObj.helloTo(txt); JOptionPane.showMessageDialog(null, response); } 5. Lookup The Service 6. Use Service

Deploy COMPILE: javac src/it/unitn/rmiinterface/RMIInterface.java src/it/unitn/rmiserver/ServerOperation.java src/it/unitn/rmiclient/ClientOperation.java START REGISTRY cd src start rmiregistry START SERVER java it.unitn.rmiserver.ServerOperation START CLIENT java it.unitn.rmiclient.ClientOperation

Running

Question What do you have to change in the process and in the code if you run on different machines?

An RMI implementation - VERY IMPORTANT NOTES- Distributed Objects An RMI implementation - VERY IMPORTANT NOTES-

VERY IMPORTANT: Parameter passing Java Standard: void f(int x) : Parameter x is passed by copy void g(Object k) : Parameter k and return value are passed by reference Java RMI: void h(Object k) : Parameter k is passed by copy! UNLESS k is a REMOTE OBJECT (in which case it is passed as a REMOTE REFERENCE, i.e. its stub is copied if needed)

IMPORTANT: Parameter passing Passing By-Value When invoking a method using RMI,all parameters to the remote method are passed by-value .This means that when a client calls a server,all parameters are copied from one machine to the other. Passing by remote-reference If you want to pass an object over the network by-reference,it must be a remote object, and it must implement java.rmi.Remote.A stub for the remote object is serialized and passed to the remote host. The remote host can then use that stub to invoke callbacks on your remote object. There is only one copy of the object at any time,which means that all hosts are calling the same object.

Serialization Any basic primitive type (int,char,and so on) is automatically serialized with the object and is available when deserialized. Java objects can be included with the serialized or not: Objects marked with the transient keyword are not serialized with the object and are not available when deserialized. Any object that is not marked with the transient keyword must implement java.lang.Serializable .These objects are converted to bit-blob format along with the original object. If your Java objects are neither transient nor implement java.lang.Serializable, a NotSerializable Exception is thrown when writeObject()is called.

Serialization All serializable classes must declare a private static final field named serialVersionUID to guarantee serialization compatibility between versions. If no previous version of the class has been released, then the value of this field can be any long value, as long as the value is used consistently in future versions. private static final long serialVersionUID = 227L;

When not to Serialize The object is large. Large objects may not be suitable for serialization because operations you do with the serialized blob may be very intensive. (one could save the blob to disk or transporting the blob across the network) The object represents a resource that cannot be reconstructed on the target machine.Some examples of such resources are database connections and sockets. The object represents sensitive information that you do not want to pass in a serialized stream..

An RMI implementation - Addendum - Distributed Objects An RMI implementation - Addendum -

RMI-IIOP RMI-IIOP is a special version of RMI that is compliant with CORBA. RMI has some interesting features not available in RMI-IIOP,such as distributed garbage collection, object activation and downloadable class files. EJB and J2EE mandate that you use RMI-IIOP, not RMI. rmic –iiop generates IIOP stub and tie (instead of stub and skeleton) rmic –idl generates OMG IDL See docs.oracle.com/javase/7/docs/technotes/tools/#rmi

Preparing and executing NOTES: starting from Java 2 the skeleton may not exist (its functionality is absorbed by the class file). Starting from Java 5 the rmic functionality has been absorbed by javac, so the whole process becomes transparent (but even more misterious…) See docs.oracle.com/javase/tutorial/rmi/ for an example of current usage of rmi

Preparing and executing - security The JDK security model requires code to be granted specific permissions to be allowed to perform certain operations. You need to specify a policy file when you run your server and client. grant { permission java.net.SocketPermission "*:1024-65535", "connect,accept"; permission java.io.FilePermission "c:\\…path…\\", "read"; }; java -Djava.security.policy=java.policy executableClass

Access to system properties Nota: instead of specifìying a property at runtime (-D switch of java command), You can hardwire the property into the code: -Djava.security.policy=java.policy System.getProperties().put( "java.security.policy", "java.policy");