5. www.SunilOS.com5
Java Remote Object Invocation (RMI)
Java RMI allows the programmer to execute a method of
remote class using the same semantics as local method
call.
Local Machine (Client)
AddServer remoteObject;
int s;
…
s=remoteObject.sum(1,2);
System.out.println(s);
Remote Machine
(Server)
public int sum(int
a,int b) {
return a + b;
}
1,2
3
6. www.SunilOS.com 6
The General RMI Architecture
The server must first bind its
name to the registry.
The client looks up the
server name in the registry
to establish remote
references.
The Stub serializes the
parameters to skeleton, the
skeleton invokes the remote
method and serializes the
result back to the stub.
7. www.SunilOS.com 7
The Stub and Skeleton
A client invokes a remote method, the call is first forwarded to
stub.
The stub is responsible for sending the remote call over to the
server-side skeleton.
The stub opens a socket to the remote server, marshals the
object parameters and forwards the data stream to the skeleton.
A skeleton contains a method that receives the remote calls,
unmarshals the parameters, and invokes the actual remote object
implementation.
Stub
RMI Client RMI Server
skeleton
return
call
8. www.SunilOS.com 8
Steps for Developing an RMI System
1. Define the remote interface.
2. Develop the remote object by implementing the remote
interface.
3. Develop the client program.
4. Compile the Java source files.
5. Generate the client stubs and server skeletons.
6. Start the RMI registry.
7. Start the remote server objects.
8. Run the client.
10. www.SunilOS.com 10
Step 1: Defining the Remote Interface
import java.rmi.*;
public interface AddServerIntf extends Remote
{
public int sum(int a,int b) throws
RemoteException;
}
11. www.SunilOS.com 11
Step 2: Defining the Remote Server
The server is a simple unicast remote server. Server is created by
extending java.rmi.server.UnicastRemoteObject.
The server uses the RMISecurityManager to protect its resources
while engaging in remote communication.
import java.rmi.*; import java.rmi.server.*;
import java.rmi.registry.*;
public class AddServerImpl extends UnicastRemoteObject implements
AddServerIntf {
AddServerImpl() throws RemoteException{
super();
}
….
12. www.SunilOS.com 12
Step 2: Defining the Remote Server ( Cont.)
// Remote method implementation
public int sum(int a,int b) throws RemoteException{
return a + b;
}
The server must bind its name to the registry, the client will look up the
server name.
Use java.rmi.Naming class to bind the server name to registry. In this
example the name of server is “ADD-SERVER”.
In the main method of your server object, the RMI security manager is
created and installed.
13. www.SunilOS.com 13
Step 2: Defining the Remote Server
Main method to start Server
public static void main(String args[]) throws Exception{
//set the security manager
System.setSecurityManager(new RMISecurityManager());
//create a local instance of the server object
AddServerImpl serverObj= new AddServerImpl();
//Bind server instance with RMI Registry
Naming.rebind("ADD-SERVER" , serverObj);
System.out.println("Server is started.....");
}
14. www.SunilOS.com 14
Step 3: Developing the Client program
Client will look up the name of server in the registry using
java.rmi.Naming class.
The server name is specified as URL:
o rmi://host:port/name
Default RMI port is 1099.
The name specified in the URL must exactly match the name that
the server has bound to the registry. In this example, the name of
server is “ADD-SERVER”.
The remote method invocation is programmed using the remote
interface name (remoteObject) as prefix and the remote method
name (sum) as suffix.
15. www.SunilOS.com 15
Step 3: Developing the client program (Cont.)
public class SampleClient{
public static void main(String[] args){
// set the security manager for the client
System.setSecurityManager(new RMISecurityManager());
//get the remote object from the registry
try {
String url = “rmi://localhost:1099/ADD-SERVER”;
AddServerIntf remoteObject =(AddServerIntf)Naming.lookup(url);
System.out.println(" SUM = " + remoteObject.sum(1,2) );
}catch (RemoteException exc) {
System.out.println("Error in lookup: " + exc.toString()); }
catch (java.net.MalformedURLException exc) {
System.out.println("Malformed URL: " + exc.toString()); }
catch (java.rmi.NotBoundException exc) {
System.out.println("NotBound: " + exc.toString());
}
}
16. www.SunilOS.com 16
Step 4 : Compiling Classes
Assume the program is in c:rmi
o c:rmi> set CLASSPATH=”c:/rmi”
o c:rmi> javac AddServerIntf.java
o c:rmi> javac AddServerImpl.java
o c:rmi> javac SampleClient.java
17. www.SunilOS.com 17
Step 5: Generating stubs and skeletons
The RMI system provides an RMI compiler (rmic) that
takes your RMI interface and class and generates stub
and skeleton.
Assuming that classes are stored in c:/rmi folder and
run following commands from command prompt:
o c:rmi> set CLASSPATH=”c:/rmi”
o c:rmi> javac AddServerIntf.java
o c:rmi> javac AddServerImpl.java
o c:rmi> rmic AddServerImpl
18. www.SunilOS.com 18
Step 6: Starting the RMI registry
The RMI Server objects are bound with the RMI Registry
and registry is started by calling command:
o rmiregistry
By default port #1099 is used by RMI Registry. You can
also bind registry to a different port number by indicating
the port number next to command as:
o rmiregistry <new port>
c:rmi>rmiregistry 1099
19. www.SunilOS.com 19
Steps 7: Starting Server and Client
First Registry is started then Server will be started and bound to
Registry by a unique name.
A security policy will be set when server is started. Granted
permissions are declared in a policy file. It is a text file. Policy file is
applied by –D command option:
o –Djava.security.policy=policy.all
Open a command prompt and start server by:
o c:rmi> java –Djava.security.policy=policy.all AddServerImpl
Open another command prompt and run client:
o c:rmi> java –Djava.security.policy=policy.all SampleClient
20. www.SunilOS.com 20
Java Policy File
The Java application must first obtain information
regarding its privileges. It can obtain the security
policy through a policy file.
In our example, we will allow Java code to have
all permissions. Policy file contents will be:
grant {
permission java.security.AllPermission;
};
21. Specific Permissions
grant {
permission java.io.filePermission “/tmp/*”, “read”, “write”;
permission java.net.SocketPermission “www.sunrays..co.in :
999”,”connect”;
permission java.net.SocketPermission “*:1024-65535”,”connect,request”;
permission java.net.SocketPermission “*:80”,”connect”;
};
Allows classes to read/write any file under the “/tmp” directory and its
subdirectories.
Allows classes to establish a network connection with the host
“www.SunilOS.com” at port 999.
Allows classes to connect to or accept connections on unprivileged
ports greater than 1024, on any host.
Allows all classes to connect to the HTTP port 80 on any host.
www.SunilOS.com 21
22. www.SunilOS.com 22
Run it!
Open Command Prompt 1
>rmiregistry
Open Command Prompt 2
>java -Djava.security.policy=policy.all AddServer
Open Command Prompt 3
>java -Djava.security.policy=policy.all AddClient
23. Disclaimer
This is an educational presentation to enhance the
skill of computer science students.
This presentation is available for free to computer
science students.
Some internet images from different URLs are
used in this presentation to simplify technical
examples and correlate examples with the real
world.
We are grateful to owners of these URLs and
pictures.
www.SunilOS.com 23