Reputation: 599
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?
Upvotes: 1
Views: 340
Reputation: 2947
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);
}
Upvotes: 0