Remote Method Invocation (RMI)
Remote Method Invocation (RMI)
Remote Method Invocation (RMI)
JRMI-1.1
Introduction
The Core Java Classes That Made the Success of Java
Provides the Basic Building Blocks for Creating
CSE333
Robust Networked Applications.
Those Classes Are All the Ones Included in the
Following Packages:
java.applet,
java.awt,
java.io,
java.lang,
java.net and
java.util.
They Form the Base of the Java Framework That Was
Shipping in the Java Development Kit, Version 1.0.
JRMI-1.2
Introduction
The 1.1 Version of the JDK, Was an Essential
Improvement As the Kit Contained a Set of New
CSE333 API’s Specific to Distributed Computing:
java.security,
java.sql with sun.jdbc,
java.beans, and
java.rmi.
JRMI-1.3
Introduction (Concluded)
The Next Version JDK 1.2 Extends the Set of New
API's Specific to Distributed Computing:
Enterprise Java Beans (EJB),
CSE333
javaRMI Extensions
java Naming and Directory Interface (JNDI),
java IDL,
java Transaction Systems (JTS),
java Messaging Systems (JMS)
JRMI-1.4
RMI defined
Java Remote Method Invocation (RMI) Is an Inter-
process Protocol for Java, Allowing Java Objects
CSE333 Living in Different Java Virtual Machines to
Invoke Transparently Each Other's Methods.
Since These Virtual Machines Can Be Running on
Different Computers Anywhere on the Network,
RMI Enables Object-oriented Distributed
Computing.
Java RMI Provides the Java Programmers With an
Efficient, Transparent Communication Mechanism
That Frees Them of All the Application-level
Protocols Necessary to Encode and Decode
Messages for Data Exchange.
JRMI-1.5
RMI CORBA metaphor
Remote Objects Written in Java RMI Interact
Approximately in Much the Same Way CORBA
CSE333 Objects Do:
Server Objects Publish Their Interfaces to Make
Them Available to RMI Clients;
Stub Classes Deal With Binding to the Remote
Objects and Do the Client-side Data Marshaling;
Skeleton Classes on the Server-side Handle
Incoming Calls.
The Communication Mechanism Is Shown in the
Next Slide . Without Any Surprise, This Looks
Very Much Like the Elementary ORB Structure of
CORBA.
JRMI-1.6
RMI CORBA Block Diagrams
CSE333
JRMI-1.7
Extensions included in RMI
Java RMI Introduces a New Object Model to
Programmers:
The Java Distributed Object Model, Which
CSE333
Naturally Extends the Pre-JDK1.1 Object
Model.
A Few New Features or Divergence's Were
Added.
The Results of a Remote Invocation, As Well
As the Arguments That Came Along With the
Initial Request, Are Passed by Value Rather
Than Reference.
This Is Because Object References Are Only
Useful Within the Same Virtual Machine.
JRMI-1.8
Extensions included in RMI
New Features (Continued)
Instead, Ordinary Objects Are Passed by Value, by
CSE333 Copy, Between Two Different Virtual Machines.
To Do This Copy Operation, RMI Uses the New
Object Serialization Service, Which Flattens a
Local Java Object's State Into a Serial Stream That
It Can Then Pass As a Parameter Inside a Message.
A Remote Object, an Object That Implements the
java.rmi.Remote Interface, Can Nevertheless Be
Passed by Reference and Not by Copying the
Actual Implementation. Clients of Remote Objects
Interact With Remote Interfaces, Never With the
Implementation Classes of Those Interfaces.
JRMI-1.9
Extensions included in RMI (Concluded)
JRMI-1.11
RMI Block Diagram for HelloWorld
Application
Client Server Hello
RMI Layer
NDR NDR NDR NDR
4
Start rmiregistery
JRMI-1.12
Hello.java
package examples.hello;
CSE333
import java.rmi.Remote;
import java.rmi.RemoteException;
JRMI-1.13
HelloImpl.java
package examples.hello;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
CSE333 import java.rmi.server.UnicastRemoteObject;
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());
}
try {
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();
}
}
} JRMI-1.14
HelloApplet.java
package examples.hello;
import java.applet.Applet;
import java.awt.Graphics;
import java.rmi.Naming;
CSE333 import java.rmi.RemoteException;
public class HelloApplet extends Applet {
String message = "blank";
// "obj" is the identifier that we'll use to refer
// to the remote object that implements the "Hello"
// interface
Hello obj = null;
public void init() {
try {
obj = (Hello)Naming.lookup("//" +
getCodeBase().getHost() + "/HelloServer");
System.out.println("Host = "+getCodeBase().getHost());
message = obj.sayHello();
} catch (Exception e) {
System.out.println("HelloApplet exception: " +
e.getMessage());
e.printStackTrace();
}
}
public void paint(Graphics g) {
g.drawString(message, 25, 50);
}
} JRMI-1.15
RMI Steps
The Different Steps That Must Follow to Get a
Server Implemented and Running Are:
CSE333
1. Define the Server Interface. The Server
Object Declares Its Service Via a Remote
Interface, That Must Therefore Extends the
java.rmi.Remote Interface.
JRMI-1.16
RMI Steps
Different Step (Continued)
JRMI-1.17
RMI Steps (Concluded)
Different Step (Continued)
4. Compile the Server Class. First, Uses the
CSE333 Javac Compiler to Produce the Server .class
File, and Secondly, Run the rmic Compiler to
Obtain the Stub and Skeleton .class Files.
5. Execute the Server Program. If the
RMIRegistry Daemon Is Not Already Running,
It Must Be Launched. He Can Then Run the
Java Interpreter With His Server .class File.
6. Distribute the Stub Class. In Order to Get
Clients Calling the Methods of the Server, the
Stub Class That Has Been Generated Needs to
be Provided . This Will Enable Client Code to
Make Successful Requests to Server Object.
JRMI-1.18
Java, JavaBeans and Java RMI
Java Is "A Simple, Object Oriented, Distributed,
Interpreted, Robust, Secure, Architecture Neutral,
Portable, High-performance, Multithreaded and
CSE333
Dynamic Language".
Javabeans Is the Specification for Building Reusable
Software Components (on the Client Side) Using Java.
Java-RMI Ensures Communication Between Distributed
Program-level Java Objects Residing in Different
Address Spaces by Assuming a Homogeneous Java
Virtual Machine Environment.
It Thus Takes Advantage of the Java Object Model
Whenever Possible to Support Distributed Objects in
the Java Environment.
JRMI-1.19
Enterprise Java Beans
Enterprise Javabeans (EJB) (for the Server Side)
Provides a Fully-scaleable, Distributed, and Cross-
CSE333 platform Architecture That Makes the Most of
Your Business Resources.
Not Only Can These Components Run on Any
Platform, but They Are Also Completely Portable
Across Any Vendor's EJB Component Execution
System.
The EJB Environment Automatically Maps the
Component to the Underlying Vendor-specific
Execution Services.
JRMI-1.20
Java Remote Method Invocation
Remote Method Invocation (RMI) Is the Object
Equivalent of Remote Procedure Calls (RPC).
CSE333 While RPC Allows You to Call Procedures Over
a Network, RMI Invokes an Object's Methods.
In the RMI Model,
The Server Defines Objects That the Client
Can Use Remotely.
The Clients Can Now Invoke Methods of This
Remote Object As If It Were a
Local Object Running in the Same Virtual
Machine As the Client.
RMI Hides the Underlying Mechanism of
Transporting Method Arguments and Return
Values Across the Network.
JRMI-1.21
Java Remote Method Invocation
JRMI-1.22
Java-RMI and comparison with other
Middleware Specifications
JRMI-1.23
Java-RMI and comparison with other
Middleware Specifications
JRMI-1.25
Workings of Java-RMI
JRMI-1.26
The RMI Registry
The Server Object Makes Methods Available for
Remote Invocation by Binding It to a Name in the
CSE333 RMI Registry.
JRMI-1.28
Rmic Compiler
The rmic Compiler Generates Two Files:
A Stub and a Skeleton.
CSE333 The Stub Resides on the Client Machine and the
Skeleton Resides on the Server Machine.
When a Client Invokes a Server Method, the
JVM Looks at the Stub to Do Type Checking.
The Request Is Then Routed to the Skeleton on
the Server, Which in Turn Calls the
Appropriate Method on the Server Object. In
Other Words, the Stub Acts As a Proxy to the
Skeleton and the Skeleton Is a Proxy to the
Actual Remote Method.
JRMI-1.29
Java RMI Internals
JRMI-1.30
Java RMI Internals
JRMI-1.31
Java RMI Internals (Continued)
A Client Obtains This Stub by Calling the
Registry, Which Hands It the Stub Directly.
CSE333 (This Is Also Where the “codebase” Comes In:
If the Server Specified a “codebase” to Use for
Clients to Obtain the Class File for the Stub,
This Will Be Passed Along to the Client Via
the Registry.
The Client Can Then Use the codebase to
Resolve the Stub Class - That Is, to
Load the Stub Class File Itself).
The RMIRegistry Holds Onto Remote Object
Stubs Which It Can Hand off to Clients When
Requested.
JRMI-1.32
Java RMI Internals (Continued)
When the Client Issues a Remote Method Invocation
to the Server, the Stub Class Creates a "RemoteCall"
CSE333 Which Basically
(A) Opens a Socket to the Server on the Port
Specified in the Stub Itself, and
(B) Sends the RMI Header Information As
Described in the RMI Spec.
The Stub Class Marshals the Arguments Over the
Connection by Using Methods on
RemoteCall to Obtain the Output Stream Which
Basically Returns a Subclass of ObjectOutputStream
Which Knows How to Deal With Passing Objects
Which Extend java.rmi.Remote, Which Serializes
Java Objects Over the Socket
JRMI-1.33
Java RMI Internals (Continued)
The Stub Class Calls RemoteCall.executeCall
Which Causes the RMI to Happen.
CSE333
On the Server Side, When a Client Connects to the
Server Socket, a New Thread Is
Forked to Deal With the Incoming Call.
The Original Thread Can Continue Listening to
the Original Socket So That Additional Calls
From Other Clients Can Be Made.
JRMI-1.34
Java RMI Internals (Concluded)
The Server Calls the “dispatch” Method of the Skeleton
Class (the Server-side ”stub" Generated by rmic),
Which Calls the Appropriate Method on the Object and
CSE333
Pushes the Result Back Down the Wire (Using the Same
‘RemoteCall Interface Which the Client Used to
Marshall the Arguments).
If the Server Object Threw an Exception Then the
Server Catches This and Marshals That Down the
Wire Instead of the Return Value.
Back on the Client Side, the Return Value of the RMI Is
Unmarshalled (Using the RemoteCall as Created
Above) and Returned From the Stub Back to the Client
Code Itself.
If an Exception Was Thrown from the Server that's
Unmarshalled and Re-thrown From the Stub.
JRMI-1.35
CSE333
Paul C. Barr
[email protected]
JRMI-1.36
The Java 1.1 Event Model
JRMI-1.37
The Java 1.1 Event Model
JRMI-1.38
The Java 1.1 Event Model (Continued)
The First Approach Is the Easiest to Implement, but
Any Time You Use a Switch Statement, You
CSE333
Somehow Have This Nagging Feeling That Perhaps
There Is Something Un-object-oriented in the Design
or That a Different Class Hierarchy and Design Would
Have Made This Go Away.
The Second Approach Is More Object-oriented, but
Involves a Great Deal of Work.
With the New 1.1 Delegation Event Model, a
Component Can Be Told Which Object or Objects
Should Be Notified When the Component Generates a
Particular Type of Event.
If a Component Is Not Interested in an Event Type,
Then Events of That Type Will Not Be Propagated.
JRMI-1.39
The Java 1.1 Event Model
(According to Sun)
Simple and Easy to Learn
Support a Clean Separation Between Application
CSE333 and GUI Code
Flexible Enough to Enable Varied Application
Models for Event Flow and Propagation
For Visual Tool Builders, Enable Run-time
Discovery of Both Events That a Component
Generates As Well As the Events It May Observe
Support Backward Binary Compatibility With the
Old Model
Facilitate the Creation of Robust Event Handling
Code Which Is Less Error-prone (Strong Compile-
time Checking)
JRMI-1.40
The 1.1 Delegation Event Model
JRMI-1.41
The 1.1 Delegation Event Model
(Pictorial Representation)
CSE333
JRMI-1.42
The 1.1 Delegation Event Model
(Continued)
add( m_Label );
add( m_Button );
}
JRMI-1.44
Event Listener Object
//This is one typical Event Listener Object implementation
class EventListener implements ActionListener {
private EventSource m_sourceObject;
JRMI-1.45
Event Object Interactions
JRMI-1.46
Event Delegation Model
JRMI-1.47
Event Classes
CSE333
JRMI-1.48
Event Class Specifics
Every Event Is a Subclass of java.util.EventObject.
It Is a Very General Class With Only One Method of
Interest:
CSE333
Object getSource()
This Method Returns the Object That Originated the
Event. Every Event Has a Source Object, From Which
the Event Originated. This Method Returns a Reference
to That Source.
AWT Events, Which Is What We Are Concerned With Here,
Are Subclasses of java.awt.AWTEvent.
This Is the Superclass of All the Delegation Model Event
Classes. The Most Interesting Method in This Class is int
getID()
This Method Returns the ID of the Event. An Event’s ID
is an int That Specifies the Exact Nature of the Event.
This Value is Used to Distinguish the Various Types of
Events That Are Represented by Any Event Class.
JRMI-1.49
AWT Events
JRMI-1.50
Types of Events
ActionEvent Generated by Component Activation
AdjustmentEvent Generated by Adjustment of Adjustable
Components Such As Scroll Bars
CSE333
ContainerEvent Generated When Components Are Added to
or Removed From a Container
FocusEvent Generated When a Component Receives Input
Focus
ItemEvent Generated When an Item Is Selected From a List,
Choice or Check Box
KeyEvent Generated by Keyboard Activity
MouseEvent Generated by Mouse Activity
PaintEvent Generated When a Component Is Painted
TextEvent Generated When a Text Component Is Modified
WindowEvent Generated by Window Activity Like
Minimizing or Maximizing
JRMI-1.51
Handling Events
JRMI-1.52
The Event Listeners
JRMI-1.53
//This is one typical Event Listener Object implementation
class EventListener implements ActionListener {
private EventSource m_sourceObject;
CSE333
public EventListener( EventSource sourceObject ) {
m_sourceObject = sourceObject;
}
JRMI-1.54
The applet code that forms the Event
Source
//This is one typical Event Source Object implementation
public class EventSource extends Applet {
public Label m_Label;
public Button m_Button;
CSE333 public int m_nCounter;
add( m_Label );
add( m_Button );
}
JRMI-1.55
If You Notice the Code Fragment Above, an
Instance of the EventListener Class Is Created.
CSE333 This Instance listenerObject, is Set As One of the
Button's Action Listeners Using the
addActionListener() Method Call. All in All,
You Will Find a Standard Pattern Followed for
Giving a Listener to a Component. It Can Be
Summarized As Follows
Create a Listener Class That Implements the
...Listener Interface
Construct the Component
Construct an Instance of the Listener Interface
Call Add...Listener() on the Component, Passing in
the Listener
JRMI-1.56
EventListener Removal
m_button.Removeactionlistener( listenerObject );
JRMI-1.57