Is there a way to make the message queue processing by MDB, on WildFly, FIFO?

1.1k Views Asked by At

Creating a JMS queue on WildFly 8.2 (with JMS provider HornetQ), and having a message driven bean "activated" by this queue, I saw that if the producer sends, in a rapid succession, multiple messages to the queue, the message driven bean does not process them necessarily in the order in which they were sent. Can one configure WildFly so that the messages are processed in the order in which they are sent (first in first out)?

1

There are 1 best solutions below

1
John Donn On

(I think to have understood what happens after reading https://stackoverflow.com/a/6744508/999264)

There are multiple threads which execute the onMessage method of the message driven bean (MDB), one thread per message, and thus, if multiple messages arrive almost simultaneously, one cannot know which message will be processed first (because one cannot know which one of the threads will finish onMessage execution first). The only way to know this is to make sure that the number of threads is 1: in this case the only thread first processes the first message, then the second one, and so on.

In WildFly and JBoss the annotation @MessageDriven has the "activation config property" maxSession, which, as I understand, controls the maximal number of threads used to process the messages which arrive from the queue to the MDB. Setting its value to 1, as below

@MessageDriven(activationConfig = {
                @ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "java:/jms/queue/myOwnQueue"),
                @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
                @ActivationConfigProperty(propertyName = "maxSession", propertyValue = "1")})
public class MyOwnMDB implements MessageListener {

    public void onMessage(Message message) {
        System.out.println("message received " + message.toString());
    }

}

and running the code, I see that indeed the messages are being processed, by the message driven bean, in the order in which they were sent.

I changed the title of the question since the original title, "Is there a way to make the message queue on WildFly FIFO?", appears incorrect: the queue itself is FIFO (actually, I found written somewhere that this is a part of JMS spec, though I cannot pinpoint the exact place).