Monday, March 2, 2009

NETWORKING

Remote Procedures
In the previous example, the client communicated to the server over a socket connection using a protocol known to both parties
But both the client and server had to be aware of the socket level details
Wouldn't it be nice if even these details were abstracted away and the request to the server looked like a local procedure call from the viewpoint of the client?
That's the idea behind a Remote Procedure Call (RPC), a technology introduced in the late 1970's
• Two RPC specifications:
c) SUN's Open Network Computing (ONC) RPC
c) OSF's Distributed Computing Environment (DCE) RPC

Design Patterns In Java

Distributed Object Technology
But RPC is not object-oriented. In the 00 world, we'd like to have distributed objects and remote method calls.
While there are many Distributed Object Technologies available
today, three are widely available: ¢ RMI
¢ CORBA ¢ SOAP
• Remote Method Invocation (RMI) ¢ Developed by SUN
¢ Available as part of the core Java API ¢ Java-centric
¢ Object interfaces defined as Java interfaces ¢ Uses object serialization

Design Patterns In Java

Remote Method Invocation 12

Bob Tarr
Distributed Object Technology
• Common Object Request Broker Architecture (CORBA) c) Developed by the Object Management Group (OMG)
c) Language and platform independent
c) Object interfaces defined in an Interface Definition Language (IDL)
An Object Request Broker (ORB) facilitates the client-server request/response action
ORBs communicate via a binary protocol called the Internet Inter-ORB Protocol (IIOP)
• SOAP
c) Simple Object Access Protocol c) XML-Based
c) Developed from an earlier spec called XML-RPC c) Standardized by the W3C
c) Many implementations availablt

Design Patterns In Java

Remote M d hod Invocation 13

Bob Tarr
RMI
Provides a distributed object capability for Java applications
Allows a Java method to obtain a reference to a remote object and invoke methods of the remote object nearly as easily as if the remote object existed locally
The remote object can be in another JVM on the same host or on different hosts across the network
Uses object serialization to marshal and unmarshal method arguments
Supports the dynamic downloading of required class files across the network

Design Patterns In Java

Remote Method Invocation 14

Bob Tarr
RMI
Provides a distributed object capability for Java applications
Allows a Java method to obtain a reference to a remote object and invoke methods of the remote object nearly as easily as if the remote object existed locally
The remote object can be in another JVM on the same host or on different hosts across the network
Uses object serialization to marshal and unmarshal method arguments
Supports the dynamic downloading of required class files across the network

Design Patterns In Java

Remote Method Invocation 14

Bob Tarr

RMI Application
• RMI Application
client
\Neb server
Design Patterns In Java

---
- . -
-- -

. --
. --
---
. - - . - - -- - ---"'-.-.'
. ~~ \/f'}~:.()\.


__ It Server
I I
URL prcd:oc:ol
I I
I
u~~ ~~;:._--------_ ~r---h--' r
1(. d.JCot -- - ...••. , W F! SF! t\l'F!
Remote M(\flwd Invocation 1

Bob Tarr
RMI Stubs And Skeletons


RMI uses stub and skeleton objects to provide the connection between the client and the remote object
A stub is a proxy for a remote object which is responsible for forwarding method invocations from the client to the server . where the actual remote object implementation resides
A client's reference to a remote object, therefore, is actually a reference to a local stub. The client has a local copy of the stub object.
A skeleton is a server-side object which contains a method that dispatches calls to the actual relTIote object implementation
A remote object has an associated local skeleton object to dispatch remote calls to it
Design Patterns In Java

Remote Md hod Invocation 16

Bob Tarr



RMI Stubs And Skeletons
Note: Java 2 (JDKl.2) does not require an explicit skeleton class.
The skeleton object is automatically provided on the server side .
• A method can get a reference to a remote object
¢ by looking up the remote object 111 some directory service. RMI provides a simple directory service called the RMI registry for this purpose.
¢ by receiving the remote object reference as a method argument or return value

Design Patterns In Java

Remote Mdlwcllnvocation II

Bob Tarr
Developing An RMI Application
An object becomes remote-enabled by implementing a remote interface, which has these characteristics:
c:) A remote interface extends the interface java.rmi.Remote
c:) Each method of the interface declares java.rmi.RemoteException in its throws clause, in addition to any application-specific exceptions
• Steps To Develop An RMI Application
c:) 1. Design and implement the COIIl ponents of your distributed application .• Define the remote interface( s )
.• Implement the remote object(s)
.• Implement the client( s)
c:) 2. Compile sources and generate stubs (and skeletons) c:) 3. Make required classes network :Iccessible
c:) 4. Run the application



Design Patterns In Java

Remote Mt1. hCHI Invocation In

Bob Tarr
RMI Example 1
The classic "Hello, World" Example using RMI!
First, define the desired remote interface:
import jaya.rmi.*;
/**
* Hello Interface.
*/
public interface IHello extends Remote {
public String sayHello() throws RemoteException;
}
A class that implements this rUlnote interface can be used as a remote object. Clients can rClllotely invoke the sayHello() method which will return thc string "Hello, World" to the client.

Design Patterns In Java

Remote M d lwei Invocation In

Bob Tarr

RMI Example 1 (Continued)

Next, provide an implementation of the remote object
We'll implement the remote object as a server
• The remote object server implementation should: c) Declare the remote interfaces being implemented
c) Define the constructor for the relTIote object
c) Provide an implementation for each remote method in the remote interfaces c) Create and install a security manager
c) Create one or more instances or a remote object
Register at least one of the relTIotc objects with the RMI remote object registry (or some other naming service), for bootstrapping purposes
To make things simple, our rClllote object implementation will extend java.rmi.server.Unicast l{cmoteObject. This class provides for the "exporting" of a remote object by listening for incoming
calls to the remote object on an anonymous port.
Remote Mdhucllnvocation
o
Bob Tarr
Design Patterns In Java
RMI Example 1 (Continued)
• Here's the server for our remote object:
import java.rmi.*;
import java.rmi.server.*;
II Hello Server.
public class HelloServer extends UnicastRemoteObject implements IHello {
private String name;
public HelloServer(String namo) throws RemoteException { super() ;
this.name = name;
}


public String sayHello() {

n "Hello, World!";}

Design Patterns In Java

Remote Mdhu-Ilnvocation I

Bob Tarr

}
catch(Exception e) {
System. out .println ("HelloServer error: " + e. getMessage () ) ; e.printStackTrace() ;
Design Patterns In Java
}

RMI Example 1 (Continued)
public static void main (String[] args) {
II Install a security manager! System.setSecurityManager(new RMISecurityManager(»;
try {.
II Create the remote object.
HelloServer obj = new HelloServer("HelloServer") ;
II Register the remote object as "HelloServer". Naming.rebind("rmi:llserverhost/HelloServer", obj); System.out.println("HelloServer bound in registry!");
}
}
Remote Mt,Owd Invocation

Bob Tarr

RMI Example 1 (Continued)
• Next, we need to write our client application:
import java.rmi.*;
II Hello Client.
public class HelloClient {
public static void main (String[] args) {
II Install a security manager! System.setSecurityManager(new RMISecurityManager(»;
try {
II Get a reference to tho remote object.
IHello server = (IHello)Naming.lookup("rmi:llserverhost/HelloServer") ; System.out.println("Bound to: " + server);

'~

Design ]>~lttcrIlN III .1""11

Remote Md hntllnvocation

Bob Tarr
}

RMI Example 1 (Continued)
//Invoke the remote method. System.out.println(server.sayHello(» ;
}
catch(Exception e) { e.printStackTrace() ;
}
}



Design Patterns In Java

Remote Mdhod Invocation 4

Bob Tarr
RMI Example 1 (Continued)
• Now we can compile the client and server code:
Javac IHello.java javac HelloServer.java Javac HelloClient.java
We next use the rmic utility to generate the required stub and skeleton classes:
rmic HelloServer
• This generates the stub and skeleton classes:
HelloServer Stub. class
HelloServer Skel.class (Not needed in Java 2)

1

Design Patterns In Java

Remote Method Invocation 25

Bob Tarr


RMI Example 1 (Continued)
Our next step would be to make the class files network accessible.
For the moment, let's assume that all these class files are available locally to both the client and the server via their CLASSPATH. That way we do not have to worry about dynamic class downloading over the network. We'll see in the next example how to properly handle that situation.
The files that the client must have in its CLASSP ATH are:
IHello.class HelloClient.class HelloServer Stub. class
• The files that the server must h,ave in its CLASSP A TH are:
IHello.class HelloServer.class HelloServer Stub. class
HelloServer Skel.class (Not needed in Java 2)
Design Patterns In Java
Remote Method Invocation 26
Bob Tarr

RMI Example 1 (Continued)
If you run this example in Java 2, you need a security policy file that allows the downloading of class files
Here is an example policy file tl1at allows anything!
grant {
permission java.security.AIIPermission;

} ;
Here's a policy file that allows the program to connect to or accept connections from any IH)~t on ports greater than 1024 and to connect to any host on port XO (the default HTTP port):
grant {
permission java.net.Sock "connect,accept";
permission java.net.Socketl'qnni.ssion "*:80", "connect";

mission "*:1024-65535",
} ;
Design Patterns In Java
Remote M('~llC)lllnvocation 7
Bob Tarr

/"1
r
J RMI Example 1 (Continued)
Now, we/are ready to run the application: • On the server:
c) Start the rmiregistry:
rmiregistry &
c) Start the server:
java -Djava.security.policy=policy HelloServer

• On the client:
c) Start the client:
java -Djava. securi ty. policy=policy HelloClient
• Get this wonderful output on the client:
Hello, World!
Design Patterns In Java
Remote Method Invocation 28
Bob Tarr
HelloServer.java
import java.rmi.*;
import java.rmi.server.*; II Hello Server.
public class Helloserver extends unicastRemoteobject implements IHello {
private String name;
public Helloserver(string name) throws RemoteException { super();
this.name = name;
}
public string sayHello() {return "Hello, world!";} public static void main(string[] args) {
II Install a security manager! system.setsecurityManager(new RMIsecurityManager())" try {
II Create the remote object.
HelloServer obj = new Helloserver("Helloserver");
II Register the remote object as "HelloServer". Naming.rebind("rmi:lllocalhost/Helloserver", obj); System.out.println("Helloserver bound in registry!"); }
catch(Exception e) {
system.out.println("HelloServer error: II + e.getMessage()); e.printStackTrace();
}
}
}
page 1


Helloserver.iava
import java.rmi.~·
import java.rmi .server.%a
II Hello Server.
public class Helloserver extenas implements IHello {
private String name;
public Helloserver(String name) throws RemoteException { super();
this. name = name;
}
public String sayHello() {return "Hello, world!";} public static void main(String[] args) {
II Install a security manager! System.setsecurityManager(new RMISecurityManager()); try {
II Create the remote object.
Helloserver obj = new Helloserver("Helloserver");
II Register the remote object as "Helloserver". Naming.rebind("rmi:lllocalhost/Helloserver", obj); System.out.println("Helloserver bound in registry!"); }
catch(Exception e) {
system.out.println("Helloserver error: II + e.getMessage()); e.pri~tstackTrace();
}
}
}
page 1
HelloServer.java
import java.rmi.*; import java.rmi.server.*;
II Hello Server.
public class HelloServer extends unicastRemoteobject implements IHello {
private string name;
public Helloserver(string name) throws RemoteException { super();
this.name = name;
}
public String sayHello() {return "Hello, world!";} public static void main(string[] args) {
II Install a security manager! system.setsecurityManager(new RMIsecurityManager()); try {
II Create the remote object.
HelloServer obj = new Helloserver(IHelloserver");
II Register the remote object as "Helloserver". Naming.rebind(lrmi:lllocalhost/Helloserver", obj); system.out.println("Helloserver bound in registry!"); }
catch(Exception e) {
System.out.println("HelloServer error: II + e.getMessage()); e.printStackTrace();
}
}
}
page 1

HelloClient.java
import java.rmi.*; II Hello client.
public class Helloclient {
public static void main(string[] args) {
II Install a security manager! system.setsecurityManager(new RMIsecurityManager()); try {
II Get a reference to the remote object.
IHello server = (IHello)Naming.lookup(lrmi:lllocalhost/Helloserver"); system.out.println("Bound to: II + server);
IIInvoke the remote method. system.out.println(server.sayHello());
}
catch(Exception e) { e.printstackTrace(); }
}
}
page 1
HelloClient.java
import java.rmi.*; II Hello client.
public class Helloclient {
public static void main(String[] args) {
II Install a security manager! system.setsecurityManager(new RMIsecurityManager()); try {
II Get a reference to the remote object.
IHello server = (IHello)Naming.lookup("rmi:lllocalhost/Helloserver"); system.out.println("Bound to: II + server);
IIInvoke the remote method. system.out.println(server.sayHello());
}
catch(Exception e) { e.printstackTrace(); }
}
}
page 1

No comments:

Post a Comment

JTSEARCH