Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveMQ throttling consumer

Tags:

java

activemq

i want to do a throttling to a consumer of some queue in the activeMQ, in hornetq (of jboss, this is do it with annotations on the definition of the mdb Consumer). I can't find any similar in the documentation of activemq, the closest that i find was this

consumer.recvDelay   0 ms    Pause consumer for recvDelay milliseconds with each message (allows consumer throttling).

from: http://activemq.apache.org/activemq-performance-module-users-manual.html

But there i can't find how i can do it in java.

Thanks in advance,

Regards.

EDIT: Here is the ActiveMQManager code and the consumer code:

public class ActiveMQManager {

    private static ActiveMQConnectionFactory CONNECTION_FACTORY;

    public static Connection CONNECTION;

    public static Session SESSION;

    public static Destination TEST_QUEUE;

    public static void start() {
        try {

            CONNECTION_FACTORY = new ActiveMQConnectionFactory("vm://localhost");

            CONNECTION = CONNECTION_FACTORY.createConnection();
            CONNECTION.start();

            SESSION = CONNECTION.createSession(false,
                    Session.CLIENT_ACKNOWLEDGE);

            TestClient testClient = new TestClient();

            TEST_QUEUE = SESSION.createQueue("TEST.QUEUE");

            MessageConsumer testConsumer = SESSION.createConsumer(TEST_QUEUE);
            test.setMessageListener(testClient);

        } catch (Exception e) {
        }
    }

    public static void stop() {
        try {
            // Clean up
            SESSION.close();
            CONNECTION.close();
        } catch (JMSException e) {
            log.error(e);
        }
    }
}

The consumer code is very simple (for this example):

public class TestConsumer implements MessageListener {

    @Override
    public void onMessage(Message message) {
        //Do something with the message
    }

}
like image 805
Enrique San Martín Avatar asked Dec 21 '22 11:12

Enrique San Martín


2 Answers

this depends on the consumer technology being used...but here are a few options

  • you can manually introduce a delay in your consumer code (not an exact science but this will limit the throughput)

  • you can also control the number of threads that your consumer uses by setting maxConcurrentConsumers property of you JMS connection...that said, this won't throttle message throughput, just limit the level of concurrency being used by your consumer

  • better yet, you can set the exact number of messages to consume per time period using a throttler EIP implementation

    for example, this is trivial using the Camel Throttler

    from("activemq:queueA").throttle(10).to("activemq:queueB")

like image 113
Ben ODay Avatar answered Dec 22 '22 23:12

Ben ODay


With ActiveMQ you can set the consumer prefetch limit: http://activemq.apache.org/what-is-the-prefetch-limit-for.html

To configure it, you can use the connection URL (most of the configurations can be done using the URL) or the Java API.

For more interesting parameters: http://activemq.apache.org/connection-configuration-uri.html

like image 38
Gilberto Torrezan Avatar answered Dec 23 '22 01:12

Gilberto Torrezan