Ask Your Question
1

What is the method to retrieve the message count in the Solace JMS Queue using Java?

asked 2021-10-09 11:00:00 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-02-25 18:00:00 +0000

lalupa gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-10-09 11:00:00 +0000

Seen: 7 times

Last updated: Feb 25 '22