1

I have a JMS listener app, and the class QueueReceive implements MessageListener.the main function as below:

public static void main(String[] args) throws Exception {

    InitialContext ic = getInitialContext();
    QueueReceive qr = new QueueReceive();
    qr.init(ic, QUEUE);

    System.out.println("JMS Ready To Receive Messages (
         To quit, send a \"quit\" message).");    
    // Wait until a "quit" message has been received.

    synchronized(qr) {
        while (! qr.quit) {
           try {
              qr.wait();
           } catch (InterruptedException ie) {}
           }
        }
        qr.close();
    }

Is there any way to quit the app at a specific time within the program not by way of the jms Message?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Pengyi Wang
  • 123
  • 1
  • 16

4 Answers4

3

You can use TimerTask [Sample Code] for this purpose.

Example:

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class ExitOn {
Timer timer = new Timer();
TimerTask exitApp = new TimerTask() {
    @Override
    public void run() {
        System.exit(0);
    }
};
public ExitOn() {
timer.schedule(exitApp, new Date(System.currentTimeMillis()+5*1000));//Exits after 5sec of starting the app
while(true)
    System.out.println("hello");
}

public static void main(String[] args) {
    new ExitOn();
}
}
Emil
  • 13,577
  • 18
  • 69
  • 108
  • I try the code, in the run() function i run a jms client. But when a new task start, it just run a new client, the old client is still there. The number of the consumer client is increasing on Weblogic jms console. Actually i want the java app quit at a specific time. – Pengyi Wang Aug 04 '11 at 02:38
  • @rocwing:What is the code that you put in run() ? I think u simply need to give a System.exit(0) ,you need not start a new jms client in it.I'll write a sample code and post. – Emil Aug 04 '11 at 05:21
1

If we talk about JMS, then the class implementing MessageListener will have a method onMessage, which will be called when any message enters the queue. You can implement this method such that it can check the incoming message and call the quit() method on specific condition.

I think, we don't need the while loop here for constantly checking for quitting your QueueReceive.

Parth
  • 1,281
  • 8
  • 17
  • several days later, the jms client will dead, but the app didn't quit.So sometimes it can't receive the quit message. We want the app quit at a specific time, and it will be started up by another program. – Pengyi Wang Aug 04 '11 at 02:53
  • hmm... so you want the quit control for the program itself, but wakeup by some other... `Quartz` is the library which provides specific time triggers to be executed. That can be used to achieve if you want to go on time. But again, if you need event based mechanism, like `if the queue is empty`, then thats special... – Parth Aug 04 '11 at 03:50
1

Use java.util.Timer (not the one in javax.swing!)

    boolean daemon = true;
    Calendar cal = Calendar.getInstance();
    //cal.set() to whatever time you want
    Timer timer = new Timer(daemon);
    timer.schedule(new TimerTask() {
        public void run() {
            // Your action here
        }
    }, cal.getTime());
pmnt
  • 379
  • 1
  • 6
0

you can use Timer Task as @Emil suggested, this is only useful for simple scenarios like quit after x mins or hours.

if you need more advanced scheduling its best to use Quartz. Using Quartz you can provide the specific date of a month of a year.. basically any possible combination of times which you can imagine can be configured using quartz.

Anantha Sharma
  • 9,920
  • 4
  • 33
  • 35