Is there a way to configure a specific scheduled task in different environments and schedules? E.g. The same scheduled task 'MyTask' is supposed to run in Integration and Production. In Production 'MyTask' has to run every 24h and in Integration 'MyTask' must not run at all. Currently we're focusing on the native Java EE 7 schedule mechanism. Spring, Quartz are additional frameworks/libraries which we don't want to use (if possible).
2 Answers
If you are using Spring try @Scheduled("${cronEx}")
. You can provide for each environment a different configuration defining the cronEx-value. For example you could get the cronEx-value via JNDI. More about it: SO-Q&A and SO-Q&A.
If you need something more sophisticated have a look at the QUARTZ project: http://www.quartz-scheduler.org/ It is a library to schedule jobs.

- 5,055
- 2
- 18
- 34
-
Currently we're focusing on the native java EE 7 schedule mechanism. Spring, Quartz are additional frameworks/libraries which we don't want to use (if possible). – InformatikBabo May 29 '18 at 07:56
There are several ways of creating a scheduled task in Java EE. I think that what fits better for you is using ManagedScheduledExecutorService.
@ApplicationScoped
public class PeriodicTask {
@Resource
ManagedScheduledExecutorService mses;
@Inject
@Config("period")
private int period;
public void startJobs() {
mses.scheduleAtFixedRate(this::task, 0, period, TimeUnit.MINUTES);
}
private void task() {
...
}
...
}
That way you can, for instance, inject the config value period depending on the running environment. If you don't need to schedule a task for an specific environment you can have another configuration parameter to avoid calling scheduleAtFixedRate method.
The only thing pending to do is calling startJobs method.

- 56
- 2
- 5