Lab 2 : Java RMI. request sayhello() Hello interface remote object. local object. response "Hello world"

Similar documents
JAVA RMI. Remote Method Invocation

RMI (Remote Method Invocation) Over the year, there have been 3 different approaches to application development:

CSci Introduction to Distributed Systems. Communication: RPC In Practice

Remote Objects and RMI

CS 5523 Operating Systems: Remote Objects and RMI

IBD Intergiciels et Bases de Données

Distributed Computing

Generic architecture

Remote Method Invocation

Remote Method Invocation in Java

Distributed Systems. 5. Remote Method Invocation

Last Class: Network Overview. Today: Distributed Systems

Distributed Systems. Distributed Object Systems 2 Java RMI. Java RMI. Example. Applet continued. Applet. slides2.pdf Sep 9,

Written by: Dave Matuszek

JAVA RMI Java, summer semester

Distributed object component middleware I - Java RMI

Distributed object component middleware I - Java RMI

Steps to Demonstrate CORBA Application using Java

RMI. Remote Method Invocation. 16-Dec-16

Department of Computer Science & Engineering. M.Tech(CSE)-I Year-II Semester WEB SERVICES AND SERVICE ORIENTED ARCHITECHTURE (B1513) Mr.K.

presentation DAD Distributed Applications Development Cristian Toma

5.4. Events and notifications

Communication Basics, RPC & RMI. CS403/534 Distributed Systems Erkay Savas Sabanci University

03 Remote invocation. Request-reply RPC. Coulouris 5 Birrel_Nelson_84.pdf RMI

1 interface TemperatureSensor extends java.rmi.remote 2 { 3 public double gettemperature() throws java.rmi.remoteexception; 4 public void

DISTRIBUTED OBJECTS AND REMOTE INVOCATION

IJESRT. http: //

PART VII Building Web Services With JAX-RPC. 7.5 JAX Web Service Architecture. Development of a Web Service with JAX. Runtime View of a Web Service

EAST WEST UNIVERSITY

COMP 6231: Distributed System Design

Remote Method Invocation

Remote Procedure Call

Distributed Systems. 6. Remote Method Invocation. Werner Nutt

6 Distributed Object-Based Systems

Firewall Issues. The possible scenarios: the RMI client, the server, or both can be operating from behind a firewall

Contents. Java RMI. Java RMI. Java RMI system elements. Example application processes/machines Client machine Process/Application A

Lecture 18 Inside Java RMI

Object Interaction. Object Interaction. Introduction. Object Interaction vs. RPCs (2)

Distributed Objects. Remote Method Invokation

Concurrent Programming (RIO) Lesson 9. Ch 8 [BenA 06] Messages Channels Rendezvous RPC and RMI. X=f(..); send X to B. OS kernel.

JAC444 - Lecture 11. Remote Method Invocation Segment 2 - Develop RMI Application. Jordan Anastasiade Java Programming Language Course

Advanced Java Programming

Concurrency Control in Distributed Environment

Introduction & RMI Basics. CS3524 Distributed Systems Lecture 01

Verteilte Systeme (Distributed Systems)

Chapter 4 Remote Procedure Calls and Distributed Transactions

Agent and Object Technology Lab Dipartimento di Ingegneria dell Informazione Università degli Studi di Parma. Distributed and Agent Systems RMI

CS193k, Stanford Handout #12. Threads 4 / RMI

Communication and Distributed Processing

Lab 1 : Java Sockets

Desarrollo de Aplicaciones en Red RMI. Introduction. Considerations. Considerations. RMI architecture

Course Snapshot. The Next Few Classes. Parallel versus Distributed Systems. Distributed Systems. We have covered all the fundamental OS components:

Session plan. sessionx. Desarrollo de Aplicaciones en Red. What s Corba? RPC vs. Corba. Middleware. Middleware task

Java WebStart, Applets & RMI

Remote Method Invocation. Benoît Garbinato

Remote Method Invocation Benoît Garbinato

RPC flow. 4.3 Remote procedure calls IDL. RPC components. Procedure. Program. sum (j,k) int j,k; {return j+k;} i = sum (3,7); Local procedure call

Course Snapshot. The Next Few Classes

Grid Computing. Java Remote Method Invocation (RMI) RMI Application. Grid Computing Fall 2006 Paul A. Farrell 9/5/2006

Communication and Distributed Processing

Distributed Systems COMP 212. Lecture 10 Othon Michail

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

RMI. (Remote Method Invocation)

RMI Example RMI. CmpE 473 Internet Programming RMI

CC755: Distributed and Parallel Systems

CS555: Distributed Systems [Fall 2017] Dept. Of Computer Science, Colorado State University

Outline. Chapter 4 Remote Procedure Calls and Distributed Transactions. Remote Procedure Call. Distributed Transaction Processing.

5 Distributed Objects: The Java Approach

Network. Dr. Jens Bennedsen, Aarhus University, School of Engineering Aarhus, Denmark

55:182/22C:182. Distributed Application Frameworks Java RMI, CORBA, Web Services (SOAP)

Developing RMI Based Server (ChatServer)

Activation of remote objects

Distributed Programming with RMI. Overview CORBA DCOM. Prepared By: Shiba R. Tamrakar

Distributed Applications Programming. Lab 4

Distributed Objects and Remote Invocation. Programming Models for Distributed Applications

How are classes loaded into the Java Virtual Machine (JVM)? from the local file system (CLASSPATH). by an instance of ClassLoader

EDA095 Remote Method Invocation

Questions and Answers. A. RMI allows us to invoke a method of java object that executes on another machine.

The UNIVERSITY of EDINBURGH. SCHOOL of INFORMATICS. CS4/MSc. Distributed Systems. Björn Franke. Room 2414

Distributed Programming in Java. Distribution (2)

Activation of remote objects

EDA095 Remote Method Invocation

Distributed Systems. 02r. Java RMI Programming Tutorial. Paul Krzyzanowski TA: Long Zhao Rutgers University Fall 2017

Reflection/RMI 4/28/2009

Java RMI Activation: A running example We have the following classes: MyRemoteInterface: the remote interface. Client: the client that invokes a

Remote Method Invocation

Bharati Vidyapeeth s Institute of Computer Applications and Management A-4, Paschim Vihar, New Delhi-63.

The Common Object Request Broker Architecture (CORBA)

Concurrent Object-Oriented Programming

Component-Based Software Engineering

Sockets and RMI. CS151 Chris Pollett Dec. 5, 2005.

Chapter 4: Processes. Process Concept. Process State

Lecture 15: Frameworks for Application-layer Communications

Lecture 15: Frameworks for Application-layer Communications

Modulo II Socket, RMI e Corba

CSS 533 Program 3: Mobile-Agent Execution Platform Professor: Munehiro Fukuda Due date: see the syllabus

Web Technology for IE 20 November ISE 582: Information Technology for Industrial Engineering

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Applications. RMI, RPC and events. Request reply protocol External data representation. Operating System

Lecture VI: Distributed Objects. Remote Method Invocation

Transcription:

Lab 2 : Java RMI 1. Goals In this lab you will work with a high-level mechanism for distributed communication. You will discover that Java RMI provides a mechanism hiding distribution in OO programming. Java RMI does provide: - location transparency - naming transparency - programming support for complex data exchange - programming support for application-level protocols Java RMI does not provide failure transparency. 2. Official documentation [1] https://docs.oracle.com/javase/tutorial/rmi/ [2] http://docs.oracle.com/javase/7/docs/technotes/guides/rmi/hello/hello-world.html [3] http://www.oracle.com/technetwork/java/javase/overview/index-jsp-136424.html 3. The RMI Model RMI follows the client-server model. The server provides access to remote objects which implement well-defined interfaces. A client may obtain the reference to a remote object and call it using the interface. In the picture below, the server provides a service (remote object) implementing the Hello interface. The client may call the sayhello() method if it gets the reference to the remote object. The object will return its response. local object request sayhello() Hello interface remote object client response "Hello world" server To obtain the reference of a remote object, a client may use the RMI naming service (rmiregistry) or receive it as a result of a method call to another remote object.!1

4. Exercice : Run and Understand the HelloWorld Example The goal of this exercice is to make you discover the basic principles of Java RMI and make you succeed in running a Java RMI application. 3.1 The Remote Interface In your favorite IDE, define the remote interface Hello. Hello.java 1. public interface Hello extends Remote { 2. 3. // A method provided by the 4. // remote object 5. public String sayhello() throws RemoteException; 6. 7. Pay attention to the fact that the interface needs to extend the Remote interface and that each remotely-accessible method is to throw a RemoteException. If you do not follow these rules, you cannot benefit from the RMI framework and from the automatic management for the distributed communication between the client and the server. 3.2 Implementation of the remote object Create a class that implements the remote interface. It will provide the implementation of the service that will be mad accessible by the server. HelloImpl.java 1. import java.rmi.*; 2. 3. public class HelloImpl implements Hello { 4. 5. private String message; 6. 7. public HelloImpl(String s) { 8. message = s ; 9. 10. 11. public String sayhello() throws RemoteException { 12. return message ; 13. 14.!2

3.3 The server - The server creates the object that implements the service in line 11. - In line 12, it obtains the remote reference of the object. This reference is needed by the clients to communicate with the object. As you see, we do not manipulate explicitly the IP address of the physical machine, nor the port. The exportobject method takes care of all the underlying details (actually, there will be a socket and a port allocated, but RMI hides this from us). The value 0 for the port parameter says that an available port will be used. - To help clients obtain the reference of the newly created object, the server puts the reference in the Java naming service (the registry). The server obtains a reference to the registry (line 15) and registers the reference of the object under a given name (line 16). For this to work, the registry should already run. If LocateRegistry is used without parameters, the registry would be looked up at the localhost. Otherwise, it is possible to connect to a distant registry via getregistry(string host, int port). HelloServer.java 1. import java.rmi.*; 2. import java.rmi.server.*; 3. import java.rmi.registry.*; 4. 5. public class HelloServer { 6. 7. public static void main(string [] args){ 8. 9. try { 10. // Create a Hello remote object 11. HelloImpl h = new HelloImpl ("Hello world!"); 12. Hello h_stub = (Hello) UnicastRemoteObject.exportObject(h, 0); 13. 14. // Register the remote object in RMI registry with a given identifier 15. Registry registry= LocateRegistry.getRegistry(); 16. registry.bind("helloservice, h_stub); 17. 18. System.out.println ("Server ready"); 19. 20. catch (Exception e) { 21. System.err.println("Error on server :" + e) ; 22. e.printstacktrace(); 23. 24. 3.3 The client - The client locates the registry (line 13). This should be the registry used by the server to register the hello service.!3

- The client searches for the hello service using the registry and using a predefined name (line 14) - If the reference is obtained, the client may call the remote object using the standard method call syntax. In other words, for this part, the distribution is transparent for the developer. HelloClient.java 1. import java.rmi.*; 2. import java.rmi.registry.*; 3. 4. public class HelloClient { 5. public static void main(string [] args) { 6. try { 7. if (args.length < 1) { 8. System.out.println("Usage: java HelloClient <rmiregistry host>"); 9. return; 10. 11. String host = args[0]; 12. // Get remote object reference 13. Registry registry = LocateRegistry.getRegistry(host); 14. Hello h = (Hello) registry.lookup("helloservice"); 15. // Remote method invocation 16. String res = h.sayhello(); 17. System.out.println(res); 18. 19. catch (Exception e) { 20. System.err.println("Error on client: " + e) ; 21. 3.4 Run the application - Compile the sources - Define the CLASSPATH variable. The classpath gives the location(s) of the class files to be used by java. The easiest is to define it in a global manner, otherwise you can use the -cp option when launching the java command. - Run the rmiregistry rmiregistry & - Run the server java HelloServer or java -cp your classpath HelloServer - Run the client java HelloClient localhost!4

5. Exercice : Modify the Hello Application The goal of this exercice is to make you discover: - that RMI references may be passed as parameters (hence also received as results) - that clients may also be servers Goal Each time the sayhello() method is called on the server side, the server will print a message giving the identity of the client that called the method. Design discussion There are two alternatives: 1) The client provides its name (a String) each time it calls the sayhello() method. This will imply to change the Hello interface in the following way: public interface Hello extends Remote { public String sayhello(string clientname) throws RemoteException; 2) The server contacts the client to ask it for its name. This means that the server makes a remote call to the client. In RMI, the only possible way of doing this is that the client provides a remote object implementing a remote interface. The server should obtain a reference to this object to be able to call it. This reference may be passed to the server by the client itself (i.e this reference does not need to be put in the Java registry). Your turn! 1) Implement the first solution 2) Implement the second solution - Define a remote interface Info_itf for the client public interface Info_itf extends Remote { public String getname() throws RemoteException; - Modify the client to implement the Info_itf. - Modify the Hello interface to become public interface Hello extends Remote { public String sayhello(info_itf client) throws RemoteException; - Change the implementation of the Hello interface to call the client and print the message about its identity. 3) Compare the solutions in terms of distributed communications!5

6. Exercice : Continue with the Hello Application The goal of this exercice is to make you discover: - that servers may provide access to different services (different interfaces) Goal We want now that the server informs the client when it has reached a certain number of calls of the sayhello() method. For example, the server may notify the client that it has reached the limit of 10 calls, then 20 calls, etc. Design discussion The server should be able to : - call a client to inform it about the number of calls it has made. - distinguish between clients (the server may have multiple clients) so as to count the number of calls per client. This requires that, at each call of the the sayhello method, the clients should identify themselves. Your turn! Modify the Hello application in the following way : - Define a remote interface Accounting_itf for the client public interface Accounting_itf extends Remote { public void numberofcalls(int number) throws RemoteException; - Change the client to implement this interface - Define a new remote interface for the server Registry_itf public interface Registry_itf extends Remote { public void register(accounting_itf client) throws RemoteException; This interface is to be used by a new client of the service Hello. The client will thus present itself to the server. The server should manage somehow the list of clients it knows. - Provide the implementation of the interface at the server side. - Provide a Hello2 interface in which the sayhello() method takes as a parameter a Accounting_itf type. The method should not be executed if the client has not registered itself. - Provide the implementation of the Hello2 interface to count the number of calls in the sayhello method - Make the server bind to the registry the three services : Hello, Hello2 and Registry_itf - Test the application. Note that with this version the client can obtain the references to Hello and Hello2 and use two different implementations of the service.!6

7. Exercice : The Chat Application The goal of this exercice is to make you practice the architectural design of an RMI application. Contrary to the previous examples, it will be up to you to define the entities, their roles and their interfaces. Your task is to build a chat application. There are four stages that are proposed to you: a) build a chat system, where participants can dynamically join, leave, exchange messages b) add a history feature to the chat system. This means that the server «remembers» all the messages that have been exchanges since the start of the system and that a new client can consult this history. c) make the chat system persistent (persistent history of messages). This means that if the server shuts down and restarts, it can recover the message history. d) implement a graphical user interface for the chat system. This feature is for the ones that find RMI really easy, have plenty of time to do other stuff, are tired of the textual interface of the chat system and enjoy graphics!!7