I have the following singleton which should be executed when the web application starts but it does not, and the scheduled task does not run either.
@Singleton
@Startup
public class Scheduler {
private static int count = 0;
@PostConstruct
public void onStartup() {
System.out.println("Initialization success.");
}
@Schedule(second="*/10", minute="*", hour="*")
public void execute() {
System.out.println("its running count..."+count);
count++;
}
}
I am using Glassfish server 3.1.2.
EDIT
The startup method is now being executed but the schedule method does not run.
My problem was that I used the wrong Singleton
class, not javax.inject.Singleton
but javax.ejb.Singleton
Adding another answer as there was some questions on Boreded's own answer.
The reason why setting persistence=false
solved the problem is likely due to that persistent timers are not re-created if already existing when keepstate
is set to true.
you should see the following in the log
INFO: keepstate is true and will not create new auto timers during deployment.
I think my answer here (together with Roland Tiefenbrunner's answer on same question) covers the issue somewhat well.
Also watch out for the default values of the annotation. Hour, minute and second default to 0 NOT *, whereas the others default to * , so e.g. @Schedule(minute = "*/20", persistent = false) will only fire at 20 + 40 mins past midnight.
@Target(value=METHOD)
@Retention(value=RUNTIME)
public @interface Schedule {
String dayOfMonth() default "*";
String dayOfWeek() default "*";
String hour() default "0";
String info() default "";
String minute() default "0";
String month() default "*";
boolean persistent() default true;
String second() default "0";
String timezone() default "";
String year() default "*";
}
For anyone else with a similar issue adding the following fixed my problem:
persistent=false
So my schedule annotation is now
@Schedule(second="*/10", minute="*", hour="*", persistent=false)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With