Message Driven Bean and message consumption order

I have an MDB that gets subscribed to a topic which sends messages whose content is eventually persisted to a DB.

I know MDBs are pooled, and therefore the container is able to handle more than one incoming message in parallel. In my case, the order in which those messages are consumed (and then persisted) is important. I don't want an MDB instance pool to consume and then persist messages in a different order as they get published in the JMS topic.

Can this be an issue? If so, is there a way of telling the container to follow strict incoming order when consuming messages?

Upvotes: 2

Views: 2133

Answers (2)

pgras
pgras

Reputation: 12780

Copied from there:

To ensure that receipt order matches the order in which the client sent the message, you must do the following:

  • Set max-beans-in-free-pool to 1 for the MDB. This ensures that the MDB is the sole consumer of the message.

  • If your MDBs are deployed on a cluster, deploy them to a single node in the cluster, [...].

  • To ensure message ordering in the event of transaction rollback and recovery, configure a custom connection factory with MessagesMaximum set to 1, and ensure that no redelivery delay is configured. For more information see [...].

Upvotes: 2

Rich
Rich

Reputation: 15767

You should be able to limit the size of the MDB pool to 1, thus ensuring that the messages are processed in the correct order.

Of course, if you still want some parallelism in there, then you have a couple of options, but that really depends on the data.

If certain messages have something in common and the order of processing only matters within that group of messages that share a common value, then you may need to have multiple topics, or use Queues and a threadpool.

If on the other hand certain parts of the logic associated with the arrival of a message can take place in parallel, while other bits cannot, then you will need to split the logic up into the parallel-ok and parallel-not-ok parts, and process those bits accordingly.

Upvotes: 2

Related Questions