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.
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. With this approach, counting in the flow is as simple as this:
Learn why we are the Leaders in API management and iPaaS
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.