Module II
Remote Method Invocation: RMI architecture; RMI Object services; Naming/registry service,
object activation service, distributed garbage collection; Defining Remote objects; Key RMI classes for
remote object implementations; Stubs and skeletons; Accessing remote object as a client; Remote
method arguments and return values; Factory classes; Dynamically loaded classes; Configuring clients
and servers for remote class loading;
Remote Method Invocation
Java RMI provides the following elements:
Remote object implementations
Client interfaces, or stubs, to remote objects
A remote object registry for finding objects on the network
A network protocol for communication between remote objects and their client
A facility for automatically creating (activating) remote objects on−demand
Introduction to RMI
RMI is the distributed object system that is built into the core Java environment. RMI is a
built−in facility for Java that allows us to interact with objects that are actually running in Java
virtual machines on remote hosts on the network. With RMI wecan get a reference to an object
that "lives" in a remote process and invoke methods on it as if it were a local object running
within the same virtual machine as your code (hence the name, "Remote Method Invocation
API").
RMI was added to the core Java API in Version 1.1 of the JDK (and enhanced for Version 1.2
of the Java 2 platform), in recognition of the critical need for support for distributed objects in
distributed−application development.
Java RMI is a Java−only distributed object scheme; the objects in an RMI−based distributed
application have to be implemented in Java.
The advantages of RMI primarily revolve around the fact that it is "Java−native." Since RMI is
part of the core Java API and is built to work directly with Java objects within the Java VM.
You really can use RMI−enabled objects as if they live in the local Java environment. And
since Java RMI is built on the assumption that both the client and server are Java objects, RMI
can extend the internal garbage−collection mechanisms of the standard Java VM to provide
distributed garbage collection of remotely exported objects.
RMI in Action
, As an example, we can create an Account object that represents some kind of bank account
and then use RMI to export it as a remote object so that remote clients (e.g., ATMs, personal
finance software running on a PC) can access it and carry out transactions.
The first step is to define the interface for our remote object. The following example shows the
Account interface. You can tell that it's an RMI object because it extends the
java.rmi.Remote interface. Another signal that this is meant for remote access is that
each method can throw a java.rmi.RemoteException. The Account interface
includes methods to get the account name and balance and to make deposits, withdrawals, and
transfers.
Steps:
1. Create an interface & compile.
Eg:- vi Account.java
javac Account.java
2. Write the implementation class & compile. Also compile with rmic compiler to
generate stub and skelton.
Eg:- vi AccountImpl.java
javac AccountImpl.java
rmic −d /home/classes AccountImpl
3. Write the registration file, compile. Run the registry and run the program.
Eg:- vi AccountReg.java
javac AccountReg.java
rmiregistry &
java AccountReg
4. Write the client program, compile & run.
Eg:- vi AccountClient.java
javac AccountClient.java
java AccountClient
Example 3−1. A Remote Account Interface
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Account extends Remote
{
public String getName() throws RemoteException;
public float getBalance() throws RemoteException;
public void withdraw(float amt) throws RemoteException;
public void deposit(float amt) throws RemoteException;
}
Compile the program:
, $javac Account.java
The next step is to create an implementation of this interface, which leads to the
AccountImpl class shown in the following example. This class implements all the methods
listed in the Account interface and adds a constructor that takes the name of the new account
to be created. Notice that the AccountImpl class implements the Account interface, but
it also extends the java.rmi.UnicastRemoteObject class. This RMI class provides
some of the basic remote functionality for server objects.
Example 3−2. Implementation of the Remote Account Interface
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
public class AccountImpl extends UnicastRemoteObject implements Account
{
private float mBalance = 0;
private String mName = "";
// Create a new account with the given name
public AccountImpl(String name) throws RemoteException
{
mName = name;
}
public String getName() throws RemoteException
{
return mName;
}
public float getBalance() throws RemoteException
{
return mBalance;
}
// Withdraw some funds
public void withdraw(float amt) throws RemoteException
{
mBalance −= amt;
// Make sure balance never drops below zero
mBalance = Math.max(mBalance, 0);
}
// Deposit some funds
public void deposit(float amt) throws RemoteException
{
mBalance += amt;
}
}
}
Compile the program:
$javac AccountImpl.java
Export the CLASSPATH by adding path to the current directory
$ export CLASSPATH=$CLASSPATH:/home/bca3/
Compile the program using rmic compiler to generate stub and skelton:
Remote Method Invocation: RMI architecture; RMI Object services; Naming/registry service,
object activation service, distributed garbage collection; Defining Remote objects; Key RMI classes for
remote object implementations; Stubs and skeletons; Accessing remote object as a client; Remote
method arguments and return values; Factory classes; Dynamically loaded classes; Configuring clients
and servers for remote class loading;
Remote Method Invocation
Java RMI provides the following elements:
Remote object implementations
Client interfaces, or stubs, to remote objects
A remote object registry for finding objects on the network
A network protocol for communication between remote objects and their client
A facility for automatically creating (activating) remote objects on−demand
Introduction to RMI
RMI is the distributed object system that is built into the core Java environment. RMI is a
built−in facility for Java that allows us to interact with objects that are actually running in Java
virtual machines on remote hosts on the network. With RMI wecan get a reference to an object
that "lives" in a remote process and invoke methods on it as if it were a local object running
within the same virtual machine as your code (hence the name, "Remote Method Invocation
API").
RMI was added to the core Java API in Version 1.1 of the JDK (and enhanced for Version 1.2
of the Java 2 platform), in recognition of the critical need for support for distributed objects in
distributed−application development.
Java RMI is a Java−only distributed object scheme; the objects in an RMI−based distributed
application have to be implemented in Java.
The advantages of RMI primarily revolve around the fact that it is "Java−native." Since RMI is
part of the core Java API and is built to work directly with Java objects within the Java VM.
You really can use RMI−enabled objects as if they live in the local Java environment. And
since Java RMI is built on the assumption that both the client and server are Java objects, RMI
can extend the internal garbage−collection mechanisms of the standard Java VM to provide
distributed garbage collection of remotely exported objects.
RMI in Action
, As an example, we can create an Account object that represents some kind of bank account
and then use RMI to export it as a remote object so that remote clients (e.g., ATMs, personal
finance software running on a PC) can access it and carry out transactions.
The first step is to define the interface for our remote object. The following example shows the
Account interface. You can tell that it's an RMI object because it extends the
java.rmi.Remote interface. Another signal that this is meant for remote access is that
each method can throw a java.rmi.RemoteException. The Account interface
includes methods to get the account name and balance and to make deposits, withdrawals, and
transfers.
Steps:
1. Create an interface & compile.
Eg:- vi Account.java
javac Account.java
2. Write the implementation class & compile. Also compile with rmic compiler to
generate stub and skelton.
Eg:- vi AccountImpl.java
javac AccountImpl.java
rmic −d /home/classes AccountImpl
3. Write the registration file, compile. Run the registry and run the program.
Eg:- vi AccountReg.java
javac AccountReg.java
rmiregistry &
java AccountReg
4. Write the client program, compile & run.
Eg:- vi AccountClient.java
javac AccountClient.java
java AccountClient
Example 3−1. A Remote Account Interface
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Account extends Remote
{
public String getName() throws RemoteException;
public float getBalance() throws RemoteException;
public void withdraw(float amt) throws RemoteException;
public void deposit(float amt) throws RemoteException;
}
Compile the program:
, $javac Account.java
The next step is to create an implementation of this interface, which leads to the
AccountImpl class shown in the following example. This class implements all the methods
listed in the Account interface and adds a constructor that takes the name of the new account
to be created. Notice that the AccountImpl class implements the Account interface, but
it also extends the java.rmi.UnicastRemoteObject class. This RMI class provides
some of the basic remote functionality for server objects.
Example 3−2. Implementation of the Remote Account Interface
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
public class AccountImpl extends UnicastRemoteObject implements Account
{
private float mBalance = 0;
private String mName = "";
// Create a new account with the given name
public AccountImpl(String name) throws RemoteException
{
mName = name;
}
public String getName() throws RemoteException
{
return mName;
}
public float getBalance() throws RemoteException
{
return mBalance;
}
// Withdraw some funds
public void withdraw(float amt) throws RemoteException
{
mBalance −= amt;
// Make sure balance never drops below zero
mBalance = Math.max(mBalance, 0);
}
// Deposit some funds
public void deposit(float amt) throws RemoteException
{
mBalance += amt;
}
}
}
Compile the program:
$javac AccountImpl.java
Export the CLASSPATH by adding path to the current directory
$ export CLASSPATH=$CLASSPATH:/home/bca3/
Compile the program using rmic compiler to generate stub and skelton: