wood burning stoves 2.0*
The moose likes EJB and other Java EE Technologies and the fly likes Need to restart timer with different time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Need to restart timer with different time" Watch "Need to restart timer with different time" New topic
Author

Need to restart timer with different time

aanshu gupta
Greenhorn

Joined: Feb 08, 2010
Posts: 23
Hi,
I have a requirement where in we need to create a timer which runs every 5 mins(default). This works fine and our operation runs fine every 5 mins.
The problem is we need to change this time duration based on user choice and this is what creates the problem.
We have tried stopping the timer by timer.cancel() and restarting it with the new passed time interval but this does not work.

Anyone having worked on similar requirements/ knows the workaround?
Please help.
Thanks


Anshu Gupta, Persistent Systems Limited, India
SCJP 5.0, SCWCD 5.0
aanshu gupta
Greenhorn

Joined: Feb 08, 2010
Posts: 23
sorry forgot to mention this but we are using EJB 3.0 and Jboss 4.2

Davide Crudo
Ranch Hand

Joined: Sep 08, 2009
Posts: 62
aanshu gupta wrote:Hi,
I have a requirement where in we need to create a timer which runs every 5 mins(default). This works fine and our operation runs fine every 5 mins.
The problem is we need to change this time duration based on user choice and this is what creates the problem.
We have tried stopping the timer by timer.cancel() and restarting it with the new passed time interval but this does not work.

Anyone having worked on similar requirements/ knows the workaround?
Please help.
Thanks


Remember that timers will survive the server restart...are you sure you didn't schedule multiple timers? It sounds like you cancelled one timer
but probably all other timers are still active.

How do you cancel the timer? do you assign a different name to each one of them?


SCJP, SCBCD
aanshu gupta
Greenhorn

Joined: Feb 08, 2010
Posts: 23
We are creating only one instance of the timer. For this we use
@PoolClass(value = org.jboss.ejb3.StrictMaxPool.class, maxSize = 1)
This ensures we have only one timer object in the object pool.
For canceling timer am using :
for (Object timerObj : this.timerService.getTimers()) {
if (timerObj != null) {
Timer timer = (Timer) timerObj;
timer.cancel();
}
}
This cancels the timer.
What I am doing is on @Timeout method checking for a particular condition and based on that stoping and again restarting the timer with new time interval.
But the timer is not getting restarted. It waits till the next timeout method and then stops if the time interval has changed.
Thanks for your response.
Davide Crudo
Ranch Hand

Joined: Sep 08, 2009
Posts: 62
Hi,

As a general rule, i don't think is a good idea to control the Pool size of the timers. The pool size is automatically managed
by the container.

From your code, i can see you cancel the timer but where do you re-create it?

could you post the complete @Timeout method?

Dave
aanshu gupta
Greenhorn

Joined: Feb 08, 2010
Posts: 23
@Timeout
public void timeout(Timer timer) throws Exception{
//Some code to retrieve a value from DB using JPA
int timeoutInterval ;//Some code to retrieve a value from DB using JPA
if(timeoutInterval != DEFAULT_TIMEOUT_VALUE){
stopTimer();
startTimer(timeoutInterval);
}
else
//Using the existing timeout interval and do the job.
}

public void startTimer(long timeperiod) {

int timerNum = this.timerService.getTimers().size();
if ( timerNum == 1)
return;
else if (timerNum > 1) {
stopTimer(); //stop all the old timers before launch new.
}
Timer timer = timerService.createTimer(new Date(), timeperiod, null);
}

public void stopTimer() {
for (Object timerObj : this.timerService.getTimers()) {
if (timerObj != null) {
Timer timer = (Timer) timerObj;
timer.cancel();
}
}
}

Thanks
Davide Crudo
Ranch Hand

Joined: Sep 08, 2009
Posts: 62
I have the feeling that your timers overlap and autocancel, more specifically:



i would try to change it into:



I think the timer is Scheduled for "now" then it runs and finds another timer running and it will cancel it.

As mentioned earlier, is better you name your timers so that you know which one you are cancelling, otherwise
with your code you are cancelling All timers and it will prevent you from using other timers efficiently. (to name
a timer, just change the 'null' field into a string or something)

I've prepared an example you can try out. This will cancel the current timer and re-create a new one each time:


aanshu gupta
Greenhorn

Joined: Feb 08, 2010
Posts: 23
Thanks a lot Davide.. It solved the problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need to restart timer with different time
 
Similar Threads
Thread Scheduler
new Thread freezes up program
Java EE timer service and periodical jobs
Orphan LOCKS/Lost Clients
Ending a thread from jsp