RMI Remote Method Invocation

Download Report

Transcript RMI Remote Method Invocation

RMI Remote Method
Invocation
Y. Daniel Liang, Introduction to
Java Programming
Introduction
Sun developed a simple mechanism called Remote Method Invocation just for
communicating between Java applications.
Remote Method Invocation (RMI) technology provides a framework for building
distributed Java systems.
Using RMI , a Java object on one system can invoke a method in an object on
another system on the network.
A distributed Java system can be defined as a collection of cooperative distributed
objects on the network. The method appears local to the programmer.
Java.rmi package provides a standard way for a server to permit method invocations
on its object. An RMI server creates local objects and makes them available to RMI
clients as remote objects.
Y. Daniel Liang, Introduction to
Java Programming
Benefits of RMI
Remote Method Invocation (RMI) applications are scalable and easy to maintain
RMI provides a mechanism that frees you from writing tedious code for handling
parameter passing and calling remote methods. JDK 1.5 simplified RMI development
and deployment.
You can change the RMI server or move it to another machine without changing
the client program with one exception. You must reset the URL to find the server.
To overcome this exception, you can pass the URL as a command-line parameter
to the client program.
RMI clients can directly invoke the server method, while socket-level programming
Is limited to passing values.
Socket-level programming is very primitive. Avoid using it to develop client/server
applications Socket-level programming is similar to programming in assembly
language while RMI is similar to programming in a high-level language.
.
Y. Daniel Liang, Introduction to
Java Programming
Benefits continued
• Another important benefit of RMI is that it
supports callbacks, which enable the server
to invoke methods on the client
With this feature, you can develop interactive
distributed applications.
Y. Daniel Liang, Introduction to
Java Programming
Fundamental RMI classes
Object
RemoteObject
RemoteStub
Remote
<<interface>>
RemoteServer
(an abstract class)
Unicast
RemoteObject
resides on a server
Y. Daniel Liang, Introduction to
Java Programming
The Remote Interface
• To create a remote interface, define an
interface that extends interface
java.rmi.Remote
• Remote is a tagging interface and it
does not declare any methods
Y. Daniel Liang, Introduction to
Java Programming
The Remote Interface
• Every method in a Remote interface must specify that it throws
a RemoteException
• All method arguments and return values must be either
primitive types (char, int, double, or boolean). They
are passed-by-value like a local call
• Local object types, like java.lang.String are also passed-byvalue
Other objects types must implement the Serializable
interface. Note can not pass the object reference because the
address on one machine is meaningless to a different JVM.
Y. Daniel Liang, Introduction to
Java Programming
RMI Basics
• RMI is the Java Distributed Object Model that provides an efficient way
for objects to communicate with distributed objects.
• RMI is a higher-level API built on top of sockets.
• Socket-level programming allows you to pass data through sockets
among computers.
• RMI enables you to not only pass data among objects on different
systems, but it also allows you to invoke methods in a remote object.
• Remote objects can be manipulated as if they were residing on the local
host. The transmission of data among different machines is handled
transparently by the JVM.
Y. Daniel Liang, Introduction to
Java Programming
RMI Basics continued
•
RMI is an evolution of the client/server technology
•
A clients makes a request for a services. For example, The clients retrieves the
Student. scores from an RMI server.
•
A server honors the request and return the service to the client.
The findScore method retrieves a student score.
•
RMI maintains the concept of client and server. But its capability is more flexible
and powerful than the client server paradigm
An RMI component can be both a client and a server based on the
required scenario.
An RMI system can pass functionality from a server to a client, and vice
versa. A client/server system usually passes only data between the two.
Y. Daniel Liang, Introduction to
Java Programming
RMI works as follows:
1. A server object is registered with the RMI registry
2. A client looks through the RMI registry for the remote
object.
3. Once the remote object is located, its stub is returned to
the client. The stub method resides on the client machine
not on the server machine.
4. The stub packages(converts) the parameters used in the
remote method into a group of bytes suitable to transport
from one machine to another. This conversion is called
marshalling.
5. The remote object can be used in the same way as a local
object. Communications between the client and the server
is handled through the stub and the skeleton.
Y. Daniel Liang, Introduction to
Java Programming
The Ways RMI Works
RMIC Compiler
creates
Client Host
Server Object
Interface
(4) Data
Communication
Client
Program
Server Host
Server Object
Interface
Server
Stub
Server
Skeleton
(3) Return Server
Stub
RMI Registry Host
(2) Look for Server Object
by name
Server
Object
(1) Register Server
Object by name
RMI
Registry
Java RMI uses a registry to provide naming services for remote objects, and
Uses stub and the skeleton to facilitate communications between client and server.
Y. Daniel Liang, Introduction to
Java Programming
See Programs, UML Diagrams and instructions in the RMI folder::
SmartAlex.java and .class
ResponseClient.java and .class
Rudness.java and .class
SmartAlex_stub.class
See SmartAlexResponseClient Rudness DiagramNew.doc
See SmartAlex Dos Run.doc for instructions on how to run the
programs.
Y. Daniel Liang, Introduction to
Java Programming
Application to Retrieve Student Scores from an RMI Server
The Steps to develop the RMI Application
Step 1. Define Object Interface
interface StudentServerInterface
Step2. Define Server Implementation Class
ServerInterfaceImp.java
Step3. Create and Register Server Object
RegisterWithRMIServer.java
Step4. Develop a Client class
StudentServerInterface.java
Y. Daniel Liang, Introduction to
Java Programming
Instructions to Run the RMI Application
Place all the programs on the Desktop. Compile all the programs . Run all the programs
from a different Command window on the desktop
1. Start the RMI Registry by typing “ rmiregistry” at a DOS prompt from the
Desktop folder
OR
By default, the port number 1099 is used by rmiregistry. To use a different port
number, simply type the command “ rmiregistry portnumber” at a DOS
prompt
2. Run the RMIC compiler: on the server code in a different DOS window
Desktop> RMIC StudentServerInterfaceImpl
Start the server RegisterWithRMIServer using the following command at
the Desktop folder:
Desktop> java RegisterWithRMIServer
Y. Daniel Liang, Introduction to
Java Programming
Instructions to Run the RMI Application contd.
3. Run the client StudentServerInterfaceClient as an application on the Desktop:
Desktop> java StudentServerInterfaceClient
4. Run the client StudentServerInterface.html from the applevtiewer.
Y. Daniel Liang, Introduction to
Java Programming
Summary
• RMI allows us to distribute our program
across a network
• We can bind an object in the
rmiregistry server so that an object
in another application can look it up and
invoke its methods
• The rmic compiler creates the skeleton
and stub needed to call remote methods
• Remote objects use interfaces to
declare their remote methods
Y. Daniel Liang, Introduction to
Java Programming