[英]Is it possible to execute scheduled task only one time and not in all thread pool?
I'm using Spring and I have a scheduled task that makes some operations on the database.我正在使用 Spring,并且我有一个计划任务,可以对数据库进行一些操作。 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有可能吗?谢谢
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.我知道已经很晚了,但我遇到了同样的问题,对我来说,解决方案是用@Singleton注释我的调度程序类,然后调度程序在所有这些集群和实例中按时运行。
As seemed at https://docs.oracle.com/cd/E19226-01/820-7627/6nisfjmnv/index.html正如https://docs.oracle.com/cd/E19226-01/820-7627/6nisfjmnv/index.html
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(...)
.如果是这样,最简单的方法是创建多个 TaskScheduler,池大小为 1。您可以使用
TaskScheduler.schedule(...)
在 java 中触发计划作业。 If you have several schedulers, you need to inject different schedulers with qualified name.如果您有多个调度程序,则需要注入具有限定名称的不同调度程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.