When I was looking some good references to learn RPC with Java, I realized that it is very hard to find good and complete reference for it. So I have decided to Blog about how to implement and run distributed application in Java by using RPC (Remote Procedure Call) with the help of Netbula JRPC.
What is Remote Procedure call in Distributed application ?
Well I' m not soo good at memorizing definitions ;) so I'm putting wiki's definition here :)
According to the Wikipedia,
" In computer science, a remote procedure call (RPC) is an inter-process communication that allows a computer program to cause a subroutine or procedure to execute in another address space(commonly on another computer on a shared network) without the programmer explicitly coding the details for this remote interaction."
Ok ok enough theories :) lets get back to our topic...
In order to understand this article you should have basic knowledge about Java and Distributed application architecture.
Within this article I will show you how to implement and run simple hello world application with RPC, but I' m not gonna go into deep in theories.
System Requirements...
1. Java Development Kit (JDK) 1.6 or higher
2. Java Runtime Environment (JRE) 1.6 or higher
Note: When i was implementing this application I have worked on Windows 7 - 64 bit edition. So the procedures may be different in other platforms.
So what should be the first thing to do ?
First of all you need to download Netbula package form their official site.
Link to download page (get the link at the bottom) and remember we need to download both client and server editions of it. (tick the check box carefully)
Then,
extract your downloaded file into any folder as your wish.
Now go to "<<folder_location>>\orpc.dev\bin\win32" directory and copy ' jrpcgen.exe ' file into new folder named ' rpc_root ' anywhere you want.
It is time to write IDL file to our distributed application :)
within your ' rpc_root ' file create file named ' HelloWorld.x ' and insert following code into that file...
program msgserv {
version MSGSERV_V1 {
string sendmsg(string)=2;
}= 1;
} = 1234567;
now open windows command prompt and change your working directory into your ' rpc_root ' by simply typing " cd << path to rpc_root >> "
now we can generate necessary client stub and server stub (sometimes called as server skeleton) by using netbula jrpcgen tool.
execute following command in your cmd.
jrpcgen HelloWorld.x
msgserv.java
msgserv_cln.java
msgserv_svcb.java
then create 3 directories within your 'rpc_root' directory which are named as 'rpc_client', 'rpc_server' and 'portmapper' respectively.
now copy 'msgserv.java' and 'msgserv_cln.java' into 'rpc_client' directory.
copy 'msgserv.java' and 'msgserv_svcb.java' into 'rpc_server' directory.
now you can delete all other files within 'rpc_root' if you want.
Now we are ready to implement our client and server :)
go to directory where you have extracted netbula package, there you will find 'lib' directory.
go to 'lib' directory and copy 'orpc.jar' file into both 'rpc_client' , 'rpc_server' and 'portmapper' directories within your 'rpc_root' directory.
wait, You have another file to copy ;)
you will find 'pmapsvc.class' file within 'portmapper' directory where you have extracted netbula package, copy that file into 'portmapper' directory within your 'rpc_root'.
Implementing RPC Server...
go to 'rpc_server' directory and create file named 'RPCServerImplementation.java' in it, and insert following code into that file.
import netbula.ORPC.rpc_err;
public class RPCServerImplementation extends msgserv_svcb {
public RPCServerImplementation() {
}
public String sendmsg(String msg) {
System.out.println("got msg from client " + msg);
return msg;
}
public static void main(String srgv[]) throws rpc_err {
new RPCServerImplementation().run();
}
}
now open cmd then change directory into 'rpc_root/rpc_server' and execute following statement inorder to complie our rpc server.
javac -classpath ".;orpc.jar" *.java
Implementing RPC Client...
go to 'rpc_root/rpc_client' and create file named 'RPCClientImplementation.java' and insert following code into it.
import netbula.ORPC.*;
import java.net.*;
public class RPCClientImplementation {
public RPCClientImplementation() {
}
public static void main(String args[]) {
try {
msgserv_cln cl = new msgserv_cln(args[0], "udp");
String msg = "hello world :P \n";
System.out.println("sending message.. ");
String reply = cl.sendmsg(msg);
System.out.println("got " + reply + "\n");
} catch (rpc_err e) {
System.out.println("rpc: " + e.toString());
}
}
}
To compile above code, open cmd and change directory into 'rpc_root/rpc_client' then execute following statement...
javac -classpath ".;orpc.jar" *.java
Running Application...
We can run this application's both server and client in single computer (localhost), Or we can run them in separate machines.
First we will look at how we can run this distributed application in a single machine.
In order to run this application in each way, there is a procedure to follow.
1. Run RPC portmapper in server machine.
2. Run RPC server module
3. Run RPC Client module
Running PortMapper
In order to run portmapper follow the following instructions.
1. open dedicated command prompt for portmapper and change directory into 'rpc_root/portmapper'
2. execute following command in cmd
Note : above process will run until you interrupt that process, so you can't use that particular command prompt window for any other purpose during that time.
Running RPC Server...
In this case you have to decide whether you are going to run this application in 2 different computers or with a single computer.
It doesn't matter whether you are going to run this application with one computer or with 2 computers, because in each case you must run the portmapper in particular computer.
then, to run RPC server...
1. Open dedicated command prompt and change its directory to 'rpc_root/rpc_server'
2. execute following command.
java -cp ".;orpc.jar" RPCServerImplementation
Note : if you are planning to run this application in 2 different computers then you must run portmapper in a computer where you are going to run the RPC server module. (in other words, computer of the portmapper must be as same as the computer of RPC server)
Note : above process will run until you interrupt that process, so you can't use that particular command prompt window for any other purpose during that time.
Running RPC Client...
If you are running server and client in a same computer then it is easy ;) because you don't have to worry about exceptions caused by network issues :D
If you done with above 2 steps (running portmapper and RPC server within a same machine) then you are ready to run your RPC client.
follow the following steps in order to run RPC client application...
1. Open dedicated command prompt for RPC client and change its directory into 'rpc_root/rpc_client'
2. Execute following command
java -cp ".;orpc.jar" RPCClientImplementation localhost
If you are running client in a different computer then first of all send ping request to server computer from client computer and check whether your network connection is working properly. (In this case my remote server address is : 169.254.63.89)
then replace remote address (in this case 'localhost' host name) with the IP address of RPC server's computer as follows...
java -cp ".;orpc.jar" RPCClientImplementation 169.254.63.89
And That is it :)
The main objective of this article is to show how to implement and run simple RPC application on java. I have shown the way of doing this in very simple manner, but we can do this application within 5 min. time with the help of latest IDEs and build tools.
- Together For All
References :






Wow...this is a very good article on RPC. Thanks for sharing
ReplyDeleteWell done macho... Keep up the good work !!
ReplyDeleteGreat Work !!
ReplyDeleteIn linux when setting the CLASSPATH instead of ";" you need to use ":"
ReplyDeleteExcellent
ReplyDeleteWhat are wordsssss...Really important... thumbs up
ReplyDeleteThankzz 4 everything
Thank you very much machan... please write on the rest of the syllabus too :)
ReplyDeletegreat !! thanks machan
ReplyDeleteThank you very much Buddhi...! you are always helping us in our programming assignments... :)
ReplyDeleteit's expired the package.
ReplyDeletehow to fix it?