Reputation: 3328
I'm using Spring and I have a scheduled task that makes some operations on the database. I figured out this task is executed on each pool instead I would like to have only one execution of it. For example in my log file I read:
2016-08-04 01:01:01 [pool-2-thread-1] INFO c.w.c.FleetAndCarControllerImpl - SCHEDULED ACTIVITY TO DELETE OLD NOTIFICATIONS
2016-08-04 01:01:01 [pool-3-thread-1] INFO c.w.c.FleetAndCarControllerImpl - SCHEDULED ACTIVITY TO DELETE OLD NOTIFICATIONS
I have this configuration:
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(100);
executor.setQueueCapacity(100);
executor.initialize();
return executor;
}
and this is the task:
//This task is executed every day at 01:01 a.m.
@Scheduled(cron = "0 1 1 * * ?")
public void smartQuery(){
try {
LOG.info("SCHEDULED ACTIVITY TO DELETE OLD NOTIFICATIONS");
notificationManagementServices.deleteOldNotifications();
} catch (QueryException e) {
ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e);
LOG.error("Threw exception in WakeUpDatabase::smartQuery :" + errorResponse.getStacktrace());
}
}
Is it possible?thanks
Upvotes: 2
Views: 1331
Reputation: 41
I know it's late, but I've faced the same problem and for me the solution was to annotate my Scheduler class with @Singleton, then Scheduler just runned on time in all those clusters and instances.
As seemed at https://docs.oracle.com/cd/E19226-01/820-7627/6nisfjmnv/index.html
Upvotes: 1
Reputation: 3883
Do you mean this:
You want to have multiple threads, and some task only gets executed in one thread, or each task runs in it's own thread.
If so, the most easy way is to create several TaskSchedulers, with pool size is 1. And you can trigger the scheduled job in java with TaskScheduler.schedule(...)
. If you have several schedulers, you need to inject different schedulers with qualified name.
Upvotes: 0