Query queue depth in Wildfly

What do we want? To query how many messages (and scheduled messages) are in a JMS queue.

How can we achieve this? The JMS API does not provide the ability or tools to do this, but we can query this data from Wildfly/JBoss CLI, so that, the information is present on runtime:

[standalone@localhost:9990 /] /subsystem=messaging-activemq/server=default/jms-queue=cityScheduledUpdateStateQueue:read-attribute(name=message-count)
{
    "outcome" => "success",
    "result" => 291L
}
[standalone@localhost:9990 /] /subsystem=messaging-activemq/server=default/jms-queue=cityScheduledUpdateStateQueue:read-attribute(name=scheduled-count)
{
    "outcome" => "success",
    "result" => 290L
}

However, if it works from Wildfly CLI, it will also work within Wildfly, using ModelControllerClient, which is good for the same thing as Wildfly CLI, only programmatically. You need a jar of the same version of our Wildfly Core:

<dependency>
    <groupId>org.wildfly.core</groupId>
    <artifactId>wildfly-controller-client</artifactId>
    <version>30.0.0.Final</version>
</dependency>

And the following small program:

    private void depthOfQueue(final String queueName) {
        try (final ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getByName("localhost"), 9990)) {
            final ModelNode address = Operations.createAddress("subsystem", "messaging-activemq", "server", "default", "jms-queue", queueName);
            final ModelNode op = Operations.createReadResourceOperation(address, true);
            op.get(ClientConstants.INCLUDE_RUNTIME).set(true);
            final ModelNode result = client.execute(op);
            if (Operations.isSuccessfulOutcome(result)) {
                final Long messageCount = Operations.readResult(result).get("message-count").asLong();
                final Long scheduledCount = Operations.readResult(result).get("scheduled-count").asLong();
                LOGGER.info("Queue '{}' has {} messages and {} scheduled messages.", queueName, messageCount, scheduledCount);
            } else {
                LOGGER.error("Error while querying the queue depth: {}", Operations.getFailureDescription(result).asString());
            }
        } catch (IOException e) {
            LOGGER.error("Error while querying the queue depth.", e);
        }
    }

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top