Title: Java RMI
1Chapter 13
2What is RMI?
- Remote Method Invocation (RMI) allows a Java
object to call public methods on another java
object which may exist on another machine. - This protocol supports a client/server
architecture (where a client is the calling
object and the server is the object being called).
3How RMI Works
- Objects that want to communicate via RMI will use
classes from java.rmi package. - A server object that wants to publish its public
methods, making them available to remote client
objects, must create a Remote Stub class. - A client will then use this stub to instantiate
an object that is actually a pointer to the real
object on the server. - Methods called on this local object will invoke
the real methods of that object on the server.
4RMI Architecture
- Client Object Any Java class (servlet, bean,
applet, etc) - Stub Object Remote interface for the server
object, thats compiled and shipped as part of
the client package. - Server (Remote) Object Actual instantiated
object that client is calling remotely. - Skeleton Translates the call from the client
(retired in JDK 2).
5RMI Architecture
- A message sent by a client is Marshalled over the
network and recd by the server object. In JDK
1.1, the Skeleton object receives the marshalled
message. In JDK 2.0, the RMI libraries handle it. - Transport protocols such as Java Remote Method
Protocol (JRMP) are used, and will sometimes
serialize an object before marshalling.
6RMI Architecture
- All network related code is contained within the
RMI framework. Developers should not worry about
how marshalling is done, for instance. - Security is outside the scope of this lecture,
but is important for real world applications that
are accessible outside a firewall (client is
outside, server is inside).
7Client Architecture
- Clients that need to access a server object must
have the server objects stub class as part of
its package. - Once this is imported, a client application can
instantiate an object of that type by doing an
RMI or JNDI Lookup for that remote object, and
then use this object instance as if it were just
another local object. - Client must be prepared to catch RMI-level
exceptions, network errors or custom server
exceptions.
8Client Architecture
- Example Stub Interface
- public interface DateTimeRemote extends
Remote static String SERVER_NAME
objDayTime - String getDateTime() throws RemoteException
- void setDateTime( Date dt )
- throws RemoteException
9Client Architecture
- Example RMI Client
- ...
- static DateTimeRemote objDayTime
- public myClient extends Applet
-
-
- objDateTime (DateTimeRemote)
Naming.lookup(DateTimeRemote.SERVER_NAME) - println(objDateTime.getDateTime())
- ...
10Server Architecture
- To make a server object available remotely, you
must - have the RMIREGISTRY program running on the
server - create a java runnable class which extends a
remoteobject class and implements the remote
interface for that object - A small class with a main() can be run which acts
as the server, and receives requests by remote
clients.
11Server Architecture
- Example RMI Server
- public class Server extends UnicastRemoteObject
- implements DateTimeRemote
-
- public Server() throws RemoteException
super() -
- public String getDateTime() throws
RemoteException - // code here ..
- public void setDateTime( Date dt ) throws
RemoteException - // code here
- public static void main(String args)
-
- DateTimeRemote objDateTime new Server()
- Naming.rebind(DateTimeRemote.SERVER_NAME,
objDateTime) -
12Example of Bidirectional Messaging via RMI
- This is a chat program, where each client
registers with a single server, and each time any
client sends a message, that message is
broadcasted back to all registered clients. - Components of this application are
- Message Receiver
- Message Server
- Server Applications
- Client Application(s)
13Message Receiver
- Extends the RMI Remote interface class
- Contains a print method that is used by the
server to print each incoming message to each
client that is registered. - A client will IMPLEMENT this class (meaning itll
have a public method called print). - A server will instantiate each registered client
and cast them to this class so it can use this
method to make the client print. This is done by
RMI.
14Message Server
- Extends the RMI Remote interface class.
- Contains the send method that is used by the
client to send a message to the server. - A server will implement this interface, and
define the send() message that clients will use. - A client will instantiate this class, and via
RMI, will call the send method.
15Server Application
- Java application that takes requests from all
registered client applications. - Implements the MessageServer.send and
MessageServer.register methods to be used by each
client. - The server will connect to each client via RMI
and store that pointer in an enumerated class
(assuming itll be connected to more then one
client). - Server will then use each clients
MessageReceiver.print method to broadcast each
message received to ALL clients.
16Server Application
Public class Server extends UnicastRemoteObject i
mplements MessageServer // c enumeration
of each registered client ... public void
send() RMI MessageReceiver m
(MessageReceiver) c.nextClient() m.print(
message ) ... public int register()
c.addClient() ...
17Client Application
- Java application, with a single frame containing
a - TestArea object used for receiving messages from
the server - TestField object used for sending messages to the
server - Implements the MessageReceiver.print() method so
the server can call it. - Connects to the server via RMI and uses the
servers send message to deliver a new message to
the server.
18Client Application
Public class Client extends Frame implements
MessageReceiver, etc public static void
main() RMI MessageServer s (MessageServer)
Naming.lookup( MessageServer.SERVER_NAME)
... when finished entering new
message s.send( clientname, message )
19Homework
- Implement the Bidirectional Messaging server with
the following enhancements - User can specify how message is sent, either
broadcasted or point-to-point. - For broadcasting, the algorithm in the book
applies. - For point-to-point, another client name will be
entered and only that client will see the message
that is sent.