In this post we will see how RPC (Remote Procedure Call) mechanism can be implemented in Java.
RMI (Remote Method Invocation) is the special mechanism to do RPC in Java. RMI is the java flavor of RPC in other languages, but core concept is still same!
The main Idea is summarized as below:
In this example, we make a server (RMIServer.java) to receive input text from client application and replies how many characters it received.
Server Side:
- Create a remote interface. (myInterface.java)
- Create a separate java file to implement the remote interface. (RMIServer.java)
- Register the interface in RMI registry (bind with any name eg: myRMIService)
Client Side:
- In client code, through interface name (in our case myRMIService) create a "fake remote" object reference of server within client. (RMIClient.java)
- Through this reference access the server methods / services
Let us have a detailed look on code:
1. MyInterface.java
- import java.rmi.*;
- public interface MyInterface extends Remote
- {
- public String countInput(String input)throws RemoteException;
- }
- It simply says about a remote service to accept a string input and gives string output.
- Line no 4 insists that any code implementing this interface should have countInput method.
2. RMIServer.java
- import java.rmi.*;
- import java.rmi.server.*;
- public class RMIServer extends UnicastRemoteObject implements MyInterface
- {
- public RMIServer()throws RemoteException
- {
- System.out.println("Remote Server is running Now.!!");
- }
- public static void main(String arg[])
- {
- try{
- RMIServer p=new RMIServer();
- Naming.rebind("rmiInterface",p);
- }
- catch(Exception e)
- { System.out.println("Exception occurred : "+e.getMessage()); }
- }
- @Override
- public String countInput(String input) throws RemoteException
- {
- System.out.println("Received your input "+ input+" at server!!");
- String reply;
- reply="You have typed "+ input.length() +" letters!!";
- return reply;
- }
- }
- Line 3 says we are going to implement MyInterface interface
- Line 13 says we register this service with name rmiInterface. Any client can access the services from this class using this name.
- Line 19-27 implements the methods offered by interface.
3. RMIClient.java
- import java.rmi.*;
- import java.io.*;
- public class RMIClient
- {
- public static void main(String args[])
- {
- try
- { BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
- MyInterface p=( MyInterface)Naming.lookup("rmiInterface");
- System.out.println("Type something...");
- String input=br.readLine();
- System.out.println(p.countInput(input));
- }
- catch(Exception e) {
- System.out.println("Exception occurred : "+e.getMessage());
- }
- }
- }
Line 9: Looking for the remote service using the name!
Line 12: Accessing the service using the remote object.
RESULT:
Compile the java files.
RESULT:
Compile the java files.
Run remote object registry called rmiregistry. It is used by RMI servers on the same host to bind remote objects to names.
Run the Server class.
Run the client class. You can see it is prompting for input
Type some text
Input is received by remote server
Client receives reply from remote server
Hope this post was useful!!
Feel free to post your suggestions and comments!
12 comments :
Good job...!!
Thank you Abhinav Biswas, for your feedback :)
Great Information in the post
Thank you for your comment, West Bengal KPS Application..
I hope it was useful..
This site is very good
cbse result 2017 class 10 I like this website
Very helpful bro
@Unknown
Thank You!!
Frame Example
Full Subtractor
Functional Dependency Closure
TOC Functions
Fuzzy Logic
pragmatic processing
Thanks for sharing this information with us, i always like to read these articles, cool..!
24JobsExam Free Job Alert Bankers Adda
Check 24JobsExam govt job Portal Placement Store
this is rpc or rmi example?
I am new to java programming and also RPC can you tell me how to run client and server code on separate PC and for that what changes I need to do in the code?
Post a Comment