Remote Method Invokation (RMI) in Java
# Remote Method Invokation (RMI) in Java #
- What is RMI ?
- RMI is an API that provide an mechanism to create distributed application in java.
- RMI allow an object to invoke the method of an object running on another JVM(Java virtual Machine).
- RMI provide Remote Communication to the application using two special objects which are stub and skeleton .
- what is stub ?
- stub is an object act as gateway for client side application.
- All outgoing request are routed by stub.
- Following are the tasks of stub.
- To establish the connection with the remote JVM.
- To write and trasmit the parameters to the remote object. (Marshalling)
- stub wait for the result.
- To read the return value from remote object.(Unmarshalling)
- To send the result to its caller.
- what is skeleton ?
- skeleton is an object act as gateway for server side application.
- All incoming request are routed by stub.
- Following are the tasks of skeleton.
- To read the parameters from caller
- Invoke the method
- To write and transmit the result to its caller .
- Steps to create remote application
- Create Remote Interface
- Create Class for implementing Remote Interface.
- Compile the class and interface and create stub and skeleton object using rmi tools.
- Start the registry service using rmiregistry.
- Create Class Server and Start server.
- Create Class Client and Start Client.
# Practical Implementation of RMI #
- Create Remote Interface
code : import java.rmi.Remote;
public interface AddI extends Remote
{
public int add(int a,int b) throws Exception;
}
- Create Remote Interface
public interface AddI extends Remote
{
public int add(int a,int b) throws Exception;
}
- Create Class for implementing Remote Interface.
code :
import java.rmi.server.*;
public class AddC extends UnicastRemoteObject implements AddI
{
public AddC() throws Exception
{
super();
}
public int add(int a,int b)
{
return a+b;
}
}
- Create Server Class
Code :
import java.rmi.*;
public class Server
{
public static void main(String a[]) throws Exception
{
AddC obj = new AddC();
Naming.rebind("ADD",obj); // Register the obj and create caption
System.out.println("Server started");
}
}
- Create Client Class
Code:
import java.rmi.*;
public class Client
{
public static void main(String a[]) throws Exception
{
AddI obj = (AddI)Naming.lookup("ADD");
int n = obj.add(4,5);
System.out.println("Addition is : "+n);
}
}
- Compile All Class and Interface
Note : you can compile them one by one or at onces if they are in one folder by
using command " javac *.java ".
- Create Stub.
- open one terminal and locate it to folder where classes are store .
- Execute command "rmic AddC ".
- " AddC_Stub.class " file were created.
- Start Registry.
- Execute command " start rmiregistry ".
- Start Server.
- open new terminal and Execute command " java Sever ".
- Start Client.
- open new terminal and Execute command " java Client ".
- you will get output " Addition is : 9 ".
THE END
Thnx For RMI Notes❤️
ReplyDeleteThanks for sharing your important notes
ReplyDelete