如何在Spring Batch中设置多线程? [英] How to set up multi-threading in Spring Batch?

查看:314
本文介绍了如何在Spring Batch中设置多线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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".

我想要的基本想法是制作一个任务列表或任务步骤,让它们被独立的线程拾起并处理,最好是在池外限制为'n'个线程数。

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.

这可能吗?如果是这样,怎么样?有人可以从我目前的位置向我指示这一点吗?

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.

这是我当前的simpleJob.xml文件,其中包含作业详细信息:

Here's my current simpleJob.xml file, which contains the job details:

<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来破坏这篇文章。

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.

非常感谢你的帮助! / p>

Thanks very very much for any help!

推荐答案

创建一个Split,你就可以在不同的分支之间使用多线程。
使用TaskExecutor定义并行策略。

Create a Split and you will be able to use multithreading between the different branches. Use a TaskExecutor to define your parallelism policy.

参见多线程步骤

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

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).

这篇关于如何在Spring Batch中设置多线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆