Distributed Objects
1
In the Good Old Days...
Only local objects existed
My Machine
My Object
2
Today’s World...
Network and Distributed Objects
My Machine
Local
Objects
Remote
Machine
Remote
Objects
3
Overview
• What options do I have for distributed application
development?
• Developers who program using the Java programming
language can choose several solutions for creating
distributed applications programs.
1) Java RMI technology
2) Java IDL technology (for CORBA programmers)
3) Enterprise JavaBeans technology
In this section we shall be talking about Java RMI and
IDL (Corba) technologies
4
JAVA RMI
Remote Method Invocation
5
Outline
1)
2)
3)
4)
5)
RMI Concepts
RMI architecture
Implementing and running RMI system
Implementing activatable RMI server
Summary
6
Introduction
• Remote Method Invocation (RMI) technology
was first introduced in JDK1.1.
• RMI allows programmers to develop distributed
Java programs with the same syntax and
semantics used for non-distributed programs.
• RMI is based on a similar, earlier technology for
procedural
programming
called
remote
procedure call (RPC)
7
Introduction
Disadvantages of RPC
a) RPC supports a limited set of data types.
Therefore it is not suitable for passing and
returning Java Objects
b) RPC requires the programmer to learn a
special interface definition language (IDL) to
describe the functions that can be invoked
remotely
8
Introduction
The RMI architecture defines
a) How objects behave.
b) How and when exceptions can occur.
c) How memory is managed.
d) How parameters are passed to, and returned
from, remote methods.
The remote object model for Enterprise
JavaBeans (EJB) is RMI- based.
9
Introduction
•
•
RMI is designed for Java-to-Java distributed
applications. RMI is simpler and easier to
maintain than using socket.
Other options for creating Java-to-non-Java
distributed applications are:
a) Java Interface Definition Language (IDL)
b) Remote Method Invocation (RMI) over
Internet Inter-ORB Protocol (IIOP) -- RMIIIOP.
10
RMI architecture
• RMI allows the code that defines the behavior and the
code that implements the behavior to remain separate
and to run on separate JVMs.
• At client side, RMI uses interfaces to define behavior.
• At server side, RMI uses classes to define
implementation.
11
RMI Layer
Java Virtual Machine
Java Virtual Machine
Client
Object
Remote
Object
Stub
Skeleton
Remote Reference Layer
Remote Reference Layer
Transport Layer
TCP
Transport Layer
12
Remote Objects
• Remote Objects
– Live on server
– Accessed as if they were local
13
Remote Objects
14
Stubs and Skeletons
• Stub
– lives on client
– marshals argument data (serialization)
– unmarshals results data (deserialization)
• Skeleton
– lives on server
– unmarshals argument data
– marshals results data
– receives requests from stub
– delivers response to stub
16
Remote Reference Layer
• This layer provides a RemoteRef object that
represents the link to the remote service
implementation object.
• Encodes and decodes the on-wire protocol.
• Implements the remote object protocols
(Unicast Remote Object/ activatable remote
objects ).
17
Transport Layer
• The Transport Layer makes the connection
between JVMs. All connections are streambased network connections that use TCP/IP.
• Dispatching messages between stub and
skeleton.
18
RMI Flow
1. Server Creates Remote Object
Client Virtual Machine
2. Server Registers Remote Object
Client
Server Virtual Machine
Remote
Object
1
Skeleton
Stub
Server
2
“Fred”
Registry Virtual Machine
19
RMI Flow
Client Virtual Machine
Client
Server Virtual Machine
Remote
3. Client requests object from Registry
Object
4. Registry returns remote reference
(and stub gets created)
Skeleton
Stub
3
Server
4
“Fred”
Registry Virtual Machine
20
RMI Flow
Client Virtual Machine
Server Virtual Machine
Client
Remote
Object
5
7
6
Stub
Skeleton
Server
5. Client invokes stub method
6. Stub talks to skeleton
7. Skeleton invokes remote object
“Fred”
method
Registry Virtual Machine
21
The steps...
•
•
•
•
•
•
•
Create the Interface to the server
Create the Server
Create the Client
Compile the Interface (javac)
Compile the Server (javac)
Compile the Client (javac)
Generate Stubs and Skeletons (rmic)
22
Part II: RMI Usage
23
Creating Remote Objects
• Define a Remote Interface
– extends java.rmi.Remote
• Define a class that implements the Remote
Interface
– extends java.rmi.RemoteObject
– or java.rmi.UnicastRemoteObject
24
Remote Interface Example
import java.rmi.*;
public interface Adder extends Remote
{
public int add(int x, int y)
throws RemoteException;
}
25
Remote Interface Example
• Remark:
• All the interface has to extend the
java.rmi.Remote interface
• and all the methods has to declare that it may
throw a RemoteException object.
26