Need to invoke a EJB method running on a different machine
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?
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?
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.
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.
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.
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
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.
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!
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.
Thank you DimaZ. Now the project compiles. The call to the EJB project works. Now how do I get the web project to work?
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.
Brilliant! It WORKED! Thank you TekGirl and DimaZ! You people were really great!
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… 🙂
First of all many thanks!!!
Great,
The solution really worked for me that what i was looking for.
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
Follow the steps mentioned below to access the remote EJB method.
Thank you Dimaz!
This has been great solution.
Millions begin! =)