1
votes

I've JAR with EJB 3.1 that I need to use in WAR. Both JAR and WAR are deployed separately on same JBoss (AS 7.1). When I inject EJB in one of the CDI beans in WAR I got this error:

16:45:19,003 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-1) MSC00001: Failed to start service jboss.deployment.unit."TEST_WEB.war".WeldService: org.jboss.msc.service.StartException in service jboss.deployment.unit."TEST_WEB.war".WeldService: org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [TestSessionEJBLocal] with qualifiers [@Default] at injection point [[field] @Inject cz.pfreiberg.test.view.portlet.ViewPortlet.testBean]
    at org.jboss.as.weld.services.WeldService.start(WeldService.java:83)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_79]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_79]
    at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_79]
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [TestSessionEJBLocal] with qualifiers [@Default] at injection point [[field] @Inject cz.pfreiberg.test.view.portlet.ViewPortlet.testBean]
        at     org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:275)
        at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:244)
        at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:107)
        at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:127)
        at org.jboss.weld.bootstrap.Validator.validateBeans(Validator.java:346)
        at org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:331)
        at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:366)
        at org.jboss.as.weld.WeldContainer.start(WeldContainer.java:83)
        at org.jboss.as.weld.services.WeldService.start(WeldService.java:76)

From my current point of view the @Inject doesn’t see the EJB, but if I use manual JNDI lookup (from WAR) is possible for me to get instance of this bean. Is possible to inject EJB from another JAR with @Inject annotation?

Class in WAR (CDI Bean) which @Inject EJB from JAR:

@Model
public class ViewPortlet implements Serializable {
     @Inject
     TestSessionEJBLocal testBean;
}

EJB class in JAR which I'm trying to @Inject with local and remote interface:

@Stateless
public class TestSessionEJBBean implements TestSessionEJB, TestSessionEJBLocal

Thanks for any ideas.

2
Do you have a beans.xml in both your JAR and WAR?jpkrohling
Yes, I have (one at META-INF for JAR, one at WEB-INF for WAR). According to the specification it is even optional.Petr Freiberg
@PetrFreiberg Does it also work with @EJB annotations or just for CDI with @Inject?LenglBoy
You can map EJB from another project if you declare full JNDI path in this annotation. Example: @EJB(mappedName = full JNDI path to external EJB) ExternalSessionEJBLocal bean;Petr Freiberg

2 Answers

1
votes

Short answer: no

JNDI lookup is the go-to && correct way here.

Your WAR cannot see anything from the JAR file since it does not depend on it. It will most likely be loaded with another class loader as well. If you added the JAR as a dependency, it might work. However, I suppose you do not want to go that way as you have other deployment(s) depending on that JAR.

0
votes

So after a lot of research probably the only viable solution is to use separate class (EJBProducer) which use CDI @Produces annotation and hides JNDI lookup. Then you can use @Inject inside your application. The only problem is that you've to write JNDI for each bean separately. Here is example: http://pastebin.com/3V3kKgD9

I must thank Martin Polák for this solution.