0
votes

I am studying for the Spring Core certification and I have some doubt about how JMS works and about how Spring handle it.

So I know that the JMS Core Components are:

  1. Message
  2. Destination
  3. Connection
  4. Session
  5. MessageProducer
  6. MessageConsumer

From what I have understand a Message is what is send and shared with other entities (other products that have to consume this data) and a message can have differents shapes: TextMessage, ObjectMessage, MapMessage, BytesMessage and StreamMessage.

Ok, from what I understand the Destination component of the JMS Core Components defines who receives messages and the logic for dispatching messages.

So I know that I have 2 kinds of destinations that are:

  1. Queue: that define a point to point message. From what I have understand I can have some Message Producer that put the generated messages into a queue and then I have some MessageConsumer that consume message. Each message can be consumed by a single MessageConsumer.

    Now my doubt is: a MessageConsumer consume message putted into the queue by a specific MessageProducer (there is something like a 1 to 1 relationship between MessageConsumer and MessageProducer) or a MessageProducer put an id related to the specific MessageConsumer that have to read the message that it put into the queue?

So for example, in the first case if I have 2 MessageProducer respectivelly named Producer-1 and Producer-2 I need to have 2 MessageConsumer named Consumer-1 and Consumer-2 where Consumer-1 consume messages produced from Producer-1 and Consumer-2 consume messages produced from **Producer-2).

Instead in the second case the Consumer-1 can consum messages produced by from both Producer-1 and Produer-2 because the MessageProducer put a message in the queue specifying who is the MessageConsumer that have to consume the message.

How exactly works? What is the correct logic?

1

1 Answers

0
votes

if I have 2 MessageProducer respectivelly named Producer-1 and Producer-2 I need to have 2 MessageConsumer named Consumer-1 and Consumer-2 where Consumer-1 consume messages produced from Producer-1 and Consumer-2 consume messages produced from **Producer-2).

Not really. The message consumer is connected to the queue and it will receive all messages that are put in the queue irrespective of the Producer. So if you have two producers that put a message each in the queue, it will be consumed by the single consumer connected to it.

If you wish to have messages produced by different producers targeted to specific consumers, you should use a concept known as 'message selectors'. To achieve this, Message producers should add 'headers' to the messages (system headers or custom ones) and then message listeners can specify that they receive messages that match a specific header.