JavaNetwork II
JavaNetwork II
Java Programming II
Contents
Distributed Java
Objects
RMI Communication of Remote Object and Client Writing Java RMI Application Hello Example RMI Structure If room (RMI Callback)
Java Programming II
2
Distributed Objects
Objects that can communicate with objects on heterogeneous run-time environments
Transparent
Distributed Objects Technology Multi-Platform Transparent access to distributed objects Language Neutral: RMI, CORBA, DCOM
Java Programming II
3
use objects on remote different runtime environments as like objects on a local run-time environment Abstraction of low-level network code on distributed network to provide developers an environment where they focus on their application development.
Java Programming II
Introduction to RMI
Distributed Processing on Network Define of Remote Interface Object Serialization java.rmi and java.rmi.server Create Stub and Skeleton
Java Programming II
Client
Stub
Skeleton
Network
Java Programming II
Java Programming II
HelloImpl object
public class HelloImpl extends UnicastRemoteObject implements Hello { public HelloImpl() throws RemoteException { super(); } public String sayHello() { return "Hello World!"; } public static void main(String args[]) { // Create and install a security manager if (System.getSecurityManager() == null) { System.setSecurityManager(new RMISecurityManager()); }
HelloImpl obj = new HelloImpl(); // Bind this object instance to the name "HelloServer" Naming.rebind("HelloServer", obj); System.out.println("HelloServer bound in registry"); } catch (Exception e) { System.out.println("HelloImpl err: " + e.getMessage()); e.printStackTrace(); } } }
Java Programming II
Java Programming II
10
Start rmiregistry. Default port is 1099. Run the RMI Server Compile the Client Run the Client
Java Programming II
11
RMI Structure
Protocol Java Remote Method Protocol (JRMP)
JVM
Remote Method Invocation
Client
Data Transformation
Stub
Skeleton
Remote Method Return
Remote Object
Marshalling
Marshalling
Object Serialization
Network
Java Programming II
13
JRMP Class
Marshall/Unmarshall (Object Serialization)
Virtual connection
Java Programming II
15
For the callback, a client (or invoker) provide services (methods) for server (or program to be invoked). The callback is that the server (callee) invokes client (caller)s methods.
If you are interested in an example for callback to a clients method, refer to the A Simple Callback Example
Java Programming II
16
/home/course/java2/codes/ByTopics/RMI/RMI-ComputeEngine
Java Programming II
17
public class CallbackImpl extends UnicastRemoteObject implements Callback { public CallbackImpl() throws RemoteException { super(); } public String greeting(String lang) throws RemoteException { CallbackServer server = new CallbackServer(); return server.sayHello(this, lang); } public String speakJapanese(){ return new String("Konnichiwa!"); } public String speakEnglish(){ return new String("How are you!"); }
Java Programming II
public String sayHello(Callback callback, String lang) throws RemoteException { String message = null;
if(lang.equals("JAPANESE")) { message = callback.speakJapanese(); System.out.println("In CallbackServer, " + message); return message; } else { message = callback.speakEnglish(); System.out.println("In CallbackServer, " + message); return message; }
Java Programming II
19