Discussions

EJB design: How to assure the sequence of message received by mdb?

  1. How to assure the sequence of message received by mdb? (4 messages)

    one manner is to set the mdb as singleton, but it has more restriction, does anyone know some other design pattern?
    so urgent, thank you very much
  2. I think it depends what you mean.

    First of all, the JMS spec makes no guarantee that messages will be delivered in any particular order. This is down to the particular provider implementation. I don't claim to be an expert on all messaging systems, but I do know that both MQSeries and WebLogic JMS give you a way to define the order in which messages should be delivered. Without this, you may assume that messages are probably being delivered in FIFO order but I don't think you can rely on that.

    Second, if it is important to you that no messages are ever processed "out of sequence" then you are going to be looking at a singleton to consume the messages. If you have two consumers in parallel on the same queue, there is the possibility that messages can be processed and committed out of sequence unless you put some kind of synchronisation in place, in which case you are effectively serializing the consumers so you're not much better off than if you just had a singleton. (Messages could be committed out of sequence if message 1 takes a long time to process and message 2 is very quick to process. In this case, message 2 will be processed and committed before message 1 if they are being processed in parallel.)
  3. thanks for your reply.

    I have do some test on jboss's jms server, And that seems it use FIFO strategy default, so it apply with my request. it's not the j2ee spec, on this point, if I couldn't find a container that support FIFO, it seems so bad. I think why j2ee's spec couldn't add this to the specification.

    and second, I set the MDB's instance pool size to 1, means only one mdb instance could be used by container at a time, so I think for a given MDB, the messages can be sequenced deliver to it.

    and , you pointed the scene,two MDB(two consumer), on that point, on my application, I don't care about this, because although the two MDB runs parallel,but on each MDB's process,because it has only one instance at a time, so there are sequence processed. the two mdb on my application are two different process logic, so no need to assure the two MDBs to keep sequence.
    for example, client send alarm message to topic, there are two MDB subscriber on the topic, one is alarm analysis , it do the alarm analysis, so it need assure the alarm arrived and processed in sequence. so I set this to only permit one instance. and another MDB do the system logging, I also set this one instance. so as messages are send, on each mdb, they are processed in sequence. but I know, the logging MDB perhaps run fast than analysis MDB, but I don't care about it. I only want the analysis process on sequence, and the logging run in sequence.
    and now , I havn't more time to think another way, Perhaps I could only use this method, although I think it's not so good .
  4. one manner is to set the mdb as singleton, but it has more restriction, does anyone know some other design pattern?so urgent, thank you very much

    Most good JMS providers will preserve order of messages sent by a single producer; then preserve order of messages as they arrive on a queue.

    However if you have multiple consumers on a queue then your ordering is broken since messages will typically be processed concurrently in different threads/processes which results in a non-deterministic order of processing of the messages.

    ActiveMQ supports a number of features to solve this...

    Exclusive Consumer...
    http://activemq.org/Exclusive+Consumer
    which ensures only 1 consumer is active at once, or the much more powerful...

    Message Groups...
    http://activemq.org/Message+Groups
    which allows you to split a queue into parallel groups using hte JMSXGroupID header to ensure all messages with the same JMSXGroupID value are processed in order by the same consumer.

    FWIW ActiveMQ 4.x also supports total ordering across all consumers on a topic.
    http://activemq.org/Total+Ordering

    James
    LogicBlaze
  5. thanks for your reply, I'm now looking it , thank you.