Title: Advanced Java Unit 4 RMI
1Advanced JavaUnit 4RMI
2Agenda
- Present some ideas about distributed computing
- Briefly look at CORBA and contrast it with RMI
- Look at the Java mechanism for Remote Method
Invocation (RMI) - See some examples
3Overview of RMI Applications
- Pure Java answer to RPC, DCOM, and CORBA
- CORBA allows object on different machines to
communicate - RMI allows objects on different JVMs potentially
on different machines to communicate
4CORBA
- The Common Object Request Brokering System
(CORBA) glues together diferent objects models
regardless of implementation language - In order to do so it uses a common interface
definition language that needs to be compiled
separately for every language for which there is
a binding
5Introduction to CORBA
- So what is an object (class, interface) in the
CORBA sense? - Collection of data items usually called
attributes or slots - Collection of behavioral attachments usually
called methods
6What Is Distributed Computing All About
- Goal is to break up a monolithic application into
smaller components - Client - user of a resource
- Server - provider of a resource
Request
Client
Server
Response
7Technical Overview of Architecture
- The client and object implementation are isolated
from the ORB by an IDL interface - CORBA requires that objects be defined by OMG
IDL - A method request does not pass directly from
client to server but rather is mediated by the
ORB - Method invocation is the same whether the object
is local or remote
8CORBA Interfaces
- IDL - interface definition language
- CORBA is language independent
- Can provide a standard notation for software
components - IDL supports library function interfaces just as
well as distributed objects across a network
C
IDL
Client
ORB
9IDL - Separating Interface From Implementation
- A standard notation language for defining
interfaces to objects - OMG IDL is a simple subset of C
- IDL is like a contract for defining an API
- Designed to be efficient across networks
10Role of IDL
Client Side
Object Implementation Side
COBOL
C
I D L
I D L
I D L
I D L
COBOL
Ada
I D L
I D L
ORB
I D L
I D L
Small talk
I D L
I D L
I D L
I D L
Small talk
C
C
JAVA
JAVA
11How the IDL is Used
IDL
IDL Compiler
Object Impl Code
Client Code
Skeleton Code
Stub Code
Language Compiler and Linker
Client
Object
Stub
Skel
ORB
12Programming Steps
- Define the IDL interfaces
- Implement these interfaces with C classes
- Write a client mainline to bind to server
- Write a server mainline which creates instances
of the classes - Register the server
13Comparison of RMI and CORBA
- RMI is Java to Java based
- JNI allows Java to other languages
- No mapping of objects no IDL
- RMI deals only with byte code
- No complicated request broker - just a simple
registry - Makes a distinction for a remote object -allows
errors to be handled
14RMI
- An Overview of RMI Applications
- Looking at a Simple Example
- Walking through a RMI Server
- Walking through a Client Program
- Running the applications
- Next time
- Additional Examples
- Reviewing Some More Examples
15An Overview of RMI Applications
- Two Parts to a RMI based distributed object
application - Server
- creates remote objects
- makes them available to client
- waits for clients to invoke methods these remote
objects - Client
- Use the references to the remote Objects
16RMI Distributed Object Applications
- Locate remote objects
- Communicate with remote objects
- Load class bytecodes for objects that are passed
around
17Locating Remote Objects
- Applications can use one of two mechanisms to
obtain references to remote objects. - An application can register its remote objects
with the rmiregistry - RMI's simple naming
facility - An application can pass and return remote object
references as part of its normal operation
18Communicating with Remote Objects
- Details of communication between remote objects
are handled by RMI - From the perspective of the programmer its as if
the objects are local - There is some overhead that makes it slower but
the communication looks like a standard Java
method invocation
19Loading the Class
- The types and the behavior of an object,
previously available only in a single virtual
machine can be transmitted to another, possibly
remote, virtual machine. - Contrast with the reading and writing of objects
that we have discussed in the previous units
20Remote Interfaces, Objects and Methods
- A distributed application built using Java RMI is
made up of interfaces and classes - An object becomes remote by implementing a remote
interface, which has the following
characteristics - A remote interface extends the interface
java.rmi.Remote. - Each method of the interface declares
java.rmi.RemoteException in its throws clause, in
addition to any application-specific exceptions.
21Remote Objects
- RMI treats a remote object differently from a non
remote object when the object is passed from one
virtual machine to another. - Rather than making a copy of the implementation
object in the receiving virtual machine, RMI
passes a remote stub for a remote object. - The stub acts as a proxy for the remote object
and is basically a remote reference. - The caller invokes a method on the local stub,
which is responsible for carrying out the method
call on the remote object.
22Layers
Client
Server
Stub
Skeleton
Remote Reference Layer
Transport Layer
23What happens Where?
- Layer 1 is the Application Layer
- Layer 2 is the client stub/skeleton layer. These
are the proxy objects these are produced by the
rmic command - Layer 3 is the remote reference that deals with
the the actual remote invocations - Layer 4 is the transport layer responsible for
actually setting up the connections and handling
the transport of data between machines
24Using Remote Objects
- A stub for a remote object implements the same
set of remote interfaces that the remote object
implements - This allows a stub to be cast to any of the
interfaces that the remote object implements - Only those methods defined in a remote interface
are available to be called in the receiving
virtual machine.
25Using RMI General Steps
- Design and implement the components of your
distributed application. - Compile sources and generate stubs and skeletons
- Make classes network accessible
- Start the application.
26Design and Implement the Application Components
- Defining the remote interfaces
- Implementing the remote objects
- Implementing the clients
27Compile Sources and Generate Stubs
- This is a two-step process.
- In the first step you use the javac compiler to
compile the source files containing the
implementation of the remote interfaces and
implementations, the server classes, and the
client classes. - In the second step you use the rmic compiler to
create stubs for the remote objects. RMI uses a
remote object's stub class as a proxy in clients
so that clients can communicate with a particular
remote object.
28Make Classes Network Accessible/ Starting
- In this step you make everything--the class files
associated with the remote interfaces, stubs, and
other classes that need to be downloaded to
clients--accessible via a Web server - Starting the application includes running the RMI
remote object registry, the server, and the
client.
29The Four Required Classes
- To use RMI you need to build man classes
- An interface for the remote object to be used by
both the client and the server - The RMI client which looks up the object on a
remote machine, cast it to the needed type of the
interface given in the step 1 - The object implementation implements the
interface of step 1 - The RMI server which creates an instance of the
object from step 3
30Steps for Compiling and Running the System
- Compile the client and the server
- Generate the client stub using rmic
- Start the RMI registry this only needs to be
done once - Start the the server done on same machine as
in step 3 - Start the client this doesnt have to be on the
same machine as 3 and 4
31A Simple Example
- In this example the remote object just returns a
message string - The interface
- The client
- The Remote Object Implementation
- The RMI Server
32The interface
import java.rmi. / The RMI client will use
this interface directly. The RMI server will
make a real remote object that implements
this, then register an instance of it with
some URL. / public interface Rem extends
Remote public String getMessage() throws
RemoteException
33The RMI Client
- Looks up the object from the appropriate host
using Naming.lookup - Casts it to the appropriate type then uses it
like a local object - Unlike CORBA RMI has to know the host that is
providing the remote services - uses URL of the form rmi//host/path or rmi
rmi//hostport/path - the default port is 1099
34The RMI Client
- A number of exceptions must be caught
- RemoteException
- NotBoundException
- MalformedURLException
- Requires java.rmi for RemoteException as well
Naming and and NotBound Exception - Requires java.net for MalformedURLException as we
have already seen - Many Clients will pass Serializable objects to
the remote objects so importing java.io is
usually done as well not needed here however
35The Client Code
- Import the needed Packages
import java.rmi. // For Naming,
RemoteException, etc. import java.net. // For
MalformedURLException import java.io. //
For Serializable interface
36The Client Code Body
- public class RemClient
- public static void main(String args)
- try
- String host (args.length gt 0) ? args0
"localhost" - // Get the remote object and store it in
remObject - Rem remObject
- (Rem)Naming.lookup("rmi//" host
"/Rem") - // Call methods in remObject
- System.out.println(remObject.getMessage())
- //catch the exceptions
-
-
-
Can now use like a local object
37Catching the Exceptions in the Client
- try
- //
- catch(RemoteException re)
- System.out.println("RemoteException "
re) - catch(NotBoundException nbe)
- System.out.println("NotBoundException "
nbe) - catch(MalformedURLException mfe)
- System.out.println("MalformedURLException
" - mfe)
- //
38The Remote Object Implementation
- This class must extend UnicastRemoteObject and
implement the interface discussed previously - The constructor show throw a RemoteException
39The Remote Object
import java.rmi. import java.rmi.server.UnicastR
emoteObject public class RemImpl extends
UnicastRemoteObject
implements Rem public RemImpl() throws
RemoteException public String getMessage()
throws RemoteException return("Here is a
remote message.")
40The RemImpl Object
- This is the actual implementation of Rem that the
RMI server uses. - The server builds an instance of this then
registers it with a URL - The client accesses the URL and binds the result
to Rem (not a RemImpl it doesn't have this).
41The RMI Server
- Builds an object and registers it with a
particular URL using Naming.rebind to replace any
other bindings or Naming.bind which throws a
AlreadyBoundException if there is a previous
binding - Bind really means register here
- You have to catch RemoteException and
URLMalformedException
42RemServer
mport java.rmi. import java.net. public
class RemServer public static void
main(String args) try RemImpl
localObject new RemImpl()
Naming.rebind("rmi///Rem", localObject)
catch(RemoteException re)
System.out.println("RemoteException " re)
catch(MalformedURLException mfe)
System.out.println("MalformedURLException "
mfe)
Makes an Object
Registers an Object
43Putting it all Together
- Compile the Client and the Server
- javac RemClient.java RemServer.java
- Generate the Client Stub and Server Skeleton
- rmic RemImpl
- Starts the RMI registry
- Rmiregistry
- Start the server Start the
client - java RemServer java RemClient
44Next Unit
- RMI/CORBA Issues
- More Examples
- Start Chapter 11 - Threads
45Next Time
- Study Chapter 7 and review Example
- Homework posted Monday AM
- Read Chapter 11 - Threads
46Selected References
- Advanced Techniques for Java Developers Chapter
6,7 - http//java.sun.com/docs/books/tutorial/rmi/index.
html - Exploring Java, OReilly, Niemeyer Peck
- Java RMI Remote Method Invocation, Troy Downing,
IDG Books - The RMI example comes from Core Web Programming,
Marty Hall, Prentice Hall