1

I'm studying the EJB and testing automatic function by create a simple EJB like below.
I hope it can run automatically once it's deployed.

import javax.ejb.Schedule;
import javax.ejb.Stateless;
@Stateless
public class TestAuto {

@Schedule(minute="*",hour="15")
    public void testprint()
    {
        System.out.println("AutoWrite");
    }
}

I run it at eclipse and it successfully deployed but I can't see any output at console. Can someone help? I used wildfly 11, java 1.8 and ejb 3.2. Thanks. Update: Now it worked. But how to pause it and restart?

kk luo
  • 549
  • 1
  • 9
  • 22
  • 2
    Your EJB will print something at 03:00 PM o' clock. It is right? Let's try with something different, for example: `second = "*/10"` that means every 10 seconds – Renato Jan 09 '20 at 08:34

1 Answers1

0

There is not such thing as a "pause" and "restart", but you can cancel an annotation based timer declared with a @Schedule, and recreate it. You will have to store the schedulerexpression somewhere.

You can do something like this:

@Resource
private TimerService timerService;

public ScheduleExpression cancelTimer() {
    Timer currentTimer = timerService.getTimers().iterator().next();
    ScheduleExpression scheduleExpression = currentTimer.getSchedule();
    currentTimer.cancel();
    return scheduleExpression;
}

public void restartTimer(ScheduleExpression scheduleExpression) {
    timerService.createCalendarTimer(scheduleExpression);
}
areus
  • 2,880
  • 2
  • 7
  • 17