Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

JMS Queue: When Size Does Matter

DZone's Guide to

JMS Queue: When Size Does Matter

· Integration Zone
Free Resource

Share, secure, distribute, control, and monetize your APIs with the platform built with performance, time-to-value, and growth in mind. Free 90-day trial of 3Scale by Red Hat

[This article was written by Adrian Hsieh.]

Anypoint Platform is fast. The legacy systems that it often connects to? Not so much.

Therefore, in real world use cases, the requirements often call for limiting the message throughput to protect the endpoint systems from being overwhelmed by traffic. Architectural designs that support message throttling commonly incorporate some elements of message queues to stage and hold messages in-flight, so that the endpoints can process them at a steadier pace.

For architectures with throttling, it is often important to be able to count how many messages in the queues are still waiting to be consumed. For example, if certain threshold has been crossed, an alert may need to be sent to the operation center, or the inbound polling may have to be stopped completely until the queue size falls below threshold again, only then can intaking of messages resume.

Muley counting his new friends.

Muley’s counting his new friends.

The QueueBrowser interface can be used to count the number of messages in-flight on the queues. For working with a Mule flow, an option is to write a custom Mule Java component that uses the QueueBrowser. Note that it is best to make the custom component a Spring bean to take advantages of using property placeholders, to make it easier to specify host address and port number.

        <spring:bean id="queue-counter" name="queue-counter" class="org.mule.api.jms.extension.CurrentQueueSize">
            <spring:property name="port1" value="${jms.port1}"/>
            <spring:property name="host1" value="${jms.host1}"/>
            <spring:property name="port2" value="${jms.port2}"/>
            <spring:property name="host2" value="${jms.host2}"/>
        </spring:bean>

With this approach, counting in the flow is as simple as this:

        <enricher   doc:name="Message Enricher">
            <component doc:name="queue-counter">
                <spring-object bean="queue-counter"/>
            </component>
            <enrich source="#[payload['${r2s.dlq}']]" target="#[flowVars['dlq-size']]"/>
            <enrich source="#[payload['${r2s.queue}']]" target="#[flowVars['queue-size']]"/>
        </enricher>

The sample code for a Mule Java component, written for ActiveMQ, is shown below. Note that failover is fully supported. In the example, performance is not a paramount concern, therefore, all the queues are being counted in one call. The code can be easily modified though, so the queue name is also passed in as a property and counting will be done only for the specified queue.


Explore the core elements of owning an API strategy and best practices for effective API programs. Download the API Owner's Manual, brought to you by 3Scale by Red Hat

Topics:
java ,enterprise-integration ,tutorial ,jms ,integration ,mule ,activemq ,muleesb ,message queue

Published at DZone with permission of Ross Mason, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}