File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes Where to use @EJB DI? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Where to use @EJB DI?" Watch "Where to use @EJB DI?" New topic
Author

Where to use @EJB DI?

Saurav Chatterjee
Greenhorn

Joined: Mar 31, 2005
Posts: 12
Here is the deal.

I have an EJB3.1 application (session bean) deployed on Weblogic 10 and that is working perfectly when I am
invoking the client using JNDI lookup.

******************EventProcessor.java****************************
@Remote

public interface EventProcessor {
public String getVersion();

}

******************EventProcessorBean.java*************************
@Stateless(mappedName="EventProcessor")
public class EventProcessorBean implements EventProcessor {

@Resource
private SessionContext sessionContext;


@PersistenceContext(unitName = "actionBazaar")
private EntityManager em;

public String getVersion() {
return "v1.0";
}

}


Now on the same server rather on the same node I have a JAX-WS webservice deployed that is also working fine. Now
I have added one method to the service that will invoke this EJB.

************************Calculator.java*****************************
@WebService(name="CalculatorPortType",
targetNamespace = "http://test.com")
public interface Calculator {

@WebMethod
public int add(@WebParam(name = "value1") int value1,
@WebParam(name = "value2") int value2);

@WebMethod
public String getVersion();
}

****************************CalculatorService.java********************
@WebService(serviceName = "Calculator",
portName="CalculatorPort",
endpointInterface = "com.test.Calculator",
targetNamespace = "http://test.com"
)
public class CalculatorService implements Calculator {

@Resource
private WebServiceContext context;

@EJB(mappedName="EventProcessor")
private EventProcessor eventProcessor;

public int add(int value1, int value2) {
System.out.println("User Principal: " + context.getUserPrincipal());
return value1 + value2;
}

public String getVersion() {
System.out.println("Version : " + eventProcessor.getVersion());
return eventProcessor.getVersion();
}
}



When I am calling the getVersion() i.e. the method calling the EJB then I am getting a nullpointer exception.

Oct 28, 2011 5:02:20 PM com.sun.xml.ws.server.sei.EndpointMethodHandler invoke
SEVERE: null
java.lang.NullPointerException
at com.test.CalculatorService.getVersion(CalculatorService.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.xml.ws.api.server.InstanceResolver$1.invoke(InstanceResolver.java:246)
at com.sun.xml.ws.server.InvokerTube$2.invoke(InvokerTube.java:146)
at com.sun.xml.ws.server.sei.EndpointMethodHandler.invoke(EndpointMethodHandler.java:257)
at com.sun.xml.ws.server.sei.SEIInvokerTube.processRequest(SEIInvokerTube.java:93)
at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:598)
at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:557)
at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:542)
at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:439)
at com.sun.xml.ws.server.WSEndpointImpl$2.process(WSEndpointImpl.java:243)
at com.sun.xml.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:444)
at com.sun.xml.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:244)
at com.sun.xml.ws.transport.http.servlet.ServletAdapter.handle(ServletAdapter.java:134)
at com.sun.xml.ws.transport.http.servlet.WSServletDelegate.doGet(WSServletDelegate.java:129)
at com.sun.xml.ws.transport.http.servlet.WSServletDelegate.doPost(WSServletDelegate.java:160)
at com.sun.xml.ws.transport.http.servlet.WSServlet.doPost(WSServlet.java:75)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:175)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3504)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(Unknown Source)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2186)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2092)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1406)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)

The WebService and EJB is on the same node of the same server. Doesn't the DI thing work for remote EJBs?


Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Short answer - no, but this might be helpful for you:

http://stackoverflow.com/questions/7839207/does-ejb-annotation-work-for-remote-call/7842345#7842345

Cheers!


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Saurav Chatterjee
Greenhorn

Joined: Mar 31, 2005
Posts: 12
Thanks for the link!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Where to use @EJB DI?
 
Similar Threads
problem to send file
Clients of Stateful Web Services on Ivan A Krizsan's notes
How to Create Custom SoapFault
java.sql.SQLException: Unexpected exception while enlisting XAConnection java.sql.SQLException: Tran
Getting sql exception iBatis In exception just giving the name of the statement