Announcement Announcement Module
No announcement yet.
newbie question: multithreadedness and SimpleMessageListenerContainer Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • newbie question: multithreadedness and SimpleMessageListenerContainer

    I have a very straightforward application using spring that is set up to use an ActiveMQ queue in order to deliver documents to our application, and our application processes the documents.

    Currently we use SimpleMessageListenerContainer to get the messages, and then from the onMessage function of our MDB, we use an executor to process each document multi-threaded.

    My understanding is that this MDB is single-threaded, ie. there is only one thread that gets onMessage called. Is there a way for the MDB to be multi-threaded, ie. for Spring to use call onMessage in a multi-threaded manner instead of having a single thread that pushes to multiple threads?

    The reason for this is that we want to keep the document persisted in the ActiveMQ queue in case of failure. If there is an exception during the onMessage function, the document stays in the ActiveMQ queue, which is essentially what we want.

    So really, we want the Listener to be multi-threaded and for Spring to call it for each message that comes in.

    Is this possible? I have scoured the Internet for more information, but I'm still relatively new to Spring so as far as I've determined, I couldn't find any documentation that talks about this specific situation.

    Thanks in advance for any help!

  • #2
    An MDB thread of execution is single threaded but the container can horizontally scale by instantiating multiple MDB's so there are multiple listeners listening on the queue to allow parallel processing of messages.

    The same is true with Spring MDP's. To control how many threads are calling your message listener (i.e. independantly receiving messages in your MLC) you set the currentConsumers/maxConcurrentConsumers attributes.


    • #3
      THANK YOU! This was exactly what I was looking for, but trying to find this was like a needle in a haystack. There doesn't seem to be any documentation that refers to this and from the description from javadocs I didn't realize this would make it multi-threaded. But once I set this, everything worked as described.

      Thanks again!


      • #4
        Just one thing to remember...

        With J2EE MDB's, if the container wants to scale processing, it just instantiates additional MDB instances. Messages are delivered serially to each MDB instance. This allows the MDB instances to be stateful (i.e. no more than one thread will ever call onMessage() on a bean instance concurrently).

        With Spring MDP's, multiple threads will typically be calling your MDP concurrently so you need to make sure that it is stateless or that any shared state is synchronized otherwise you will encounter race conditions.