tags:

views:

852

answers:

3

I use jboss-IDE. so, I created a project with many EJB's in the same project. now, I need a functionality exposed by EJB A in EJB B. so, I need to call EJB A in EJB B. How do I do that ?

P.S : dealing with EJB 2.

A: 

I'm rusty on EJB but if you want to communicate between 2 EJB's locally you can use local interfaces vs. remote. You can look at this resource for more details

DroidIn.net
A: 

Are you absolutely required to use EJB 2.0? If not, I'd make the jump to EJB 3. The use of annotations and EJB injection will make your life much easier in the long run.

Otherwise...if you must...this page on devx.com summarizes it nicely.

In essence, you'll want to create a local interface for your bean and modify your deployment descriptor to utilize a local reference as opposed to a remote reference. This serves two purposes: to avoid RMI calls, and to avoid serializing/deserializing (since regular references can therefore be passed). This applies to EJB 3 as well.

Once you have the local reference properly setup, you'll need to perform a JNDI lookup of the bean. See the article for a short/sweet example. Once you've done your lookup, you're ready to rock and role with whatever methods the interface exposes.

If you need a more thorough example, I could add some snippets, but a basic understanding of EJB's and that article should cover the bases pretty well.

James
+1  A: 

Is the question actually "How to call an EJB?"? Because if you know how to call EJB B from let's say a webapp or a Swing client, then you technically know how to call EJB A from EJB B.

Now, as other pointed out, you might want to use the local interface instead of the remote interface to avoid the overhead of remoting but without knowing if your EJBs will be deployed on multiples VMs, it's impossible to answer this question for you.

And without more information on the transactional needs, I can't say if you can call an existing method on EJB A safely or if you should expose a cloned version with other transactional settings.

Having that said, the traditional way to call EJBs 2.0 is to use a Service Locator.

Pascal Thivent