Accessing an EJB3 bean in a jar from an independently deployed war (not packaged in ear)
Asked Answered
E

3

7

For some reasons, I would like to deploy my application as two separate artifacts: Users-ejb.jar and Users-war.war, not packaged in the same ear (but still, deployed in the same JBoss AS 7.1 instance). In the Users-war.war I have a backing bean (annotated as a JSF managed bean) where I wish to inject an EJB3 packaged in the Users-ejb.jar. The simple @EJB injection that worked when everything was packaged in a single ear no longer works when the Users-ejb.jar and the Users-war.war are deployed seperately.

A narrowed-down simplified example of my setup follows:

EJB3 bean

import javax.ejb.*;

(...)

@Stateless(name="userFacade")
@Local(IUserFacadeLocal.class)
@Remote(IUserFacadeRemote.class)
public class UserFacade extends AbstractFacade<User> implements IUserFacadeLocal, IUserFacadeRemote {

Backing bean

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.ejb.EJB;

import entities.User;
import facades.IUserFacadeRemote;
import facades.IUserFacadeLocal;

@ManagedBean(name="indexBackingBean")
@SessionScoped
public class IndexBackingBean implements Serializable {

    @EJB(beanName="userFacade")
    private IUserFacadeLocal userFacade;

I've tried various combinations like declaring the type of the EJB3 bean in the backing bean as IUserFacadeRemote (as opposed to IUserFacadeLocal) but they all fail with the same exception when the Users-war.war module is deployed:

Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException:
JBAS014543: No EJB found with interface of type 'facades.IUserFacadeLocal' and
 name 'userFacade' for binding controllers.IndexBackingBean/userFacade

The Users-ejb.jar is deployed to JBoss AS 7.1 without any complains but when the Users-war.war is deployed, JBoss complains that it can't find the bean he's supposed to inject.

However, I am able to obtain a reference to the EJB3 bean using JNDI using:

String jndiName = "java:global/Users-ejb/userFacade!facades.IUserFacadeRemote";
this.userFacade = (IUserFacadeRemote) new InitialContext().lookup(jndiName);

Despite that, the @EJB injection doesn't seem to work.

UPDATE: I followed the suggestion give below by Tom Anderson and the injection that does work is the:

@EJB(mappedName = "java:global/Users-ejb/userFacade!facades.IUserFacadeRemote")

which if I understand correctly uses the vendor-specific mappedName attribute. I couldn't get the injection to work in a vendor-independent way.

Exuberate answered 13/8, 2012 at 12:10 Comment(4)
Have a look at this example. It's for GlassFish but maybe can help...Reichsmark
Did either of my suggestions work? I'm very curious to know!Example
I am sidetracked on another issue at the moment, will definitely report when I try your suggestions. At the moment I am using the JNDI lookup work-around I have added at the end of my post.Exuberate
@TomAnderson see the update on my post and my comment on your answer.Exuberate
E
4

I wish i understood this area of the EE spec well enough to give you a definitive answer, but i don't.

The JBoss EJB documentation has this to say:

  • The @EJB annotation also has a mappedName() attribute. The specification leaves this a vendor specific metadata, but JBoss recognizes mappedName() as the global JNDI name of the EJB you are referencing. If you have specified a mappedName(), then all other attributes are ignored and this global JNDI name is used for binding.
  • If you specify @EJB with no attributes defined [...] Then the following rules apply:
    • The EJB jar of the referencing bean is searched for an EJB with the interface, used in for @EJB injection. If there are more than one EJB that publishes same business interface, then an exception is thrown. If there is only one bean with that interface then that one is used.
    • Search the EAR for EJBs that publish that interface. If there are duplicates, then an exception is thrown. Otherwise the matching bean is returned.
    • Search globally in JBoss for an EJB of that interface. Again, if duplicates, an exception is thrown.
  • @EJB.beanName() corresponds to . If the beanName() is defined, then use the same algorithm as @EJB with no attributes defined except use the beanName() as a key in the search. An exception to this rule is if you use the ejb-link '#' syntax. The '#' syntax allows you to put a relative path to a jar in the EAR where the EJB you are referencing lives. See spec for more details

The "Search globally in JBoss for an EJB of that interface" certainly suggests that an injection like the one you wrote should work. Indeed, that it should work without the beanName. However, my suspicion is that from the point of view of a component in the WAR, a component in the EJB-JAR is remote, and therefore you will need to use the remote interface.

So, the first thing i'd try is:

@EJB
private IUserFacadeRemote userFacade;

Without a beanName, in case that's making trouble. It sounds like you've tried that, though.

If the normal approach to injection doesn't work, i might fall back to trying an injection via a mappedName, which in JBoss is a global JNDI name. So:

@EJB(mappedName = "java:global/Users-ejb/userFacade!facades.IUserFacadeRemote")
private IUserFacadeRemote userFacade;

This is obviously rather ugly.

Anyway, good luck!

EDIT: Something else you could try is to use a qualified relative beanName which explicitly names the EJB-JAR:

@EJB(beanName = "Users-ejb.jar#userFacade")
private IUserFacadeRemote userFacade;

Because the WAR and EJB-JAR are not packaged in an EAR, this might need to be:

@EJB(beanName = "../Users-ejb.jar#userFacade")
private IUserFacadeRemote userFacade;

But by this point i'm just guessing.

EDIT STRIKES BACK: We may have overlooked something very simple. The lookup attribute of the @EJB annotation lets you specify "A portable lookup string containing the JNDI name for the target EJB component", hence:

@EJB(lookup = "java:global/Users-ejb/userFacade!facades.IUserFacadeRemote")
private IUserFacadeRemote userFacade;

Might work. This is essentially a portable version of the JBoss-specific use of mappedName.

Example answered 13/8, 2012 at 13:32 Comment(3)
OK, so "@EJB" does not work. "@EJB" with the mappedName attribute you provided does work. So it seems that one is left with only a vendor-dependent way to perform this injection or having to use JNDI lookup. It is a bit unsettling to use a work-around without an understanding of why the other simpler annotations didn't do the trick. I may have to take this to JBoss forums to get to the bottom of this.Exuberate
Edited with yet another idea.Example
I don't think the reference to "JBoss EJB documentation" is correct for Jboss 7.1. I debugged the server and it only looks inside current application. It never searches globally.Alvarado
A
0

I have been testing this scenario in Wildfly and found that it will work with local interfaces as described above if there is a jboss-deployment-structure.xml inside of the war pointing to the ejb. Otherwise a ClassNotFoundException is thrown as the war above can't really "know" about the ejbs classes due to the modular class loading in JBoss and Wildfly. The content of the file should be:

<jboss-deployment-structure>
    <deployment>
        <dependencies>
            <module name="deployment.Users-ejb.jar" />
        </dependencies>
    </deployment>
</jboss-deployment-structure>

And then the JSF bean can use:

@EJB(lookup = "java:global/Users-ejb/userFacade!facades.IUserFacadeRemote")
private IUserFacadeLocal userFacade;
Alvarado answered 24/10, 2014 at 20:29 Comment(0)
A
0

As @TomAnderson said, the standard way to achieve cross-artifact lookup is the lookup attribute of the @EJB annotation.

Here's a complete Maven project to illustrate how this works:
https://github.com/mrts/remote-ejb-injection

You don't need to use the name attribute of the EJB class, providing the class name in lookup is sufficient. Quoting from the example above:

// in API JAR
@Remote
public interface HelloService { ... }

// in EJB JAR
@Stateless
public class HelloServiceImpl implements HelloService { ... }

// in WAR
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {

    @EJB(lookup = "java:global/service-ear/service-ejb-impl/HelloServiceImpl!" +
                  "ee.mrts.service.HelloService")
    private HelloService helloService;

    ...
}

(So, using HelloServiceImpl directly in lookup Just Works™.)

Autism answered 8/7, 2016 at 15:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.