[jboss-user] [Messaging, JMS & JBossMQ] - Message Driven Bean consuming response
ddurst@mechdrives.com
do-not-reply at jboss.com
Mon Oct 22 17:16:09 EDT 2007
I am currently running JBoss 4.04.GA.
I am trying to create a Message Driven Bean that onMessage persists some data using the local interface of a session bean and then sends the JMS client a response.
The problem: When I send the response message from the MDB, the MDB picks this message up from the Queue and starts onMessage again (Hence it consumes the response). I know there has to be a really simple fix for this but I am just missing it.
Thanks in advance.
Here is my code for the MDB:
public void onMessage(Message message) {
ObjectMessage msg = null;
Queue queue = null;
QueueConnection connection = null;
QueueSession session = null;
MessageProducer producer = null;
try {
if (message instanceof ObjectMessage) {
msg = (ObjectMessage) message;
Order e = (Order) msg.getObject();
System.out.println("Received new Order");
e = save(e);
System.out.println("Created new Order: "+e.getId());
sendResponse(e,msg.getJMSReplyTo());
}
} catch (JMSException e) {
e.printStackTrace();
mdc.setRollbackOnly();
} catch (Throwable te) {
te.printStackTrace();
}
}
public void sendResponse(Order order, Destination destination) {
ObjectMessage msg = null;
Queue queue = null;
QueueConnection connection = null;
QueueSession session = null;
MessageProducer producer = null;
try {
InitialContext ctx = new InitialContext();
queue = (Queue) ctx.lookup("queue/mdb");
QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");
connection = factory.createQueueConnection();
session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
producer = session.createProducer(destination);
ObjectMessage outMessage = session.createObjectMessage();
outMessage.setStringProperty("RECIPIENT", "CLIENT");
outMessage.setObject(order);
System.out.println("Sending response");
producer.send(destination,outMessage);
System.out.println("Response sent");
producer.close();
} catch (JMSException e) {
e.printStackTrace();
mdc.setRollbackOnly();
} catch (Throwable te) {
te.printStackTrace();
}
}
Here is my code for the JMS Client:
public Order submitOrder(Order order) {
Queue queue = null;
QueueConnection connection = null;
QueueSession session = null;
MessageProducer producer = null;
QueueReceiver consumer = null;
try {
InitialContext ctx = new InitialContext();
queue = (Queue) ctx.lookup("queue/mdb");
QueueConnectionFactory factory =
(QueueConnectionFactory) ctx.lookup("ConnectionFactory");
connection = factory.createQueueConnection();
session = connection.createQueueSession(false,
QueueSession.AUTO_ACKNOWLEDGE);
consumer = session.createReceiver(queue);
producer = session.createProducer(queue);
ObjectMessage outMessage = session.createObjectMessage();
outMessage.setObject(order);
producer.send(outMessage);
producer.close();
Message inMessage = consumer.receive(2000);
if(inMessage != null) {
if(inMessage instanceof ObjectMessage) {
ObjectMessage msg = (ObjectMessage)inMessage;
if(msg.getObject() instanceof Order) {
return (Order) msg.getObject();
}
}
}
connection.close();
} catch (JMSException ex) {
ex.printStackTrace();
} catch (NamingException ex) {
ex.printStackTrace();
}
return null;
}
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4097634#4097634
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4097634
More information about the jboss-user
mailing list