# Solace Spring JMSListner

**URL:** <https://community.solace.com/t/solace-spring-jmslistner/915>\
**Category:** Connectors & Integrations\
**Tags:** spring, jms\
**Created:** [September 2, 2021, 5:13pm UTC](https://community.solace.com/t/solace-spring-jmslistner/915 "2021-09-02T17:13:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bsudabathula](https://avatars.discourse-cdn.com/v4/letter/b/f6c823/32.png) [@bsudabathula](https://community.solace.com/u/bsudabathula)\
**Post date:** [September 2, 2021, 5:13pm UTC](https://community.solace.com/t/solace-spring-jmslistner/915/1 "2021-09-02T17:13:56Z")

</div>

Team, I am new to the Solace and i am currently using in the Solace SpringBoot JMS.

I have created a Queue and Published 500 Message to it. I want to implement a Multithreading behavior to my consumer which will consume the message parallelly and process instead of one by one.

I have added the minimum concurrency as 5 and maximum 10. When i start the application , i can see 5 consumers are in active but only one consumer is processing all the Messages. I want to know how we can use all the consumers and process the messages parlelly.

Sample Code:

```auto
@JmsListener(destination = "Q/tab/Consumer/test", concurrency = "5-10" )
	public void handle(Message message) {

		counter = counter+1;
		Date receiveTime = new Date();
		System.out.println(
				"Consumer Process started at Received at " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(receiveTime));
		if (message instanceof TextMessage) {
			TextMessage tm = (TextMessage) message;
			try {
				
				tm.getText();
				
				System.out.println(Thread.currentThread().getId());
				System.out.println("Processed ----" + counter + " so far" );

			} catch (JMSException e) {
				e.printStackTrace();
			}
		} else {
			System.out.println(message.toString());
		}
	}

```

 ![image.png](https://canada1.discourse-cdn.com/solace/original/1X/5699c124c0442f5abfa583518b1d195bf3a55de0.png)

---

<div class="post-metadata">

**Author:** ![marc](https://yyz1.discourse-cdn.com/solace/user_avatar/community.solace.com/marc/32/2191_2.png) [@marc](https://community.solace.com/u/marc)\
**Post date:** [September 2, 2021, 5:31pm UTC](https://community.solace.com/t/solace-spring-jmslistner/915/2 "2021-09-02T17:31:57Z")

</div>

Hi @bsudabathula,

Welcome to the Solace Community!  
Can you check your queue access type under `Settings`?  
You’d want to use a “non-exclusive” queue to spread the load across multiple consumers. Exclusive would deliver all the messages to the first consumer to connect and treat the others as secondary, tertiary, etc.

 ![image.png](https://canada1.discourse-cdn.com/solace/original/2X/1/1859d935b106d37dbae3db16607b7a592b665a40.png)

Hope that helps!

---

<div class="post-metadata">

**Author:** ![bsudabathula](https://avatars.discourse-cdn.com/v4/letter/b/f6c823/32.png) [@bsudabathula](https://community.solace.com/u/bsudabathula)\
**Post date:** [September 2, 2021, 5:35pm UTC](https://community.solace.com/t/solace-spring-jmslistner/915/3 "2021-09-02T17:35:24Z")

</div>

@marc Thanks for your quick response. Is it possible to implement a concurrency(5 Threads running parallelly) with a single consumer?

---

<div class="post-metadata">

**Author:** ![RobO](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@RobO](https://community.solace.com/u/RobO)\
**Post date:** [September 2, 2021, 5:45pm UTC](https://community.solace.com/t/solace-spring-jmslistner/915/4 "2021-09-02T17:45:08Z")

</div>

Yes. The concurrency attribute of the JMS listener will allow for multiple consumer threads.

---

<div class="post-metadata">

**Author:** ![bsudabathula](https://avatars.discourse-cdn.com/v4/letter/b/f6c823/32.png) [@bsudabathula](https://community.solace.com/u/bsudabathula)\
**Post date:** [September 2, 2021, 6:53pm UTC](https://community.solace.com/t/solace-spring-jmslistner/915/5 "2021-09-02T18:53:19Z")

</div>

@RobO ,My concurrency attribute set to 5, In this scenario it is creating 5 consumers on my queue. Is it possible to have a single consumer with multiple threads ?

---

<div class="post-metadata">

**Author:** ![RobO](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@RobO](https://community.solace.com/u/RobO)\
**Post date:** [September 2, 2021, 7:25pm UTC](https://community.solace.com/t/solace-spring-jmslistner/915/6 "2021-09-02T19:25:24Z")

</div>

Check out the @Async annotation. The single consumer thread can invoke a method that has the annotation of @Async with a defined Task Executor (defines the number of threads). Each call to the method should be handled in a new thread and the consumer thread should not block waiting for a return. In other words, the consumer thread should consume the next message and hand it off to the next thread, etc, etc, etc. A simple example of an async method is [here](https://www.baeldung.com/spring-async).

A more complex way to accomplish similar capability is to use the Spring Integration framework. You can create a channel (in-memory queue) that the messages can be put onto and then threads can pull from that channel and process. It still uses the TaskExecutor feature from Spring to accomplish this.

---

<div class="post-metadata">

**Author:** ![Aaron](https://yyz1.discourse-cdn.com/solace/user_avatar/community.solace.com/aaron/32/843_2.png) [@Aaron](https://community.solace.com/u/Aaron)\
**Post date:** [September 9, 2021, 9:45pm UTC](https://community.solace.com/t/solace-spring-jmslistner/915/7 "2021-09-09T21:45:21Z")

</div>

Sounds like the Reactor pattern! I made a video on how to modify the JCSMP consumer sample to be multi-threaded…!

[![](https://canada1.discourse-cdn.com/solace/original/2X/e/e6555308a84730d8281ab585dd0aeed606927ab4.jpeg "Implementing the Reactor Pattern in Solace") ](https://www.youtube.com/watch?v=5ilYW2VqTqQ)
