简体   繁体   中英

Process pending messages faster in ActiveMQ using concurrent user

I have a queue which is getting consumed by an Spring Boot application. As my Spring Boot application is waiting for response from a REST API its not able to process the incoming messages faster due to which the number of pending messages is increasing on my queue.

I have done some R&D and came out with the solution which is mentioned below. Kindly help me by reviewing the solution so that I can know whether I am on a right track.

My current ActiveMQ configuration:

@Bean
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    factory.setConnectionFactory(activeMQConnectionFactory());
    factory.setConcurrency("1-1");
    return factory;
}

@Bean
public ActiveMQConnectionFactory activeMQConnectionFactory() {
    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
    factory.setBrokerURL(brokerURL);
    factory.setUserName(userName);
    factory.setPassword(password);
    return factory;
}

My planning for solution:-

@Bean
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    factory.setConnectionFactory(activeMQConnectionFactory());
    factory.setConcurrency("1-50");
    factory.setMaxMessagesPerTask(10);
    factory.setReceiveTimeout(5000L); // 5 seconds
    return factory;
    }
    
@Bean
public ActiveMQConnectionFactory activeMQConnectionFactory() {
    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
    factory.setBrokerURL(brokerURL);
    factory.setUserName(userName);
    factory.setPassword(password);
    
    ActiveMQPrefetchPolicy policy = new ActiveMQPrefetchPolicy();
    policy.setQueuePrefetch(1);
    factory.setPrefetchPolicy(policy);
    return factory;
}

Generally speaking, more consumers (especially concurrent consumers) will process messages more quickly than fewer consumers so this looks good from that perspective.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM