Java 如何在 Spring Batch 中设置多线程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2326695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set up multi-threading in Spring Batch?
提问by Cuga
I've successfully set up a tutorial Spring Batch project. I'd really like to know if it's possible to make it multi-threaded at the "Spring level".
我已经成功建立了一个教程 Spring Batch 项目。我真的很想知道是否有可能在“Spring 级别”使其成为多线程。
The basic idea of what I want is to make a list of tasks or task steps and let them be picked up and worked on by independent threads, ideally out of a pool limited to 'n' number of threads.
我想要什么的基本思想是制作任务或任务步骤的列表,并让它们由独立线程拾取和处理,理想情况下是在限制为“n”个线程的池中。
Is this possible? If so, how? Could someone show guide me to that point from where I'm currently at?
这可能吗?如果是这样,如何?有人可以从我目前所在的位置引导我到那个点吗?
The simple project I have is from this tutorial here. It basically has different tasks which print out a message to the screen.
我拥有的简单项目来自本教程here。它基本上有不同的任务,可以将消息打印到屏幕上。
Here's my current simpleJob.xml file, which contains the job details:
这是我当前的 simpleJob.xml 文件,其中包含作业详细信息:
<import resource="applicationContext.xml"/>
<bean id="hello" class="helloworld.PrintTasklet">
<property name="message" value="Hello"/>
</bean>
<bean id="space" class="helloworld.PrintTasklet">
<property name="message" value=" "/>
</bean>
<bean id="world" class="helloworld.PrintTasklet">
<property name="message" value="World!\n"/>
</bean>
<bean id="taskletStep" class="org.springframework.batch.core.step.tasklet.TaskletStep" >
<property name="jobRepository" ref="jobRepository"/>
<property name="transactionManager" ref="transactionManager"/>
</bean>
<bean id="simpleJob" class="org.springframework.batch.core.job.SimpleJob">
<property name="name" value="simpleJob" />
<property name="steps">
<list>
<bean parent="taskletStep">
<property name="tasklet" ref="hello"/>
</bean>
<bean parent="taskletStep">
<property name="tasklet" ref="space"/>
</bean>
<bean parent="taskletStep">
<property name="tasklet" ref="world"/>
</bean>
</list>
</property>
<property name="jobRepository" ref="jobRepository"/>
</bean>
My appContext contains the job repository bean (SimpleJobRepository
), transaction manager (ResourceLessTransactionManager
) and job launcher (SimpleJobLauncher
). I can provide this code if desired as well, I just didn't want to bog down this post with tons of XML.
我的 appContext 包含作业存储库 bean ( SimpleJobRepository
)、事务管理器 ( ResourceLessTransactionManager
) 和作业启动器 ( SimpleJobLauncher
)。如果需要,我也可以提供此代码,我只是不想用大量 XML 使这篇文章陷入困境。
Thanks very very much for any help!
非常感谢您的帮助!
采纳答案by Jean-Philippe Briend
Create a Split and you will be able to use multithreading between the different branches. Use a TaskExecutor to define your parallelism policy.
创建一个拆分,您将能够在不同的分支之间使用多线程。使用 TaskExecutor 来定义并行策略。
请参阅多线程步骤
Be sure to use the latest version of Spring Batch if you want to use multithreading and the MapJobRepository (prior to latest version, this JobRepository was not thread safe).
如果要使用多线程和 MapJobRepository(在最新版本之前,此 JobRepository 不是线程安全的),请务必使用最新版本的 Spring Batch。
回答by HenioJR
Take a look at Jean answers'. An easy way is create a bean of SimpleAsyncTaskExecutor and associate a tasklet to use this bean, like that.
看看Jean的回答。一个简单的方法是创建一个 SimpleAsyncTaskExecutor 的 bean 并关联一个 tasklet 来使用这个 bean,就像这样。
<bean id="simpleTaskExecutor"
class="org.springframework.core.task.SimpleAsyncTaskExecutor">
<property name="concurrencyLimit" value="10"/>
</bean>
<batch:job id="jobTest">
<batch:step id="step1">
<!-- throttle-limit default is 4. Increase this to ensure that a thread pool is fully utilized -->
<batch:tasklet task-executor="simpleTaskExecutor" throttle-limit="20">
<batch:chunk />
</batch:tasklet>
</batch:step>
</batch:job>