Reputation: 3819
Here is my problem, I am sending some message to a queue using JMS. The program has been written in such a way that there will be a message sent to the queue within 30 seconds from the time of the previous message sent. If there are no messages sent in the 30 seconds duration then the message is the last message and I should start consuming the messages from the queue.
My Initial thought is to create a timer with a task(Here receiving message form the queue), when a new message is created, the method is called and the task waits for 30sec. If the method is called again that means another message has come so the task has to be rescheduled.
This is the code I have written:
public void startTimer() {
Timer t = new Timer();
try {
t.schedule(task, timeDelay);
} catch (Exception e) {
t.cancel();
t = new Timer();
t.schedule(task, timeDelay);
}
}
I am trying to schedule a timer, if there is an existing task scheduled to it then I am canceling that timer, creating a new timer and scheduling a new task.
I am getting error message as Task already scheduled or cancelled
.
Any idea for improvement or suggestions or solutions are most welcome.
Upvotes: 7
Views: 9591
Reputation: 1474
Reuse your Timer, and recreate your TimerTasks after cancelling them. Store tasks so that they can be cancelled later.
Upvotes: 1
Reputation: 2063
Are you sure that Timers are the way to go? It sounds like you should listen for messages with a maximum wait time. Something like this (pseudo-code);
boolean timeout = false;
while (!timeout) {
// Read a message within 30 seconds
try {
Message msg = consumer.receive( 30000 );
} catch (JMSException jmse) {
timeout=true;
}
// Process the message here
process(msg);
}
// Timed-out while waiting for a message, so process messages in the queue
processQueue();
Put this in a function and call it repeatedly. Wrap it inside of a Thread or Runnable if you need multi-threaded capabilities.
Upvotes: 2
Reputation: 53839
You cannot use the same TimerTask
in one or several Timer
.
You need to create a new instance of the TimerTask
to be executed:
t.cancel();
t = new Timer();
TimerTask newTask = new MyTimerTask(); // new instance
t.schedule(newTask, timeDelay);
Upvotes: 10