Neither JMS nor Core session objects are thread-safe so I would discourage you from creating multiple consumers + listeners on a single session.
I would instead recommend you create a new session for each consumer. Sessions are considered lightweight both in JMS and Core. This is even noted in the corresponding JMS JavaDoc. To be clear, having hundreds of sessions is no more or less problematic than having hundreds of consumers. Justin [1] https://docs.oracle.com/javaee/7/api/javax/jms/Session.html On Wed, Jul 2, 2025 at 4:24 AM Gianluca Milza <gianlucami...@me.com.invalid> wrote: > Hi all, > > I need to understand if it is possible to create new > ClientConsumer/MessageConsumer, each with their associated > MessageHandler/MessageListener, after i already created consumers in the > same way and they started receiving data. > > Basically i have a topic which will receive data updates for a large > number of items with an id identifying them. > My application will be composed of multiple instances, each one > subscribing to updates on a different set of items (using filters) and that > set is not fixed nor is predictable when a new item will need to be > subscribed: could be at startup or hours later. > > So at any time i could need to call createConsumer in a session, > particularly when async listeners have already started receiving messages > on onMessage. > Creating new sessions is problematic, as the number of items subscribed on > each instance could be on the hundreds. > > Regarding that, core ClientConsumer setMessageHandler states that: > Note that setting a handler dedicates the parent session, and its child > producers and consumers, to the session-wide handler delivery thread of > control. > > Would this thread being the one on which onMessage is called? > In that case even if is not that clean, i could process creating new > consumers inside of that callback? > Or i am forced to synchronize the onMessage with the separate part where i > create the new consumer? > > I tried to understand from JMS only documentation and discussions if there > is a way to do this, but multi threading limitations on session seems not > giving me a lot of options . > > Thanks, > Gianluca Milza >