Transcript rmiCorba13

Client/Server Distributed Systems
240-322, Semester 1, 2005-2006
13. RMI and CORBA
 Objectives
– introduce rmi and CORBA
240-322 Cli/Serv.: rmiCORBA/13
1
1. RMI Overview
1.1
1.2
1.3.
1.4.
1.5.
1.6.
1.7.
240-322 Cli/Serv.: rmiCORBA/13
What is RMI?
A Typical RMI Application
Server-side Features
Client-side Features
Advantages
Disadvantages
More Details on RMI
2
1.1. What is RMI?
 RMI
== Remote Method Invocation
– allows a Java object to call a method of a Java
object running on another machine
– RMI is a modern version of RPC for
communication between Java objects
240-322 Cli/Serv.: rmiCORBA/13
3
1.2. A Typical RMI Application
1099
lookup
remote 2
reference
remote
stub
client
invoke
method 4
via the
remote
stub
240-322 Cli/Serv.: rmiCORBA/13
rmiregistry
store (rebind)
remote
1
references
3
server
remote
objects
4
1.3. Server-side Features
 The
server:
– creates remote objects (objects that will be
accessible by clients)
 the
server is sometimes called a server factory
– places remote references (names) for the
objects in the rmiregistry
 they
240-322 Cli/Serv.: rmiCORBA/13
can then be accessed by clients
continued
5
remote
interface
impl.
 A remote
object is made from a remote
interface and a separate implementation class.
 A remote
interface is a set of method
prototypes
– a method prototype is the name of the method and
the types of its input arguments and return type
240-322 Cli/Serv.: rmiCORBA/13
continued
6
Example Remote Interface
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote
{
String sayHello() throws RemoteException;
}
The remote object will
have this interface
240-322 Cli/Serv.: rmiCORBA/13
7
1.4. Client-side Features
 The
client gets a reference to a server's
remote object by querying the rmiregistry.
 At
the programming level, this reference
appears to be the remote object
– in fact it refers to a remote stub which is
downloaded invisibly from the rmiregistry
240-322 Cli/Serv.: rmiCORBA/13
8
 The
remote stub is an ordinary Java class
– its purpose is to handle the low-level
communication between the client and the
remote object on the server-side.
 Object
data is passed between the client and
server using a standard feature of Java
called object serialization.
240-322 Cli/Serv.: rmiCORBA/13
9
Diagram of Communication
client
hi.sayHello()
is really...
remote
object
1. serialize
3. call 4. result
6.
result
the call
5.
stream
of
bytes
remote
server
stub
2. stream of bytes
240-322 Cli/Serv.: rmiCORBA/13
10
1.5. Advantages
 Dynamic
code loading
– a client does not need to contain any
communication code when written -- that is
downloaded when the remote stub is retrieved
– a client can dynamically download other classes
 e.g.
those used by the remote stub
– the server can also download code from the client
240-322 Cli/Serv.: rmiCORBA/13
continued
11
 The
programmer doesn't write any
communication code
– the remote stub is generated automatically by
passing the remote interface to the RMI
compiler (rmic)
240-322 Cli/Serv.: rmiCORBA/13
12
1.6. Disadvantages
 Not
easy to integrate RMI Java code with
legacy applications in other languages (e.g.
C, C++).
240-322 Cli/Serv.: rmiCORBA/13
13
1.7. More Details on RMI
 The Java RMI tutorial
http://java.sun.com/docs/
 A starting
point for RMI information:
http://java.sun.com/products/jdk/rmi
240-322 Cli/Serv.: rmiCORBA/13
14
2. CORBA Overview
2.1.
2.2.
2.3.
2.4.
2.5.
2.6.
2.7.
2.8.
What is CORBA?
Important CORBA Features
Why use CORBA?
CORBA/Java Advantages
A CORBA Application
Other CORBA Coding Styles
Comparisons with Other Approaches
More Details on CORBA
240-322 Cli/Serv.: rmiCORBA/13
15
2.1. What is CORBA?
 The
Common Object Request Broker
Architecture (CORBA)
– a specification, not an implementation
 The
Object Management Group’s (OMG) aim:
– specify a distributed computing environment
within an object-oriented framework
 i.e.
240-322 Cli/Serv.: rmiCORBA/13
using objects, methods, message passing, etc.
16
Using CORBA (simple view)
objects
managed by
the server
Client
object
call a method in an object
managed by the server by
using an object reference
ORB
Server
method
call & result
ORB
network
240-322 Cli/Serv.: rmiCORBA/13
17
ORBs
 An
Object Request Broker (ORB) implements
the features specified by CORBA.
 An
ORB can be coded in any language
– so long as it supports CORBA’s functionality
 ORBs
communicate using the Internet InterORB Protocol (IIOP)
– an extension of TCP
240-322 Cli/Serv.: rmiCORBA/13
18
Major ORB Components
Client
Dynamic
Invocation
Object Implementation
IDL
Stubs
ORB
Interface
IDL
Skeleton
Object
Adapter
ORB Core
240-322 Cli/Serv.: rmiCORBA/13
19
Using CORBA (more detail)
Client
Server
IDL client stub
IDL server skeleton
ORB interface
ORB interface
ORB internals
ORB internals
Network
240-322 Cli/Serv.: rmiCORBA/13
20
(Java) IDL
 IDL:
Interface Definition Language
– for defining OO data and methods
 The
J2SE idltojava compiler generates client
stubs and server skeletons for work with any
CORBA ORB.
 J2SE
includes a simple (free) ORB.
240-322 Cli/Serv.: rmiCORBA/13
21
IDL Examples
module HelloApp
{
interface Hello {
string sayHello();
};
};
240-322 Cli/Serv.: rmiCORBA/13
continued
22
module Appliance
{
interface TV {
readonly attribute string SerialNo;
attribute short Vol;
attribute short Channel;
void operate();
};
interface WebTV : TV {
void surfTo(in URL url);
};
};
240-322 Cli/Serv.: rmiCORBA/13
23
2.2. Important CORBA Features
 An
object’s interface (service) is completely
separated from its implementation.
 An
object’s location is completely hidden.
Consequently, CORBA provides:
– a naming service
(white pages)
– a trading service
(yellow pages)
– an interface Repository (IR)
240-322 Cli/Serv.: rmiCORBA/13
continued
24
 Object
communication is greatly simplified:
– messages, written in IDL
 An
object can find other objects at run-time
by using the Dynamic Invocation Interface
(DII):
– but the usual approach is to already know the
location of the other object
240-322 Cli/Serv.: rmiCORBA/13
continued
25
 Objects
can be built more easily by using
pre-existing CORBA services for:
– message encoding, object locating, security, etc.
 Advanced
services:
– persistent objects, transactions, concurrency
control, etc.
240-322 Cli/Serv.: rmiCORBA/13
continued
26
 CORBA facilities:
– horizontal and vertical application frameworks
– e.g. printing, mobile agents
 Convert
legacy code in Basic, C, etc. into
objects
– uses CORBA object adaptors
240-322 Cli/Serv.: rmiCORBA/13
27
2.3. Why Use CORBA?
 It
provides a powerful OO mechanism for
defining the interfaces between distributed
objects.
 It offers many services and facilities.
 Platform/language independent.
 An open standard:
– ensures its continued innovation and evolution
240-322 Cli/Serv.: rmiCORBA/13
28
2.4. CORBA/Java Advantages
 CORBA’s
advantages for Java:
– CORBA supports object method calling from
anywhere
– CORBA allows Java to work with objects coded in
other (non-OO) languages
– CORBA augments Java’s networking features
e.g. it encourages multi-host applications
240-322 Cli/Serv.: rmiCORBA/13
continued
29
 Java’s
advantages for CORBA
– Java’s OO features match those in CORBA
 e.g.
separation of interface (service) and impl.
– Java has many useful features for implementing
CORBA services and facilities:
multi-threading, exceptions, GUI, packages,
automatic garbage collection
240-322 Cli/Serv.: rmiCORBA/13
30
2.5. A CORBA Application
 1.
Write an IDL interface for the remote
objects managed by the server.
 2.
Compile the IDL interface
– it generates a Java version of the interface
– it generates stub and skeleton code for the client
and server
240-322 Cli/Serv.: rmiCORBA/13
continued
31
 3.
Write the server, which has two parts
– the server (factory)
 it
creates remote objects
– the remote object implementation (of the IDL
interface)
 4.
Implement the client
– it contacts and uses a remote object
240-322 Cli/Serv.: rmiCORBA/13
32
Running the Application
network
lookup
remote
reference 2
to an object
client
O
R
B
tnameserv
3
remote stub
ORB
4
invoke method
via the remote
reference and stub
240-322 Cli/Serv.: rmiCORBA/13
store (rebind)
remote
references 1
for objects
server
factory
remote
objects
33
Using a Remote Object
Client
(object)
object
ref
server
method invocation
object
4
Client
server
object
ref
object
240-322 Cli/Serv.: rmiCORBA/13
execution
continued
34
Client
object
ref
server
return method result
object
Client
object
ref
240-322 Cli/Serv.: rmiCORBA/13
server
garbage collect
object
35
Client Callback
4
Client
object A
object
ref B
method invocation
Server
object B
object
ref A
object
ref A
240-322 Cli/Serv.: rmiCORBA/13
continued
36
method invocation
(callback)
Client
object A
object
ref B
240-322 Cli/Serv.: rmiCORBA/13
Server
object B
object
ref A
37
2.6. Other CORBA Coding Styles
2.6.1. Single-threaded server
2.6.2. Multi-threaded server
2.6.3. Being a client and a server
2.6.4. Blocking vs. one-way
2.6.5. Pass by reference vs. pass by value
240-322 Cli/Serv.: rmiCORBA/13
38
1.6.1. Single-threaded Server
server
client 1
client 2
method call
suspended
busy
result
method call
wait
call proceeds
suspended
busy
result
240-322 Cli/Serv.: rmiCORBA/13
39
2.6.2. Multi-threaded Server
server
client 1
client 2
method call
method call
suspended
result
suspended
result
thread
exits
240-322 Cli/Serv.: rmiCORBA/13
thread
exits
40
Object Factory Pattern
server
client 1
client 2
request object
return ref
method call
request object
return ref
method call
result
result
object
240-322 Cli/Serv.: rmiCORBA/13
object
41
2.6.3. Being a Client and a Server
Client A
Server D
Client/
Server C
Client B
240-322 Cli/Serv.: rmiCORBA/13
Server E
continued
42
 Usually,
a client can be single-threaded,
and a server should be multi-threaded.
 In
mixed-mode, the client may need to
be multi-threaded to handle its server
role.
240-322 Cli/Serv.: rmiCORBA/13
43
A Mixed-mode single-threaded
Problem
client/
server 1
client/
server 2
method call
suspended
X
method call
waits forever
suspended
deadlock
240-322 Cli/Serv.: rmiCORBA/13
44
Multi-threaded Solution
client/
server 1
client/
server 2
method call
method call
result
thread
exits
suspended
result
thread
exits
240-322 Cli/Serv.: rmiCORBA/13
45
2.6.4. Blocking vs. One-way
Blocking
server
client
method call
suspended
result
busy
processing
resumes
240-322 Cli/Serv.: rmiCORBA/13
46
client
oneway
method call
server
one-way
processing
continues
busy
method call
finishes
 Problem:
the client has no way of
knowing if the method call has
succeeded.
240-322 Cli/Serv.: rmiCORBA/13
47
2.6.5. Pass by Reference vs. Value
server
client
Pass an object
by reference.
240-322 Cli/Serv.: rmiCORBA/13
request object
return ref
method call
result
object
48
server
client
Pass an object
by value.
request object
return
copy of
object
create
object
object
object
240-322 Cli/Serv.: rmiCORBA/13
continued
49
 If
a client invokes many methods in an
object, it may be better, in terms of
efficiency, to copy it to the client.
240-322 Cli/Serv.: rmiCORBA/13
50
2.7. More Details on CORBA
 Beginners Java and CORBA tutorial:
http://java.sun.com/developer/
onlineTraining/corba/corba.html
 More technical Java & CORBA tutorial:
http://java.sun.com/j2se/1.5.0/docs/
guide/idl/
 OMG Site:
http://www.omg.org
240-322 Cli/Serv.: rmiCORBA/13
continued
51
 Java
Programming with CORBA
Gerald Brose and others
John Wiley, 2001, 3rd ed.
http://java.coe.psu.ac.th/ForMember/
Books.html#Network
240-322 Cli/Serv.: rmiCORBA/13
52