14
15 - RMI

15 - RMI. Java RMI Architecture Example Deployment RMI is a part of J2SE (standard edition), but is used by J2EE) A J2EE server is not nessesary for using

Embed Size (px)

Citation preview

Page 1: 15 - RMI. Java RMI Architecture Example Deployment RMI is a part of J2SE (standard edition), but is used by J2EE) A J2EE server is not nessesary for using

15 - RMI

Page 2: 15 - RMI. Java RMI Architecture Example Deployment RMI is a part of J2SE (standard edition), but is used by J2EE) A J2EE server is not nessesary for using

Java RMI

Architecture

Example

Deployment

RMI is a part of J2SE (standard edition), but is used by J2EE)A J2EE server is not nessesary for using RMI

Page 3: 15 - RMI. Java RMI Architecture Example Deployment RMI is a part of J2SE (standard edition), but is used by J2EE) A J2EE server is not nessesary for using

3NOEA 2009Java-kursus – RMI

Communikation between different machines

• Sockets, RPC and RMI– For sockets a common protocol on application level is

needed to encode and decode sent and received messages– RPC (Remote Procedure Call) pulls the abstraction level for

communication up ti procedural level – RMI (Remote Method Invocation) handles communication

between objects in different adress spaces.

Page 4: 15 - RMI. Java RMI Architecture Example Deployment RMI is a part of J2SE (standard edition), but is used by J2EE) A J2EE server is not nessesary for using

4NOEA 2009Java-kursus – RMI

Distributed Object-application (DOA)

• A application, where the server provides remote objects on which methods might be activated from different clients, is called a hvis metoder kan kaldes fra forskellige klienter ”distributed object-application”

• DOA needs to:– Localize remote objects– Communicate with remote objects– Handle byte code for objects that are send as a parameter

or a return value• RMI can handle this

Page 5: 15 - RMI. Java RMI Architecture Example Deployment RMI is a part of J2SE (standard edition), but is used by J2EE) A J2EE server is not nessesary for using

5NOEA 2009Java-kursus – RMI

Architecture

Page 6: 15 - RMI. Java RMI Architecture Example Deployment RMI is a part of J2SE (standard edition), but is used by J2EE) A J2EE server is not nessesary for using

6NOEA 2009Java-kursus – RMI

Example for building a RMI-application

• The steps:

1. Contruct an interface (remote)

2. Implement the interface

3. Create a server class

4. Create a client class

5. Compile it (javac og rmic)

6. Run the application start rmi-registry (not nessesary from java 1.5), server and klient

Page 7: 15 - RMI. Java RMI Architecture Example Deployment RMI is a part of J2SE (standard edition), but is used by J2EE) A J2EE server is not nessesary for using

7NOEA 2009Java-kursus – RMI

Oprettelse af interface

import java.rmi.*;

public interface Hello extends Remote{

public String getGreeting() throws RemoteException;}

• Java.rmi.Remote is a interface that all RMI-application must inherit from

• Java.rmi.RemoteException is the superclass for exceptions that RMI can throw and shall always be handled

Page 8: 15 - RMI. Java RMI Architecture Example Deployment RMI is a part of J2SE (standard edition), but is used by J2EE) A J2EE server is not nessesary for using

8NOEA 2009Java-kursus – RMI

Implementation af interfacet

import java.rmi.*;

import java.rmi.server.*;

public class HelloImpl extends UnicastRemoteObject implements Hello{

public HelloImpl() throws RemoteException

{

//Default constructor is implemented because of RemoteException

}

public String getGreeting() throws RemoteException

{

return ("Hello there!");

}

}

Page 9: 15 - RMI. Java RMI Architecture Example Deployment RMI is a part of J2SE (standard edition), but is used by J2EE) A J2EE server is not nessesary for using

9NOEA 2009Java-kursus – RMI

Server class

//Server.import java.rmi.*;public class HelloServer{

private static final String HOST = "localhost";

public static void main(String[] args) throws Exception{HelloImpl temp = new HelloImpl();String rmiObjectName = "rmi://" + HOST + "/Hello";//Could omit host name, since 'localhost' would be//assumed by default.

Naming.rebind(rmiObjectName,temp);System.out.println("Binding complete...\n");

}}

Page 10: 15 - RMI. Java RMI Architecture Example Deployment RMI is a part of J2SE (standard edition), but is used by J2EE) A J2EE server is not nessesary for using

10NOEA 2009Java-kursus – RMI

Client classimport java.rmi.*;public class HelloClient{

private static final String HOST = "localhost";public static void main(String[] args){

try{Hello greeting = (Hello)Naming.lookup("rmi://" +

HOST + "/Hello");System.out.println("Message received: " +

greeting.getGreeting()); }

catch(ConnectException conEx){System.out.println("Unable to connect to server!");

}catch(Exception e){

e.printStackTrace();}

}}

Page 11: 15 - RMI. Java RMI Architecture Example Deployment RMI is a part of J2SE (standard edition), but is used by J2EE) A J2EE server is not nessesary for using

11NOEA 2009Java-kursus – RMI

From JDK 1.5 this is done automaticly

Generate the stub

• We still missing the piece of code that handles the communication.

• It is placed in the file HelloImpl_Stub The file is generated by running rmic.exe på impl-filen:rmic –v1.2 HelloImpl

Page 12: 15 - RMI. Java RMI Architecture Example Deployment RMI is a part of J2SE (standard edition), but is used by J2EE) A J2EE server is not nessesary for using

12NOEA 2009Java-kursus – RMI

How does it function? - Deployment

• To make the client run: the HelloClient, Hello and HelloImpl_stubmust be present on the client

• HelloServer, Hello, HelloImpl and HelloImpl_stub must be present on the server

• The stub is a proxy class for the remote-object. The stub takes care of the communication between the client and the server by marshalling/unmarshalling

• The client knows how to manipulate the server object because yhe server object implements the interface that is also known on the client

Page 13: 15 - RMI. Java RMI Architecture Example Deployment RMI is a part of J2SE (standard edition), but is used by J2EE) A J2EE server is not nessesary for using

13NOEA 2009Java-kursus – RMI

RMIRegistry

• Rmiregistry is a program that handles the nameservice. • Registry shall be started before the server is started

(This is done automatically in jdk1.5+)• The rebind-method of the server binds a name (URL) to the

implementation object on the server.• The client can use the lookup method to get a reference to

the impl-object

• Add your server files to classpath in order for rmiregistry to find them.

• Rmiregistry is started from the command promt by: start rmiregistry

Page 14: 15 - RMI. Java RMI Architecture Example Deployment RMI is a part of J2SE (standard edition), but is used by J2EE) A J2EE server is not nessesary for using

14NOEA 2009Java-kursus – RMI

Call semantics

• When a method is called on a remote object, there is two ways of passing the parameters dependent of whether the parameter (and the return value) is a remote object or not.

• If remote then is call-by-reference• If not then it is call-by-value (copy)• Objects that are sent by copy shall implement the interface

Serializable