It looks like you want to run every 4 hours or so. Can I suggest a bit cleaner implementation?
@Startup
@Singleton
public class MyScheduler {
@PostConstruct
private void runAtStartup() {
// whatever makes sense here
}
@Schedule(hour = "*/4", persistent = false )
private void runSomething() {
// something here
}
}
This sets up an EJB to run at startup (@Startup
), saying that there is only one instance of it (@Singleton
). It then runs the runSomething()
method every 4 hours. You can take a look at the Schedule Javadocs as there are many other ways to run this too. You may or may not need the @PostConstruct
method if it previously only existed to setup the timer.
The persistent
parameter tells Wildfly to remember when the last run was and, if restarted, continue to run on that schedule. If you don't care if your method runs again on restart then leave this false.
My only warning is that this mechanism doesn't handle overlaps. If you use this and a new schedule method is run before the last one finishes (in this example, 4 hours later) it can lead to errors in at least your log and the schedule not running the way you expect it to.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…