开发者

How to force ActiveMQ connection to choose a broker for a new consumer randomly?

I use the following url to create ActiveMQCo开发者_如何学CnnactionFactory:

failover:(tcp://server1:port,tcp://server2:port,tcp://server2:port)

What I want to do is to create multiple message consumers from this network of brokers. The following is not a real code, but it helps to undestand how I do that:

ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("BROKER_URL");
connection = connectionFactory.createConnection();
connection.start();

for (int i=0; i<10; i++) {
session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
Destination queue = consumerSession.createQueue("QUEUE_NAME");
consumer = consumerSession.createConsumer(queue);
consumer.setMessageListener(new MessageListener());
}

The problem is that all consumers will be connected to one randomly choosen broker. But I want them to be balanced over the network of brokers.

I believe it is possible to do that by creating multiple connections with the factory.

But what are the best practices for that? And is this a good thing which I want? :)


Actually, the consumer would not be connected to a randomly chosen broker.

A connection is the part that connects to a broker. With the connection string you have provided, you will have ONE connection mapped to ONE randomly chosen broker. All consumers have their own sessions but these would use the same ONE connection to that ONE broker.

The only setting I know of, is that you can disable the randomize behavior of the failover protocol by setting ?randomize=false on the connection string. This would mean your connection will first try the first, then the second, then the third, etc.

But to achieve your requirement. I would make each consumer to have it's own connection. This, together with the randomize feature in the fail-over protocol would kinda load-balance the consumers; but not for real, there is no intelligence in there and is just "randomizing" the broker it is connecting to.

This means, I would do the following (from your code)

ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("BROKER_URL");

for (int i=0; i<10; i++) {

connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
Destination queue = consumerSession.createQueue("QUEUE_NAME");
consumer = consumerSession.createConsumer(queue);
consumer.setMessageListener(new MessageListener());

}

This way, each consumer will have it's own connection to "a" broker of your fail-over connection string

UPDATED AFTER QUESTION CHANGE:

If you want to let ActiveMQ randomly choose a broker for each consumer, the above mentioned solution is the way to go.

The best practice would be to put your consumers and producers as close to each other as possible. For this, I would recommend lowering the network consumer priority, so the local consumer and producer would have highest priority. Only when the local consumer is not idle, it would distribute further over the network to other consumers.

In addition to that, it will be a good idea if the operation on consumer side is long running to set a lower prefetch value, so that the messages do get load balanced around the network of brokers instead of one consumer snatching up 1,000 messages while other consumers are idle.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜