Reputation: 361
A Java EE application gets data delivered by a JMS queue. Unfortunately, some of the messages which are delivered depend on each other, so they have to be processed in the correct order. As far i understand it, i can't rely on JMS here (or can i? i know that they will be sent in the correct order).
Additionally, it may be the case that there will be one kind of message that will be split by the provider of the messages, so that in some cases thousands of these messages will be sent to the application, all of them related to a specific entity. It would not be necessary to handle them one by one (which would be the easiest way, e.g. in a MDB) and i fear it will be a bad performance if i handle them this way, because i always have to save some information in the database, so i'd prefer to batch them in some way and handle them altogether in one transaction. But i don't know how to do this, since i didn't detect a way to batch messages while reading from a queue. I could get all messages out of a queue every second or so and process them, but then i don't know for sure if messages that depend on each other are already in there.
So i think i need some kind of buffering/caching or sth. like this between receiving and processing the JMS messages, but i don't see a good approach at the moment.
Any ideas how to handle this scenario?
Upvotes: 2
Views: 1656
Reputation: 16060
Section 4.4.10 of the JMS spec states that:
JMS defines that messages sent by a session to a destination must be received in the order in which they were sent (see Section 4.4.10.2 ”Order of Message Sends,” for a few qualifications). This defines a partial ordering constraint on a session’s input message stream
(The qualifications are mainly about QoS and non-persistent messages).
The spec continues:
JMS does not define order of message receipt across destinations or across a destination’s messages sent from multiple sessions. This aspect of a session’s input message stream order is timing-dependent. It is not under application control.
So, relying on ordering in your JMS client really comes down to the session scope of your producer and the number of producers.
You could also consider grouping the related messages in a transaction to ensure atomicity of whatever operation you're performing.
Hope that helps a bit.
EDIT: I assume you know "related messages"/dependencies by using some sort of correlationID? I'm getting the idea that your problem is very similar to eg. TCP, so perhaps you could use the algorithms from that area to ensure ordered delivery of your messages?
Cheers,
Upvotes: 3
Reputation: 73568
A JMS Queue
will definitely process them in the correct order. It is, after all, a Queue (and you can't cut in line).
Upvotes: 0