Stunner
Stunner

Reputation: 1121

IBM MQ JMS java listener code keeping the program running

This question is in continuation to the question asked IBM MQ listener service - onMessage not getting triggered

Let's say the same code is in production , now how do we keep the program running. I felt adding scanner code is not the correct or effective way of achieving it. Is there any better solution to keep the program running so when the end of main is reached , program will not quit. Ideally i think the call to start method qc.start() should wait indefinitely. Please let me know how do we implement this in a proper and ideal way.

Requirement is the listener should run indefinitely waiting for new messages. I do not have any other business condition to put in the main method inorder to keep the program running. Adding scanner code doesn't seem a good approach. What's the ideal way ??

Upvotes: 0

Views: 999

Answers (2)

Axel Podehl
Axel Podehl

Reputation: 4303

Please note that JMS comes with 2 methods of message dispatching: Synchronous vs. Asynchronous. For starters, and if your program doesn't have to do anything unless there is a new message, try out synchronous dispatching first. You don't need to create and set a Listener object:

  consumer = queueSession.createConsumer(queue); 
  qc.start();
  while( nummsgs<maxmsgs || maxmsgs==0 ) {

    Message message = consumer.receive(1000); // receive the next message or wait up to 1 second, for example
    if (message != null) {

      nummsgs++;
      ... processing code ...
    }
 }

(I usually like to make the while's end condition based on an input parameter like int maxmsgs = 0; so you can automatically stop after processing 100000 messages, e.g. for performance testing, or just specify 0 to keep it running forever).

There's nothing wrong with this approach and because the code is simpler to write and understand, I would recommend it as a first-level approach. The main loop is simply the MQ-dispatcher loop which makes sense if that's the main purpose of your program.

If your program does many things, one of which is MQ receiving, asynchronous dispatching might be better. The main loop does not have an 'MQ focus' anymore. It just sits there and waits until someone presses Ctrl-C or kills the program.

  consumer = queueSession.createConsumer(queue);
  Listener listener = new Listener();
  consumer.setMessageListener(listener);
  qc.start();

  Object waitLock = new Object();
  while( nummsgs<maxmsgs || maxmsgs==0 ) {

    synchronized(waitLock) {
      waitLock.wait(1000); // wait up to 1 second, for example
    }
  }

Upvotes: 1

chughts
chughts

Reputation: 4747

What I think what you are asking is how to keep a Java program running indefinitely.

There are several ways, but you do need a way of terminating it, either by user interrupt or a signal, and you don't want it in a tight loop so it will consume all CPU and stack memory, leaving nothing for your onMessage event.

You don't want to put to sleep the onMessage so it should be spawned in a secondary thread, allowing the main thread to periodically awaken from a sleep to check for a termination signal.

The basics are


Thread worker = new Thread(new yourOnMessageThread());
worker.start();

while(true) {
  try {
    Thread.sleep(sleep duration);
  } catch(InterruptedException ex) {
    // check for termination signal
    // if required, terminate
  }
}

Upvotes: 2

Related Questions