Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jsf

ejb-3.0

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.

like image 459
Marcus Junius Brutus Avatar asked Aug 13 '12 12:08

Marcus Junius Brutus


People also ask

What is the use of IBM EJB jar BND XMI?

The ibm-ejb-jar-bnd. xml file is used for EJB 3.0 and later modules that run on the product, whereas the ibm-ejb-jar. bnd. xmi file is used for pre-EJB 3.0 modules and for web modules.

Which interface is not part of EJB 3.0 specifications?

The business interface is a plain Java interface (or POJI); it should not extend javax. ejb. EJBObject or javax. ejb.

What is EJB jar XML?

The orion-ejb-jar. xml file is an EJB deployment descriptor file that contains all OC4J-proprietary options. This file extends the configuration that you specify in the ejb-jar.

How do you declare EJB?

Right click on project name in Project explorer window. Select properties. Add EJB component project created earlier under libraries using Add Project button in compile tab. Add jboss libraries using Add jar/folder button in compile tab.


1 Answers

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.

like image 128
Tom Anderson Avatar answered Oct 13 '22 10:10

Tom Anderson