message is read from queue but not removed
[nobr]Hello friends,
I have implemented a simple reques response approach where using JMS correlationid. where i have two queue requestQueue and responseQueue.
i have a messageSender class which sends a message to request queue and starts listening to response queue.
and have an MDB which listens to request queue and sends response to responsequeue.
as soon as MDB sends response my MessageSender class which was listening reads the message , but when i check the responsequeue in admin console of my JBoss server, the message still appears.what might be the problem
here i am pasting my code
package test.jms;
/**
* The SimpleQueueReceiver class consists only of a main method,
* which fetches one or more messages from a queue using
* synchronous message delivery. Run this program in conjunction
* with SimpleQueueSender. Specify a queue name on the command
* line when you run the program.
*/
import java.util.Enumeration;
import java.util.Properties;
import javax.jms.*;
import javax.naming.*;
publicclass SimpleQueueReceiver{
/**
* Main method.
*
* @param args the queue used by the example
*/
publicstaticvoid main(String[] args){
String queueName =null;
Context jndiContext =null;
QueueConnectionFactory queueConnectionFactory =null;
QueueConnection queueConnection =null;
QueueSession queueSession =null;
Queue queue =null;
QueueReceiver queueReceiver =null;
TextMessage message =null;
queueName ="queue/testQueue";
System.out.println("Queue name is " + queueName);
try{
Properties env =new Properties();
env.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.jboss.naming.NamingContextFactory");
jndiContext =new InitialContext();
queueConnectionFactory = (QueueConnectionFactory)jndiContext.lookup("QueueConnectionFactory");
queue = (Queue) jndiContext.lookup(queueName);
queueConnection = queueConnectionFactory.createQueueConnection();
queueSession = queueConnection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
queueReceiver = queueSession.createReceiver(queue);
Queue resqueue = (Queue) jndiContext.lookup("queue/CxPSendQueue");
QueueSender queueSender = queueSession.createSender(resqueue);
queueConnection.start();
TextMessage msq = (TextMessage) queueReceiver.receive();
System.out.println("msg in receiver from test queue " + msq.getText());
Thread.sleep(8000);
TextMessage res = queueSession.createTextMessage();
res.setText("respose in cxpsend queue ");
queueSender.send(res);
}catch (Exception e){
System.out.println("Exception occurred: " +e.getMessage());
}finally{
if (queueConnection !=null){
try{
queueConnection.close();
}catch (JMSException e){}
}
}
}
}
MY BEAN CLASS
/*
* Created on Nov 17, 2005
*
*/
package in.co.persistent;
import java.util.Properties;
import javax.ejb.EJBException;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
*
* @ejb.bean name="SimpleMessage"
*display-name="Name for SimpleMessage"
*description="Description for SimpleMessage"
*destination-type="javax.jms.Queue"
*acknowledge-mode="Auto-acknowledge"
*/
publicclass SimpleMessageBeanimplements MessageDrivenBean, MessageListener{
/** The MessageDrivenContext */
private MessageDrivenContext context;
/**
*
*/
public SimpleMessageBean(){
super();
}
/**
* Set the associated context. The container calls this method
* after the instance creation. <br>
*
* The enterprise bean instance should store the reference to the context
* object in an instance variable. <br>
*
* This method is called with no transaction context.
*
* @param newContext A MessageDrivenContext interface for the instance.
*
* @throws EJBException Thrown by the method to indicate a failure caused by a system-level error.
*/
publicvoid setMessageDrivenContext(MessageDrivenContext newContext)
throws EJBException{
context = newContext;
}
/* (non-Javadoc)
* @see javax.ejb.MessageDrivenBean#ejbRemove()
*/
publicvoid ejbRemove()throws EJBException{
}
/* (non-Javadoc)
* @see javax.jms.MessageListener#onMessage(javax.jms.Message)
*/
publicvoid onMessage(Message message){
Context jndiContext =null;
QueueConnectionFactory queueConnectionFactory =null;
QueueConnection queueConnection =null;
QueueSession queueSession =null;
Queue queue =null;
QueueReceiver queueReceiver =null;
TextMessage l =null;
try{
if (messageinstanceof TextMessage){
/*Context jndiContext = null;
QueueConnectionFactory queueConnectionFactory =null;
msg = (TextMessage) message;
System.out.println("Reading message in listener of MDB my code: " + msg.getText());
String xmlString = msg.getText();
/* webmethods logic to interact with sap comes over here,in brief,like validating and parsing
* this xmlString with sax parser,sending request to SAP through webmethods,getting response,
* converting that xmlrespose back to String and sending that response string in the
* body of TextMessage. */
/*DestinationreplyDestination = message.getJMSReplyTo();
Queue queue = (Queue)replyDestination;
try{
Properties env = new Properties();
env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.NamingContextFactory");
jndiContext = new InitialContext();
queueConnectionFactory = (QueueConnectionFactory)jndiContext.lookup("QueueConnectionFactory");
}catch(NamingException e){
e.printStackTrace();
}
queueConnection = queueConnectionFactory.createQueueConnection ();
QueueSession session = queueConnection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
QueueSender replyProducer = session.createSender((Queue)replyDestination);
TextMessage replyMessage = session.createTextMessage();
replyMessage.setText("theResponseXMLinStringFormat");
replyProducer.send(replyMessage);*/
TextMessage msgw =(TextMessage) message;
System.out.println("Text -> " + msgw.getText());
try{
Properties env =new Properties();
env.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.jboss.naming.NamingContextFactory");
jndiContext =new InitialContext();
queueConnectionFactory = (QueueConnectionFactory)jndiContext.lookup("QueueConnectionFactory");
queue = (Queue) jndiContext.lookup("queue/CxPSendQueue");
queueConnection = queueConnectionFactory.createQueueConnection();
queueSession = queueConnection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
TextMessage res = queueSession.createTextMessage();
res.setJMSCorrelationID(msgw.getJMSMessageID());
res.setText("kuchi pudi");
QueueSender queueSender = queueSession.createSender(queue);
queueSender.send(res);
}catch (NamingException e1){
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else{
System.out.println("Message of wrong type in listener: " + message.getClass().getName());
}
}catch (JMSException e){
e.printStackTrace();
}finally{
if (queueConnection !=null){
try{
queueConnection.close();
}catch (JMSException e){}
}
}
}
/**
* An ejbCreate method as required by the EJB specification.
*
* The container calls the instance?s <code>ejbCreate</code> method
* immediately after instantiation.
*
* @ejb.create-method
*/
publicvoid ejbCreate(){
}
}
[/nobr]

