Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JMS question  RSS feed

Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Acc to what I have read it should not matter whether a consumer receive messages from a queue synchronously or asynchronously, A queue always has one subscriber and once the message is consumed it is removed from the queue.

However I am not sure if what I am seeing under JBOSS is correct or if something in the program is wrong(This program was copied from

Even after the message has been received from the queue, the message is still available if I look via JMX on JBOSS- listMessages. Why is that the case? Shouldnt the message be deleted from the queue once it is consumed?

When I try a client with a synchronous receiver the message is deleted from the JBOSS queue and everythings fine.

Could it be that in the MessageListener's onMessage method, I need to do something specific that removes it from the client side.

Code is as follows

import java.util.Properties;

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.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

* Listens for a TextMessage from a Queue
public class AsynchQueueToMessage
InitialContext ctx;
QueueConnectionFactory cf;
QueueConnection connection;
QueueSession session;
Queue destination;
QueueReceiver receiver;

public static void main(String[] args) throws NamingException,
new AsynchQueueToMessage().run();

public void run() throws NamingException, JMSException

Properties props = new Properties();
props.put(Context.PROVIDER_URL, "localhost:1099");

ctx = new InitialContext(props);
cf = (QueueConnectionFactory)ctx.lookup("ConnectionFactory");
destination = (Queue)ctx.lookup("queue/testQueue");

connection = cf.createQueueConnection();
session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
receiver = session.createReceiver(destination);
receiver.setMessageListener(new MyMessageListener());

System.out.println("Waiting For A Message.");

class MyMessageListener implements MessageListener {

public void onMessage(Message msg)
TextMessage message = (TextMessage)msg;
System.out.println("The message was: "+message.getText());
catch (JMSException e)
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The message is sent back to the queue if there is an error. Check your server log for an unhandled exception.
[ December 28, 2004: Message edited by: M Burke ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!