简体   繁体   English

是否可以只执行一次而不是在所有线程池中执行计划任务?

[英]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.

相关问题 一次仅具有一个活动线程的线程池,并在一个任务中产生让另一个线程运行的任务 - Thread pool with only one active thread at a time and yielding inside a task to let another thread run 带超时的计划线程池-可能的方法 - Scheduled Thread Pool with timeout - possible approaches 单线程池与每个任务的一个线程池 - Single thread pool vs one thread pool per task 当任务通过执行器服务提交给线程池中的线程时,如何保证一次只能有1个线程占用一个同步任务? - When task is submitted to threads in a thread pool via executor service how to ensure that only 1 thread can occupy a synchronized task at a time? 一次只执行一个线程,如何同时执行多个任务? - At a time only one thread is executed, how to execute multiple tasks simultaneously? 具有一个计划任务(可运行)的ScheduledThreadPoolExecutor始终保持向后报告作业计数器+ 1 - ScheduledThreadPoolExecutor with one scheduled task(Runnable) keeps reporting back job counter + 1 all the time 是否可以执行比线程池中的线程更多的请求? - Is it possible to execute more requests than there are threads in thread pool? 线程池任务执行器 - Thread Pool Task Executor 如何使用invokeAll()让所有线程池完成他们的任务? - How to use invokeAll() to let all thread pool do their task? 如何限制任务使用线程池中的所有线程? - How to limit a task from using all threads in a thread pool?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM