RMI (Remote Method Invocation)
1. stub
2. skeleton
5. RMI Example
The RMI (Remote Method
Invocation) is an API that provides a mechanism to create distributed
application in java. The RMI allows an object to invoke methods on an object
running in another JVM.
The RMI provides remote communication between
the applications using two objects stub and skeleton.
Understanding
stub and skeleton
RMI uses stub and skeleton
object for communication with the remote object.
A remote
object is an
object whose method can be invoked from another JVM. Let's understand the stub
and skeleton objects:
stub
The stub is an object, acts as
a gateway for the client side.
All the outgoing requests are routed through it. It resides at the client side
and represents the remote object. When the caller invokes method on the stub
object, it does the following tasks:
skeleton
The skeleton is an object, acts
as a gateway for the server side
object. All the incoming requests are routed through it. When the
skeleton receives the incoming request, it does the following tasks:
In the Java 2 SDK, an stub
protocol was introduced that eliminates the need for skeletons.
Steps to write the RMI program
The is given the 6 steps to
write the RMI program.
1)
create the remote interface
1.
import java.rmi.*;
2.
public interface Adder extends Remote{
3.
public int add(int x,int y)throws RemoteException;
4.
}
2)
Provide the implementation of the remote interface
Now provide the implementation of the remote
interface. For providing the implementation of the Remote interface, we need to
1. import java.rmi.*;
2. import java.rmi.server.*;
3. public class AdderRemote extends UnicastRemoteObject implements Adder{
4. AdderRemote()throws RemoteException{
5. super();
6. }
7. public int add(int x,int y){return x+y;}
8. }
3) create the stub and skeleton objects using the
rmic tool.
Next step is to create stub and
skeleton objects using the rmi compiler. The rmic tool invokes the RMI compiler
and creates stub and skeleton objects.
1. rmic AdderRemote
4) Start
the registry service by the rmiregistry tool
Now start the registry service
by using the rmiregistry tool. If you don't specify the port number, it uses a
default port number. In this example, we are using the port number 5000.
1.
rmiregistry 5000
5) Create
and run the server application
1.
import java.rmi.*;
2.
import java.rmi.registry.*;
3.
public class MyServer{
4.
public static void main(String args[]){
5.
try{
6.
Adder stub=new AdderRemote();
7.
Naming.rebind("rmi://localhost:5000/sonoo",stub);
8.
}catch(Exception e){System.out.println(e);}
9.
}
10. }
6)
Create and run the client application
1.
import java.rmi.*;
2.
public class MyClient{
3.
public static void main(String args[]){
4.
try{
5.
Adder stub=(Adder)Naming.lookup("rmi://localhost:5000/sonoo");
6.
System.out.println(stub.add(34,4));
7.
}catch(Exception e){}
8.
}
9.
}
1.
For running this rmi example,
2.
3.
1) compile all the java files
4.
5.
javac *.java
6.
7.
2)create stub and skeleton object by rmic tool
8.
9.
rmic AdderRemote
10.
11. 3)start rmi registry in one command prompt
12.
13. rmiregistry 5000
14.
15. 4)start the server in another command prompt
16.
17. java MyServer
18.
19. 5)start the client application in another command prompt
20.
21. java MyClient
|
Computer Science :: 11th/12th, Computer Engineering :: Degree/Diploma, Coding Concept ( C, C++ & JAVA Programming, JDBC Concept, DBMS - SQL Concept, Data Structure, Communication & Network Concept.
Tuesday, December 1, 2015
RMI IN JAVA
Subscribe to:
Post Comments (Atom)
JAC Board Computer || IIT 2025 || Introductory Information Technology || Class 10 || Practical Examination 2025
Marks Division HOE 30 Marks IT Application Report File 20 Marks Viva - Voce 10 Marks 1) Write an HTML code to generate a web p...
-
1) TCP/IP - Transmission Control Protocol/ Internet Protocol. 2) HTTPs - Hyper Text Transfer Protocol. 3) ICT - Information Comunication Te...
-
1) What is Make Code Arcade? Answer: Make Code Arcade is a coding platform that enables to create block – based program. 2) How ...
-
Programmingway@Eklavya Technosys!: New Year's 2025 | Happiness & Healthier's Life Sty... : भारत कई धर्मों का संगम है, हिंदू, मुस...
No comments:
Post a Comment