ActiveMQ throttling consumer

2019-02-23 16:19发布

问题:

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
}

}

回答1:

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")



回答2:

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



回答3:

Take into account that Camel Throttler keeps the exchanges in-memory while they are blocked by the Throttler. So, if you have 100 queue consumers and the Server (exposing the SOAP service) is slow, you may have in-memory up to 100 exchanges!

Posted this question for this: Throttle consumption rate of all JMS consumers listening on an ActiveMQ queue



标签: java activemq