Search code examples
jmsqpidjboss-messagingopenjmssonicmq

Trigger Monitor in JMS Provider like MQ Series


In Websphere MQ I can configure a Queue to Trigger an Application when a message arrives.
In that way I can have an Application that starts only if needs and I don't have to manage one daemon that wait for message in that queue.
More information about here

Are There an open source JMS Provider that bring this functionality?

I tried ActiveMQ but it hasn't triggers.


Solution

  • qpid does not have a websphere-MQ like monitor trigger feature. I know ActiveMQ doesn't either, and I suspect this may be true of other JMS providers as well. However, it is possible to roll out your own monitor-trigger.

    A homebrew monitor-trigger would then become an application process(albeit light-lightweight) that you will have to manage though, would you be better off managing an actual application thread itself?


    To implement a monitor trigger in qpid:

    JMS spec defines an asynchronous delivery mode. See section 4.5.2. So you should be able to do this with any JMS provider. An asynchronous listener implements the javax.jms.MessageListener interface. The method onMessage() needs to be implemented and serves as the callback function when any new message appears on the queue it is subscribed to.

    Suppose the main application queue is mainQ. You create a new MessageListener for mainQ, in browse mode - so as to not actually consume any messages from mainQ

    Destination mainQ = (Destination) session.createQueue("mainQ; {mode: browse}");
    MessageConsumer mainQConsumer = session.createConsumer(mainQ);
    mainQConsumer.setMessageListener(this);
    

    In the onMessage() function you can either create a new message in a separate triggerQ or you can skip this step and get right to starting up the application.

    public void onMessage(Message message)
    {
      TextMessage triggerMessage = session.createTextMessage("Trigger-start-Application-X");
    
      Destination triggerQ = (Destination) session.createQueue("triggerQ");
      triggerQProducer = session.createProducer(triggerQ);
      this.triggerQProducer.send(triggerMessage);
    
      // Or alternatively:
      // if (!applicationIsActive()) activateApplication()
    }
    

    See full working sample here: https://github.com/foragerr/qpid-trigger-demo