Viktor Carlson
Viktor Carlson

Reputation: 1008

Cancel TimerService started with @Schedule

My Setup: NetBeans 8.0.2 JEE7 with EJB3.2 running on Glassfish 4.2

Hi, I started a timer service

@Schedule(second = "*/10", minute = "*", hour = "*", persistent = false)
public void chronJobSySODate() {
    System.out.println("Timer event: " + new Date());

}

I called this method in another Bean with the @PostConstruct Annotation

@PostConstruct
public void startChronJob(){
    chronJobBean.chronJobSySODate();
}

Now despite that i though because of persistent = false the timer would be gone after killing Glassfish, it is still there even in other NetBeans Project running on the same Glassfish Server.

I already looked on this StackOverflow Thread Is there a way to stop/re-start ejb 3.1 automatic timer during runtime?

So I tried

import javax.annotation.Resource;
import javax.ejb.TimerService;

    @Resource
    TimerService timerService;

    public void cancelAllTimers() {

        System.out.println("cancelAllTimers");

        for (Timer timer : timerService.getTimers()) {

            System.out.println("Canceling Timer: info: " + timer.getInfo());
            timer.cancel();

        }
    }

But regarding to the timerService variable there are no timers ( but the timer is till running.

Upvotes: 0

Views: 744

Answers (1)

Dominik Schlosser
Dominik Schlosser

Reputation: 11

I do not know if this solves your problem but since i am not yet allowed to comment (<50 rep) i post this as an answer:

You should omit the call to your timer-method in PostConstruct. Methods annotated with @Schedule are executed by the container without any further registration. Since you did not post the rest of your "caller"-class i have to guess: If it is an EJB marked as @Startup or used otherwise your timer method would be called even if there is no active timer.

Upvotes: 1

Related Questions