简体   繁体   English

Spring 带有任务执行器的批处理小任务

[英]Spring Batch Tasklet with Task-Executor

I got a weird issue about spring batch tasklet step with task-executor.我遇到了一个关于 spring 批处理任务执行器步骤的奇怪问题。 The configuration is normal and simple, just a tasklet (no chunk-oriented) as below:配置正常简单,就是一个tasklet(不面向chunk),如下:

<batch:job id="MyJob" restartable="false">
        <batch:step id="MyJob.Step1">
            <batch:tasklet ref="someBean" task-executor="simpleAsyncTaskExecutor" throttle-limit="1"/>
        </batch:step>
</batch:job>

The someBean is an instance implementated Tasklet interface. someBean是一个实例实现的Tasklet接口。 The stange thing is when I launch the job, the execute method called twice:奇怪的是,当我启动作业时,执行方法调用了两次:

@Override
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {

    // some logic and no exception

    return RepeatStatus.FINISHED;
}

actually two threads are created and executed the logic twice.实际上创建了两个线程并执行了两次逻辑。 And if I change task-executor to the normal task-executor="syncTaskExecutor" (org.springframework.core.task.SyncTaskExecutor), only one thread created and execute() invoked once.如果我将 task-executor 更改为普通的task-executor="syncTaskExecutor" (org.springframework.core.task.SyncTaskExecutor),则只会创建一个线程并调用一次 execute()。

Does anyone encountered this case and could give some idea?有没有人遇到过这种情况并可以提供一些想法? I really do not know "who" and "when" created two threads?真不知道“谁”和“什么时候”创建了两个线程? Thanks谢谢

When you specify an asynchronous task-executor , your tasklet will be executed concurrently by multiple threads.当你指定一个异步task-executor时,你的 tasklet 将被多个线程并发执行。 The limit can be set using throttle-limit which defaults to 4. In your case, two threads will execute the tasklet.可以使用默认为 4 的throttle-limit设置限制。在您的情况下,两个线程将执行 tasklet。

On the other hand, the SyncTaskExecutor will run the tasklet in the calling thread, which means only once since there are no other threads created.另一方面, SyncTaskExecutor将在调用线程中运行 tasklet,这意味着只运行一次,因为没有创建其他线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM