gpt4 book ai didi

java - 如何在 Spring Batch 中设置多线程?

转载 作者:IT老高 更新时间:2023-10-28 13:57:08 25 4
gpt4 key购买 nike

我已经成功设置了一个教程 Spring Batch 项目。我真的很想知道是否可以在“ Spring 级别”使其成为多线程。

我想要的基本想法是列出任务或任务步骤,并让它们由独立线程拾取和处理,最好是从限制为“n”个线程的池中提取和处理。

这可能吗?如果是这样,怎么做?有人可以从我目前所处的位置引导我到那个点吗?

我的简单项目来自本教程 here .它基本上具有不同的任务,可以将消息打印到屏幕上。

这是我当前的 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>

我的 appContext 包含作业存储库 bean (SimpleJobRepository)、事务管理器 (ResourceLessTransactionManager) 和作业启动器 (SimpleJobLauncher)。如果需要,我也可以提供此代码,我只是不想让这篇文章陷入大量 XML 的困境。

非常感谢您的帮助!

最佳答案

创建一个拆分,您将能够在不同分支之间使用多线程。使用 TaskExecutor 来定义您的并行策略。

Multi-threaded Step

如果您想使用多线程和 MapJobRepository,请务必使用最新版本的 Spring Batch(在最新版本之前,此 JobRepository 不是线程安全的)。

关于java - 如何在 Spring Batch 中设置多线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2326695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com