Need to invoke a EJB method running on a different machine

Asked By 280 points N/A Posted on -
qa-featured

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?

SHARE
Best Answer by DimaZ
Answered By 0 points N/A #91821

Need to invoke a EJB method running on a different machine

qa-featured

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 0 points N/A #91822

Need to invoke a EJB method running on a different machine

qa-featured

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.

Answered By 280 points N/A #91823

Need to invoke a EJB method running on a different machine

qa-featured

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 0 points N/A #91824

Need to invoke a EJB method running on a different machine

qa-featured

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 0 points N/A #91825

Need to invoke a EJB method running on a different machine

qa-featured

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.

Answered By 280 points N/A #91826

Need to invoke a EJB method running on a different machine

qa-featured

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 0 points N/A #91827

Need to invoke a EJB method running on a different machine

qa-featured

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.

Answered By 280 points N/A #91828

Need to invoke a EJB method running on a different machine

qa-featured

Thank you DimaZ. Now the project compiles. The call to the EJB project works. Now how do I get the web project to work?

Best Answer
Best Answer
Answered By 0 points N/A #91829

Need to invoke a EJB method running on a different machine

qa-featured

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.

Answered By 280 points N/A #91830

Need to invoke a EJB method running on a different machine

qa-featured

Brilliant! It WORKED! Thank you TekGirl and DimaZ! You people were really great!

Answered By 0 points N/A #91831

Need to invoke a EJB method running on a different machine

qa-featured

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… 🙂

Answered By 0 points N/A #91833

Need to invoke a EJB method running on a different machine

qa-featured

First of all many thanks!!!

Great,

The solution really worked for me that what i was looking for.

Answered By 0 points N/A #91835

Need to invoke a EJB method running on a different machine

qa-featured

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

Answered By 0 points N/A #91836

Need to invoke a EJB method running on a different machine

qa-featured

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.
 
Answered By 0 points N/A #91837

Need to invoke a EJB method running on a different machine

qa-featured

Thank you Dimaz!

This has been great solution.

Millions begin!   =)

Login/Register to Answer

Related Questions