spring任务没有并行运行

hwamh0ep  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(363)

我试图让我的应用程序做的事情并行,但它目前不工作,但我不知道为什么。作为背景信息,我有多个计划任务,即使用threadpooltaskscheduler计划的任务,我定义如下:

@Bean
public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
    ThreadPoolTaskScheduler threadPoolTaskScheduler
            = new ThreadPoolTaskScheduler();
    threadPoolTaskScheduler.setPoolSize(1000);
    threadPoolTaskScheduler.setThreadNamePrefix(
            "GAXThreadPoolTaskScheduler");
    return threadPoolTaskScheduler;
}

我还有一个simpleasynctaskexecutor,它在发布事件(观察者运行此任务)之后执行任务。该事件由另一个任务发布,我称该任务为“task1”。simpleasynctaskexecutor的配置如下:

@Configuration
public class AsynchronousSpringEventsConfig {
    @Bean(name = "applicationEventMulticaster")
    public ApplicationEventMulticaster simpleApplicationEventMulticaster() {
        SimpleApplicationEventMulticaster eventMulticaster =
                new SimpleApplicationEventMulticaster();

        eventMulticaster.setTaskExecutor(new SimpleAsyncTaskExecutor());
        return eventMulticaster;
    }
}

当前行为如下:

:Task1:
                             |
                             |
                             |
                             |
      [condition for one Object is true publish the event]
                             |
                             |
 [condition for another Object is true publish the event second time]
                             |
                             |
                      :finish Task1:
                   :handle first event:
                             |
           [time passes due to it waits on rest response]
                             |
                :finish handle first event:
                  :handle second event:
                             |
           [time passes due to it waits on rest response]
                             |
                :finish handle second event:
                          :Task1:
                             |
                             |
                           .....

但这不是我想要的行为。我想要这样的行为:

:Task1:
         |
         |
         |
         |
 [p. first event]           
         |                        :handle first event:
         |                                  |                   
 [p. second event]                 [wait for response]          :handle second event:
         |                                  |                              |
         |                     :finish handle first event:         [wait for response] 
  :finish Task1:                                                           |
      :Task1:                                                   :finish handle second event:
         |
         |
      .......

把这两个图形看作一个时间轴,越往下看时间就越长。

我现在的问题是

有人能告诉我怎样才能得到想要的行为吗?
附言:如果你需要更多的信息,我很乐意提供给你

编辑

你可以在这里找到日志文件。在日志中,您可以根据时间戳看到任务没有并行运行。日志也是我确定任务没有并行运行的地方。
任务1我开始如下:

private void startTask1(Company company) {
        ThreadPoolTaskScheduler executor = new ThreadPoolTaskScheduler();
        executor.initialize();

        Task1 task1 = new Task1(company, articleSizeService, tradeService, eventPublisher, taskExecutor, optionsService);
            ScheduledFuture scheduledFuture = executor.scheduleWithFixedDelay(task1 , 1000);

    }

调用此方法的类也是由以下对象执行的任务:

@Bean
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor
                = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setMaxPoolSize(100);
        threadPoolTaskExecutor.setThreadNamePrefix(
                "ThreadPoolTaskExecutor");
        return threadPoolTaskExecutor;
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题