Asked By
Sumon Rana
280 points
N/A
Posted on - 05/11/2011
I have two servers running Linux. A website is on one and the database with an EJB module on the other. I need to access the remote EJB methods from the web project. How do I get about it?
Answered By
TekGirl
5 points
N/A
#91821
Need to invoke a EJB method running on a different machine
We need to know the application server that you are using in order to help you better. Are you using JBoss or Glassfish? Are you using EJB 2.0 or EJB 3.0? This is because the method of calling a remote object differs from platform to platform.
Answered By
DimaZ
0 points
N/A
#91822
Need to invoke a EJB method running on a different machine
Are you using dependency injection or doing a context lookup to get the EJB object? Dependency Injection or DI is a new concept introduced in Java 6. It allows you to define resources using annotations. Java 6 reads these annotations and automatically does a context lookup for you.
Need to invoke a EJB method running on a different machine
I am using Glassfish version 3.0.1. The EJB project is using the EJB 3 specification with dependency injection. I have setup my local machine as well, in an attempt to access the remote EJB project. Context lookup still fails.
Answered By
TekGirl
5 points
N/A
#91824
Need to invoke a EJB method running on a different machine
Can you create a simple java project and attempt to call the remote object? Create a property file and put the following settings.
java.naming.factory.initial = com.sun.enterprise.naming.SerialInitContextFactory
java.naming.factory.url.pkgs = com.sun.enterprise.naming
java.naming.factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl
org.omg.CORBA.ORBInitialHost = 192.168.3.200
org.omg.CORBA.ORBInitialPort = 3007
Â
Replace the IP address and the port to the corresponding values of the ORB listener on the server. Then use the following code to call the remote object.
Â
 Properties props = new Properties();
 props.load(new FileInputStream("jndi.properties"));
      InitialContext ctx = new InitialContext(props);
 PaymentControllerRemote testEJB = (PaymentControllerRemote)  ctx.lookup("com.techyv.PaymentControllerRemote");
      System.out.println(testEJB.getSessionId());
      System.out.println(testEJB.getClientCalls());
Â
Above is a sample only. You need to modify to your EJB beans.
Â
Answered By
DimaZ
0 points
N/A
#91825
Need to invoke a EJB method running on a different machine
When using TekGirls' code, ensure your firewalls are allowing access to the specified ports. If not the remote call will not work. If the test client java application works, you can get about solving the method call from the Web project. First we need to ensure your remote EJB project is really accessible.
Need to invoke a EJB method running on a different machine
I created a new Java application project and used the sample code provided by TekGirl. I disabled the firewalls to ensure all ports are accessible. I am getting a class not found error!
Answered By
DimaZ
0 points
N/A
#91827
Need to invoke a EJB method running on a different machine
You need to refer 2 libraries, in your java project apart from the EJB library. They are "appserv-rt.jar" and "javaee.jar". The appserv-rt.jar provides necessary support for java stand-alone clients, to talk with Glassfish application servers.
Need to invoke a EJB method running on a different machine
Thank you DimaZ. Now the project compiles. The call to the EJB project works. Now how do IÂ get the web project to work?
Answered By
DimaZ
0 points
N/A
#91829
Need to invoke a EJB method running on a different machine
Good. Now that you have identified that the EJB project works and is accessible, now we move on to the Web project. Locate the file called "sun-web.xml". Add the following entries into it.
 <ejb-ref>
   <ejb-ref-name>serverOneBean</ejb-ref-name>
   <jndi-name>corbaname:iiop:192.168.3.200:3700#com.techyv.business.RegistrationRemote</jndi-name>
 </ejb-ref>
Ensure you change the IP address to reflect you server. Now inside your web project, put the EJB ref name on the annotation of the resource.
 @EJB(name="serverOneBean")
   private RegistrationRemote rq;
An EJB reference name, when mapped, will do the remote look up using the server and port details.
Need to invoke a EJB method running on a different machine
Brilliant! It WORKED! Thank you TekGirl and DimaZ! You people were really great!
Need to invoke a EJB method running on a different machine
Thanks a lot DimaZ this worked perfectly
Was just woundering how to fix this issue from a long time but this post helped me.
Definetly worth million dollars… 🙂
Need to invoke a EJB method running on a different machine
First of all many thanks!!!
Great,
The solution really worked for me that what i was looking for.
Need to invoke a EJB method running on a different machine
Salute to DimaZ, Excellent job bro. seems a good solution to the problem that I was stalked. Hope your effort would help millions! Keep it up
Need to invoke a EJB method running on a different machine
Follow the steps mentioned below to access the remote EJB method.
1. If you are using a remote computer, download this file: oc4j.jar.
Â
2. If required, configure JNDI properties for the connection.
Â
3. After you’ve selected an InitialContextFactory for the connection, using either an EJB reference or the JNDI name that’s set up in the deployment descriptor, retrieve an EJB.
Â
Thanks.
Â
Need to invoke a EJB method running on a different machine
Thank you Dimaz!
This has been great solution.
Millions begin! Â =)