Calling Session Bean from MDB - Newbie Q
ummadiravi78 Mar 28, 2006 6:34 AMHi,
I get NullPointerException when I try to invoke session bean from my message driven bean onMessage() method.
Here is my MDB code.
public class MDB implements MessageDrivenBean, MessageListener{ private WorkerHome workerHome = null; private MessageDrivenContext ctx = null; public MDB() { } public void setMessageDrivenContext(MessageDrivenContext ctx) throws EJBException { this.ctx = ctx; } public void ejbCreate() {} public void ejbRemove() {ctx=null;} public void onMessage(Message message) { try { Worker worker = workerHome.create(); if (message instanceof TextMessage) { TextMessage m = (TextMessage)message; worker.doWork(m.getText()); } } catch(Exception ex) { throw new EJBException("Could not call worker " + ex); } } }
Here is my ejb-jar.xml file:
<?xml version="1.0"?> <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/j2ee/dtds/ejb-jar_2_0.dtd"> <ejb-jar> <enterprise-beans> <message-driven> <ejb-name>MDB</ejb-name> <ejb-class>test.bean.MDB</ejb-class> <message-selector></message-selector> <transaction-type>Container</transaction-type> <ejb-ref> <description>The Workers home</description> <ejb-ref-name>ejb/worker</ejb-ref-name> <ejb-ref-type>Session</ejb-ref-type> <ejb-link>WorkerBean</ejb-link> <home>test.bean.WorkerHome</home> <remote>test.bean.Worker</remote> </ejb-ref> <message-driven-destination> <destination-type>javax.jms.Topic</destination-type> <subscription-durability>NonDurable</subscription-durability> </message-driven-destination> </message-driven> <session> <description>Worker bean</description> <display-name>Worker</display-name> <ejb-name>WorkerBean</ejb-name> <home>test.bean.WorkerHome</home> <remote>test.bean.Worker</remote> <ejb-class>test.bean.WorkerBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans> <assembly-descriptor> <container-transaction> <method> <ejb-name>MDB</ejb-name> <method-name>*</method-name> </method> <trans-attribute>Required</trans-attribute> </container-transaction> <container-transaction> <method> <ejb-name>WorkerBean</ejb-name> <method-intf>Remote</method-intf> <method-name>*</method-name> </method> <trans-attribute>Required</trans-attribute> </container-transaction> </assembly-descriptor> </ejb-jar>
When I use client code to invoke MDB's onMessage() method ( publish topic), I get the following exception on the server console:
15:44:16,453 ERROR [JMSContainerInvoker] Exception in JMSCI message listener javax.ejb.TransactionRolledbackLocalException: Could not call worker java.lang.NullPointerException; CausedByException is: Could not call worker java.lang.NullPointerException at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:232) .... ..... javax.ejb.EJBException: Could not call worker java.lang.NullPointerException at test.bean.MDB.onMessage(MDB.java:41) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke
Could any one please tell me why my MDB throws NullPointerException when trying to call session bean?
Ravi Prakash