I want to write a back-ground job (EJB 3.1), which executes every minute. For this I use the following annotation:
@Schedule(minute = \"*/1\", hour = \"*\")
Since Java EE 7 it is possible to use an "EE-aware" ManagedScheduledExecutorService, i.e. in WildFly:
In for example a @Singleton @Startup @LocalBean
, inject the default "managed-scheduled-executor-service" configured in standalone.xml
:
@Resource
private ManagedScheduledExecutorService scheduledExecutorService;
Schedule some task in @PostConstruct
to be executed i.e. every second with fixed delay:
scheduledExecutorService.scheduleWithFixedDelay(this::someMethod, 1, 1, TimeUnit.SECONDS);
scheduleWithFixedDelay:
Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next.[...]
Do not shutdown the scheduler in i.e. @PreDestroy
:
Managed Scheduled Executor Service instances are managed by the application server, thus Java EE applications are forbidden to invoke any lifecycle related method.