Java EE 6 @Startup and @Schedule never run

I am sure this question has been asked countless times, but I tried everything I read and nothing works. I am sure the fix is ​​probably also very simple.

I have the following singleton that should be executed when the web application is launched, but it is not, and the scheduled task is also not executed.

@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 3.1.2 server.

Any help would be greatly appreciated.

Thanks.

EDIT

The start method now starts, but the schedule method does not start.

+4
source share
3 answers

Adding a different answer as there were some questions about Boreded's own answer.

The reason why setting persistence=false resolved is probably due to the fact that constant timers are not recreated if it already exists when keepstate 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 (along with Roland Tiefenbrunner's answer on the same question) covers the issue pretty well.

+5
source

My problem was that I was using the wrong Singleton class, not javax.inject.Singleton , but javax.ejb.Singleton

+4
source

For anyone with a similar issue, the following fixed issue has been added:

 persistent=false 

So my scheduled annotation is now

 @Schedule(second="*/10", minute="*", hour="*", persistent=false) 
+1
source

All Articles