|
RSS Feed - WebSphere MQ Support
|
RSS Feed - Message Broker Support
|
 |
|
Stateless Session Bean - Receiver - Null Pointer Exception |
« View previous topic :: View next topic » |
Author |
Message
|
cactus |
Posted: Tue Nov 04, 2008 4:11 pm Post subject: Stateless Session Bean - Receiver - Null Pointer Exception |
|
|
 Apprentice
Joined: 12 Oct 2002 Posts: 30
|
As per application requirement we want to create stateless session bean instead of MDB.
When I invoke receiveMessage() method using Universal Test Client in RSA,it throws java.lang.NullPointerException when it is reading messages from the queue.(message = consumer.receive(10000);)
Below is the bean code and deployment descriptor.
package ejbs;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
* Bean implementation class for Enterprise Bean: ReceiveMsgBean
*/
public class ReceiveMsgBeanBean implements javax.ejb.SessionBean {
static final long serialVersionUID = 3206093459760846163L;
public static final String CONN_FACT_NAME = "java:comp/env/jms/JMSExampleConnectionFactoryRef";
public static final String QUEUE_NAME = "java:comp/env/jms/JMSExampleQueueRef";
public void receiveMessage() throws NamingException, JMSException{
consumeMessage(CONN_FACT_NAME, QUEUE_NAME);
}
private void consumeMessage(String connectionFactoryJNDIName, String destinationJNDIName)
throws NamingException, JMSException{
Connection connection = null;
Session session = null;
MessageConsumer consumer = null;
Message message =null;
try {
InitialContext context = new InitialContext();
ConnectionFactory factory = (ConnectionFactory) context.lookup(connectionFactoryJNDIName);
Destination dest = (Destination) context.lookup(destinationJNDIName);
System.out.println("destinationJNDIName = "+destinationJNDIName.toString()+", dest = "+dest.toString());
connection = factory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
consumer = session.createConsumer(dest);
do{
System.out.println("Inside do loop : " );
System.out.println("Consumer = "+consumer.toString());
message = consumer.receive(10000);
System.out.println("Before converting message: " +message.toString());
TextMessage textMessage = (TextMessage)message;
System.out.println("after converting to textMessage: " +textMessage.toString());
String text = textMessage.getText();
System.out.println("Received : " + text);
}
while(message != null);
}//try
catch (NamingException e) {
mySessionCtx.setRollbackOnly();
throw e;
}
catch (JMSException e) {
mySessionCtx.setRollbackOnly();
Exception linked = e.getLinkedException();
System.out.println("Linked Exception " +linked);
mySessionCtx.setRollbackOnly();
throw e;
}
catch(Exception e ){
e.printStackTrace();
System.out.println("Failed to get connection...exception is " +e);
}
finally {
consumer.close();
session.close();
connection.close();
System.out.println("******* Finally closed all connections ********");
}
}//consumeMessage
private javax.ejb.SessionContext mySessionCtx;
/**
* getSessionContext
*/
public javax.ejb.SessionContext getSessionContext() {
return mySessionCtx;
}
/**
* setSessionContext
*/
public void setSessionContext(javax.ejb.SessionContext ctx) {
mySessionCtx = ctx;
}
/**
* ejbCreate
*/
public void ejbCreate() throws javax.ejb.CreateException {
}
/**
* ejbActivate
*/
public void ejbActivate() {
}
/**
* ejbPassivate
*/
public void ejbPassivate() {
}
/**
* ejbRemove
*/
public void ejbRemove() {
}
}
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar id="ejb-jar_ID" version="2.1" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd">
<display-name>
LTCJMSWMQBeans</display-name>
<enterprise-beans>
<session id="SendMsgBean">
<ejb-name>SendMsgBean</ejb-name>
<local-home>ejbs.SendMsgBeanLocalHome</local-home>
<local>ejbs.SendMsgBeanLocal</local>
<ejb-class>ejbs.SendMsgBeanBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
<resource-ref id="ResourceRef_1225757582149">
<description>
</description>
<res-ref-name>jms/JMSExampleConnectionFactoryRef</res-ref-name>
<res-type>javax.jms.ConnectionFactory</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
<message-destination-ref id="MessageDestinationRef_1225757673274">
<description>
</description>
<message-destination-ref-name>jms/JMSExampleQueueRef</message-destination-ref-name>
<message-destination-type>javax.jms.Destination</message-destination-type>
<message-destination-usage>ConsumesProduces</message-destination-usage>
<message-destination-link>JMSExampleQueue</message-destination-link>
</message-destination-ref>
</session>
<session id="ReceiveMsgBean">
<ejb-name>ReceiveMsgBean</ejb-name>
<local-home>ejbs.ReceiveMsgBeanLocalHome</local-home>
<local>ejbs.ReceiveMsgBeanLocal</local>
<ejb-class>ejbs.ReceiveMsgBeanBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
<resource-ref id="ResourceRef_1225814890501">
<description>
</description>
<res-ref-name>jms/JMSExampleConnectionFactoryRef</res-ref-name>
<res-type>javax.jms.ConnectionFactory</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
<message-destination-ref id="MessageDestinationRef_1225814890501">
<description>
</description>
<message-destination-ref-name>jms/JMSExampleQueueRef</message-destination-ref-name>
<message-destination-type>javax.jms.Destination</message-destination-type>
<message-destination-usage>Consumes</message-destination-usage>
<message-destination-link>JMSExampleQueue</message-destination-link>
</message-destination-ref>
</session>
</enterprise-beans>
<assembly-descriptor id="AssemblyDescriptor_1225758252768">
<message-destination>
<description>
</description>
<message-destination-name>JMSExampleQueue</message-destination-name>
</message-destination>
</assembly-descriptor>
</ejb-jar>
Pls let me know,if there is any problem in the code/XML.
thankx _________________ thankx
cactus |
|
Back to top |
|
 |
fjb_saper |
Posted: Tue Nov 04, 2008 4:24 pm Post subject: |
|
|
 Grand High Poobah
Joined: 18 Nov 2003 Posts: 20756 Location: LI,NY
|
cactus wrote: |
Code: |
message = consumer.receive(10000);
System.out.println("Before converting message: " +message.toString());
TextMessage textMessage = (TextMessage)message;
System.out.println("after converting to textMessage: " +textMessage.toString());
String text = textMessage.getText();
System.out.println("Received : " + text);
|
When I invoke receiveMessage() method using Universal Test Client in RSA,it throws java.lang.NullPointerException when it is reading messages from the queue.(message = consumer.receive(10000)
|
You do realize I hope that if there is no message available during the interval (10 seconds?) that the call message = consumer.receive(10000); will return with a null value.
As you do not check the message for null value you will get the null pointer exception in the next line while trying to do message.toString()
Enjoy  _________________ MQ & Broker admin |
|
Back to top |
|
 |
cactus |
Posted: Tue Nov 04, 2008 4:39 pm Post subject: |
|
|
 Apprentice
Joined: 12 Oct 2002 Posts: 30
|
thankx for the reply..
Yes,that's correct but there are valid messages in the queue.
Altered the queue to get inhibited ,it should throw 2016 0x000007e0 MQRC_GET_INHIBITED but it still throws null pointer exception.
To make sure it throws mq exception I altered MCA to nobody ,it was throwing 2035 which is expected.
Initialization part looks good as I see valid connection handles.
Is there any exceptions and prints that I can add it to narrow down the issue? _________________ thankx
cactus |
|
Back to top |
|
 |
atheek |
Posted: Tue Nov 04, 2008 6:28 pm Post subject: |
|
|
 Partisan
Joined: 01 Jun 2006 Posts: 327 Location: Sydney
|
You didn't start the connection.
Do connection.start() before the do loop |
|
Back to top |
|
 |
cactus |
Posted: Tue Nov 04, 2008 6:37 pm Post subject: [Solved] Stateless Session Bean - Receiver |
|
|
 Apprentice
Joined: 12 Oct 2002 Posts: 30
|
You Rock ..atheek
thankx _________________ thankx
cactus |
|
Back to top |
|
 |
|
|
 |
|
Page 1 of 1 |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|
|