Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The following code snippet can be used to retrieve the message count in a Solace JMS Queue using Java:

import javax.jms.*;
import javax.naming.*;

public class QueueMessageCount {
    public static void main(String[] args) throws NamingException, JMSException {
        // Create a JNDI context
        Context context = new InitialContext();

        // Lookup the connection factory and queue
        ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("ConnectionFactory");
        Queue queue = (Queue) context.lookup("queue/queueName");

        // Create a connection to the Solace messaging server
        Connection connection = connectionFactory.createConnection();

        // Create a session on this connection
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        // Create a queue browser to retrieve message count
        QueueBrowser queueBrowser = session.createBrowser(queue);

        // Get the message enumeration
        Enumeration enumeration = queueBrowser.getEnumeration();

        // Count the number of messages in the queue
        int numMessages = 0;
        while (enumeration.hasMoreElements()) {
            enumeration.nextElement();
            numMessages++;
        }

        // Print the number of messages in the queue
        System.out.println("Number of messages in queue: " + numMessages);

        // Close the queue browser, session and connection
        queueBrowser.close();
        session.close();
        connection.close();
    }
}

Note: Replace "queue/queueName" with the actual JNDI name of the Solace JMS Queue.