In one hand I've a CronScheduler class which is meant to starts once per application an configure a TimerService.
In the other hand I've a heavy task (annotated as @EJB
) which I want to call in the @timeout
of the timer. Note that in the timer, I create a thread which calls p.go()
The code:
@Singleton
@Startup
public class CronScheduler {
@EJB
Processor p;
@Resource
private TimerService timerService;
@PostConstruct
public void init() {
String h = ... // h,m ,s work fine
String m = ...
String s = ...
ScheduleExpression conf = new ScheduleExpression();
conf.hour(h);
conf.minute(m);
conf.second(s);
// I've tried with the second param (TimerConfig) enabled and disabled
timerService.createCalendarTimer(conf, new TimerConfig("msg ", false));
LOG.log(Level.INFO, ">> Ready for: " + conf.toString());
}
@Timeout
public void run() {
LOG.log(Level.INFO, "Calling the process");
Thread t = new Thread() {
@Override
public void run() {
super.run();
p.go();
}
};
t.start();
}
}
The point is, the cron is initialize multiple times. The @PostConstruct
code runs N times. In the logs I see.
Ready for: A-B-C
Ready for: A-B-C
Ready for: A-B-C
The consequences are p.go()
is called multiple times. Is the @singleton
annotation working fine?