J2EE Tutorial - Running RMI Example
J2EE Tutorial - Running RMI ExampleJ2EE Tutorial - Running RMI Example
greeter.java
import java.rmi.*;
public interface greeter extends Remote
{
String greetme(String s) throws RemoteException;
}
greeterimpl.java
import javax.rmi.PortableRemoteObject;
import javax.naming.*;
import java.rmi.*;
import java.rmi.server.*;
import greeter.*;
public class greeterimpl extends
PortableRemoteObject implements greeter
{
String s;
public static void main(String args[])
{
try
{
Context initialNamingContext = new InitialContext();
// System.setSecurityManager(new RMISecurityManager());
greeterimpl obj = new greeterimpl("sam");
initialNamingContext.rebind("sam",obj);
System.out.println("remote server ready!");
System.out.println("sam is registered & waiting for call");
}
catch(Exception e1)
{
System.out.println("error"+e1);
}
}
public greeterimpl(String a) throws RemoteException
{
s = " SUN'S RMI-IIOP(RMI-CORBA) PROGRAM WELCOMES .........";
}
public String greetme(String a) throws RemoteException
{
return s+ a;
}
}
greeterclientservlet.java
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
import greeter.*;
import greeterimpl.*;
import _greeterimpl_Tie.*;
import java.rmi.*;
import java.rmi.server.*;
import javax.rmi.PortableRemoteObject;
import javax.naming.*;
import java.util.*;
public class greeterclientservlet extends HttpServlet
{
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws
ServletException,IOException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
try
{
String s1=request.getParameter("text1");
Hashtable hash1 = new Hashtable();
hash1.put("java.naming.factory.initial","com.sun.jndi.cosnaming.CNCtxFactory");
hash1.put("java.naming.provider.url","iiop://localhost:900");
Context context1 = new InitialContext(hash1);
greeter obj=
(greeter)PortableRemoteObject.narrow(context1.lookup("sam"),greeter.class);
String s = obj.greetme(s1);
out.println("<html>");
out.println("<body>");
out.println(s);
out.println("</body>");
out.println("</html>");
}
catch(Exception e1) { out.println("error"+e1); }
}
}
greeterclientservlet.htm
<html>
<body>
<form method=post action="http://localhost:8080/servlet/greeterclientservlet">
ENTER NAME <input type=text name="text1" size=20>
<br>
<input type=submit>
</form>
</body>
</html>
How to compile and run the RMI-IIOP program?
1) We require jndi package for running this program.
We create a folder in c:\jdk1.3\bin\rmicorba.
2) We brought jndi folder into c:\jdk1.3\bin\rmicorba.
3) cd to c:\jdk1.3\bin\rmicorba.
4) set path=c:\windows;c:\windows\command;c:\jdk1.3\bin;
5) set classpath=c:\jdk1.3\bin\rmicorba
6) create greeter.java in this folder.
7) compile greeter.java
8) create and compile greeterimpl.java
9) MOST IMPORTANT STEP.
>rmic -iiop greeterimpl
(creates corba style stub and tie class files.)
10) start tnameserv // ( this is transient name server)
>tnameserv
11) Go to another window ,set path & classpath as before.
start the server and register the remote object. (this line must be typed most carefully without break!) (continuously).
>java -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory
-Djava.naming.provider.url=iiop://localhost helloimpl
( -Djava means we are supplying runtime properties)
12) If every thing is done correctly , you will find the following
message in the console.
'remote server ready!'
'sam is registered and waiting for call'
13) Now compile greeterclientservlet.java
We have to set classpath=%classpath%;c:\jsdk2.0\src
14) After compiling copy all the classfiles in c:\jdk1.3\bin\rmicorba folder to
c:\tomcat\webapps\root\web-inf\classes folder
15) copy greeterclientservlet.htm to c:\tomcat\webapps\root directory.
16) Start the tomcat server as already seen.
17) type the URL as 'http://localhost:8080/greeterclientservlet.htm"
in the browser.
18) We get the form. Type your name ( say 'Thomas') and submit.
19) We will get the greeting :
" SUN'S RMI-IIOP(RMI-CORBA) PROGRAM WELCOMES ...Thomas"
20) Thus we have invoked the remote object's method in Corba style.
21) So far so good. But how about the automatic generation of IDL for non-java end? This is the best part of RMI--IIOP,
See step 9 listed above. We used the -iiop flag while invoking rmic compiler.
If we use -idl flag instead, we get the idl file automatically generated.
We can then distribute this idl file to other corba customers.
You would have noticed that , we have changed the order in which distributed order technologies in the j2ee basket were mentioned. Instaed of taking up JAVA-IDL after RMI, we took up RMI-IIOP. This way, we can easily compare RMI & RMI-IIOP.
Just to complete the picture, we will now see the JAVA-IDL version of the same program and then on to the meat of J2EE (IE) EJB.
There are 4 files as follows:
1) greeter.idl
2) greeterserver.java
3) greeterservlet.java
4) greeterservlet.htm
We begin with greeter.idl , which is the interface file written in OMG-IDL.
Let us edit this file in say, c:\idl folder.
Set path for the dos window
c:\idl>set path=c:\windows\command;c:\jdk1.3\bin
Also set classpath:
c:\idl>set classpath=c:\idl
Create greeter.idl as given below.
greeter.idl
interface greeter
{
string greetme(in string s);
};
We now use the idl compiler in jdk1.3 as follows:
c:\idl>idlj -fall greeter.idl
This command creates a number of java source files as follows:
1) greeter.java
2) _greeterImplBase.java
3) _greeterStub.java
4) greeterHelper.java
5) greeterHolder.java
6) greeterOperations.java
These files are created in the same folder as greeter.idl because we have not specified 'module' in the idl file. This is a simple method.
Now, create greeterserver.java in the same folder as follows:
greeterserver.java
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import java.io.*;
public class greeterserver
{
public static void main(String[] args)
{
try
{
ORB orb = ORB.init(args,null);
greeterservant ref = new greeterservant();
orb.connect(ref);
org.omg.CORBA.Object objRef =orb.resolve_initial_references("NameService");
NamingContext ncRef = NamingContextHelper.narrow(objRef);
NameComponent nc = new NameComponent("greeter","");
NameComponent path[] = {nc};
ncRef.rebind(path,ref);
System.out.println("server ready...waiting for client");
java.lang.Object sync = new java.lang.Object();
synchronized(sync)
{
sync.wait();
}
}
catch(Exception e1)
{
System.out.println(" "+e1);
}
}//main
}//class
//-------------
class greeterservant extends _greeterImplBase
{
public String greetme(String s)
{
return "How are you....?"+s;
}
}
Compile all the java files as follows:
c:\idl>javac *.java
This compiles all the java files in the folder.
We now create the servlet file which is the client for the corba program.
greeterservlet.java
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class greeterservlet extends HttpServlet
{
greeter server;
public void init(ServletConfig config) throws ServletException
{
super.init(config);
try
{
ORB orb = ORB.init((String[])null,null);
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
NamingContext ncRef = NamingContextHelper.narrow(objRef);
NameComponent nc = new NameComponent("greeter","");
NameComponent path[] = {nc};
server = greeterHelper.narrow(ncRef.resolve(path));
System.out.println("servlet init ok!");
} catch(Exception e1){System.out.println(""+e1);}
} //init
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String a =request.getParameter("text1"); // name
System.out.println(a);
String s1=server.greetme(a);
out.println(s1);
}
} //--servlet over-----
To compile the servlet file, we should set classpath as follows:
c:\idl>set classpath=%classpath%;c:\jsdk2.0\src;
c:\idl>javac greeterservlet.java
This command compiles the servlet.
The following html file invokes the servlet.
<html>
<body>
<form method=post action="http://localhost:8080/servlet/greeterservlet">
<input type=text name='text1'>
<input type=submit>
</form>
</body>
</html>
Now, we copy greeterservlet.htm to c:\tomcat\webapps\root
Then we copy all the class files in c:\idl folder to:
c:\tomcat\webapps\root\web-inf\classes folder.
We are now ready to test our corba program.
Start Tomcat as before.
Start the browser and type the URL as:
"http://localhost:8080/greeterservlet.htm"
We get a form with a textbox. Fill up your name,say Thomas and
submit.
We will get :
"How are you...?Thomas".
We have completed the first leg of our long journey.
In the second part, we will take up EJB , XML etc. In the previous instalment , we familiarized ourselves with Servlet, JSP, JavaMail, JDBC, RMI, RMI-IIOP and IDL. as, these were preliminaries to grapple with EJB. Now , it is time to takeup EJB.
Ads