How to inject EJB into SOAPHandler?
Asked Answered
L

3

6

My JAX-WS war contains following entries.

WEB-INF/lib/
WEB-INF/beans.xml // empty
WEB-INF/lib/commons-logging-1.1.1.jar
WEB-INF/lib/corrs-beans-1.0-alpha-1-SNAPSHOT.jar // EJBs are here
WEB-INF/lib/corrs-entities-1.0-alpha-1-SNAPSHOT.jar
WEB-INF/lib/joda-time-1.6.2.jar
WEB-INF/lib/opensaml-2.5.1-1.jar
WEB-INF/lib/openws-1.4.2-1.jar
WEB-INF/lib/slf4j-api-1.6.1.jar
WEB-INF/lib/wss4j-1.6.8.jar
WEB-INF/lib/xmlsec-1.5.3.jar
WEB-INF/lib/xmltooling-1.3.2-1.jar
WEB-INF/web.xml
META-INF/maven/
META-INF/maven/kr.co.ticomms.corrs/
META-INF/maven/kr.co.ticomms.corrs/corrs-services/
META-INF/maven/kr.co.ticomms.corrs/corrs-services/pom.xml
META-INF/maven/kr.co.ticomms.corrs/corrs-services/pom.properties

One of my SOAPHandlers trying to call EJB.

@HandlerChain(file=...)
@WebService(...)
public class MyService {
}

public class MyHandler implements SOAPHandler<SOAPMessageContext> {

    @Override
    public boolean handleMessage(final SOAPMessageContext context) {
        // MyEJB null
    }

    @Inject
    private MyEJB myEJB; // << NULL
}

MyEJB is just an nointerface-view EJB.

@LocalBean
@Stateless
public class MyEJB {
}

Can anybody please tell me how to inject EJBs into SOAPHandlers?

UPDATE / (maybe)ANSWER

I changed @Inject to @EJB and it works.

Is there any way to work with @Inject? I looks IMHO better. :)

Linseed answered 31/12, 2012 at 8:31 Comment(1)
How do you bind the handler to the web service? The specification only mentions support for @Resource annotation injection in the handler lifecycle.Vengeance
S
5

If I'm not mistaken, SOAPHandlers are invoked before the web service invocation. According to the CDI spec (see scopes and contexts), in a web services context all the normal scopes are only active during the web service invocation. In addition to all the normal scopes, there is also the @Dependent pseudo scope. Unless otherwise specified, this is the default scope. It's life cycle depends on one of the normal scopes and as such cannot exist by itself.

Now, a stateless EJB since it has no CDI related annotations, it is automatically @Dependent and cannot be injected (using @Inject) anywhere a normal scope is not active. In your case, inside a SOAPHandler there is no scope active so you cannot use @Inject.

Use @EJB, nothing wrong with that.

Singletree answered 16/3, 2013 at 6:1 Comment(0)
M
1

If you annotate your soap handler class with CDI "@named" annotation, you can inject any EJB component. By doing so, your soap handler class becomes a container managed bean and you start to be able to use other container managed beans as EJBs.

@Named

public class MyHandler implements SOAPHandler'<'SOAPMessageContext'>' {

....

@Inject

private MyEJB myEJB; 

...

}
Mneme answered 6/4, 2016 at 6:41 Comment(1)
Using Glassfish/Payara I'll get a NullPointerException at least when trying to inject a normal CDI Bean (non EJB).Slovene
T
0

In case someone else google here like I did, it's possible to do that in TomEE: http://svn.apache.org/repos/asf/tomee/tomee/trunk/server/openejb-cxf/src/test/java/org/apache/openejb/server/cxf/handler/SimpleHandler.java

Tolyl answered 15/9, 2014 at 11:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.