Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to thread pool a Spring JMS listener

I am setting up a JMS subscriber listener as follows with the goal of achieving a pool of 5 threads listening to topATopic, however, what I see at runtime is multiple consumers processing the same record (recordCount*#of consumers).

I am assuming I am doing something wrong considering I am new to spring.

<bean id="messageListener" class="com.abc.app.mdp.Receiver">
<property name="bean" ref="bean" />
</bean>

<jms:listener-container container-type="default"
connection-factory="connectionFactory" acknowledge="auto" concurrency="5" destination-type="topic" prefetch="1" cache="none" >
<jms:listener destination="topCli_Service" ref="messageListener" 
method="onMessage" subscription="AProjectSubscriber" />
</jms:listener-container>

<bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryB ean">
<property name="jndiName" value="jms/jms-top-notx" />
</bean>

Can somebody please point me in a direction to achieve my goal?

like image 852
cmutt78 Avatar asked Apr 27 '11 16:04

cmutt78


People also ask

How to listen topic in JMS using spring JMS?

This page will walk through Spring JMS topic listener example. To listen topic, we need to enable publish/subscribe functionality. By default JMS messaging is performed for point-to-point to listen queue. Here in our example we will create listener using @JmsListener and publish message using JmsTemplate .

What is a JMS listener?

A JMS listener will pick up the message and send a status message to two different status queues. On each queue, a different message listener container will read the status. 2. General Project Overview We will use the following tools/frameworks: Spring JMS 5.1 Spring Boot 2.1 ActiveMQ 5.15 Maven 3.6

How to use ActiveMQ in spring JMS?

We start from a previous Spring JMS Example using ActiveMQ. We adapt it so that an order message is sent to an order queue. A JMS listener will pick up the message and send a status message to two different status queues. On each queue, a different message listener container will read the status. 2. General Project Overview

What technologies are being used in JMS messaging?

By default JMS messaging is performed for point-to-point to listen queue. Here in our example we will create listener using @JmsListener and publish message using JmsTemplate . Find the technologies being used in our example. 1. Java 11 2. Spring 5.2.8.RELEASE 3. Spring Boot 2.3.2.RELEASE 4. Maven 3.5.2


2 Answers

Take a look at the concurrency setting on your listener container configuration. The Spring JMS doc suggests that concurrency should be set to 1 for topic listeners. See below.

concurrency: The number of concurrent sessions/consumers to start for each listener. Can either be a simple number indicating the maximum number (e.g. "5") or a range indicating the lower as well as the upper limit (e.g. "3-5"). Note that a specified minimum is just a hint and might be ignored at runtime. Default is 1; keep concurrency limited to 1 in case of a topic listener or if queue ordering is important; consider raising it for general queues.

This post is similar to the rest of your question.

If you need multiple threads to keep up with message volume, your message listener could delegate to a Spring TaskExecutor to process the messages asynchronously. TaskExecutors can be backed by a number of implementations including Thread Pool.

like image 158
MarkOfHall Avatar answered Sep 20 '22 22:09

MarkOfHall


If you want a given message to be consumed by one and only one consumer, you should use a Queue instead of a Topic. A Topic message is broadcast to all available consumers.

like image 26
Joseph Ottinger Avatar answered Sep 18 '22 22:09

Joseph Ottinger