Dynamic scaling of JMS consumer in spring boot

谁说我不能喝 提交于 2019-11-30 19:06:44

问题


I am trying to build a spring-boot application which will read data from a activeMQ producer. I want to resources available on customer to the optimum.In my spring-boot application, I want to configure multiple consumers and all these consumer will connect to a single queue.

Is their a way I can dynamically scale up and scale down the consumers on sprint-boot application?


回答1:


the consumers your are talking about, are different thread of the DefaultMessageListenerContainer or different instances of DefaultMessageListenerContainer ?

you can increase and decrease dynamically threads number of DefaultMessageListenerContainer by changing

org.springframework.jms.listener.DefaultMessageListenerContainer.concurrentConsumers

and

org.springframework.jms.listener.DefaultMessageListenerContainer.maxConcurrentConsumers accordingly

UPDATE

if you work with multiple consumers and/or threads you need to adapt the prefetchPolicy.

persistent queues (default value: 1000)
non-persistent queues (default value: 1000)
persistent topics (default value: 100)
non-persistent topics (default value: Short.MAX_VALUE - 1)

all messages was dispatched to the first connected consumer and when another one connects he don't receive messages, so to change this behavior if you have concurrent consumer for a queue you need to set prefetchPolicy to a lower value than default. for example add this jms.prefetchPolicy.queuePrefetch=1 to the uri config in activemq.xml or set it on the client url like this

ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://172.16.143.99:61616?jms.prefetchPolicy.queuePrefetch=1");

Large prefetch values are recommended for high performance with high message volumes. However, for lower message volumes, where each message takes a long time to process, the prefetch should be set to 1. This ensures that a consumer is only processing one message at a time. Specifying a prefetch limit of zero, however, will cause the consumer to poll for messages, one at a time, instead of the message being pushed to the consumer.

Take a look at http://activemq.apache.org/what-is-the-prefetch-limit-for.html

And

http://activemq.apache.org/destination-options.html



来源:https://stackoverflow.com/questions/43042909/dynamic-scaling-of-jms-consumer-in-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!