我们可以创建同一个 java(spring) 批处理作业的多个实例吗?

发布于 2024-12-14 22:51:50 字数 198 浏览 1 评论 0原文

我正在使用石英来安排春季批量作业。

该作业从文件夹(包含多个文件)读取文件并进行一些处理并将其复制到另一个文件夹。

是否可以创建该作业的多个实例,该实例将并发运行,读取多个文件?

我的问题是:

在 Spring Batch 中,是否可以生成同一作业的多个实例?我正在使用石英时间表?

I am using quartz to schedule a spring batch job.

The job reads a file from a folder( which has multiple files ) does some processing and copies it to another folder.

is it possible to create multiple instances of the job, that will run concurrenty,reading multiple files ?

My question is :

In spring batch, is it possible to spawn multiple instances of the same job? I am using quartz schedular ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

早乙女 2024-12-21 22:51:50

在 Spring Batch 中,可以启动多个作业,前提是您为每个 jobLauncher.run() 调用提供了不同的 JobParameters。如果配置了适当的任务执行器,Spring 配置中的 jobLauncher 将会在单独的线程中生成每个作业:

<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    <property name="jobRepository" ref="jobRepository" />
    <property name="taskExecutor" ref="taskExecutor" />
</bean>

<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"
    p:corePoolSize="5"
    p:maxPoolSize="30" />

In Spring Batch it is possible to start several jobs, provided you have supplied different JobParameters for each jobLauncher.run() call. jobLauncher in your Spring configuration will spawn each job in a separate thread, if it is configured with appropriate task executor:

<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    <property name="jobRepository" ref="jobRepository" />
    <property name="taskExecutor" ref="taskExecutor" />
</bean>

<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"
    p:corePoolSize="5"
    p:maxPoolSize="30" />
看海 2024-12-21 22:51:50

使用 Quartz 可以使用 MethodInvokingJobDetailFactoryBean,例如:

<bean id="myjob"
    class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject">
        <ref bean="someBean" />
    </property>
    <property name="targetMethod" value="someMethod" />
    <!-- The concurrent property is already true by default
    <property name="concurrent" value="true" />
     -->
</bean>

引用 Spring 文档

默认情况下,Quartz 作业是无状态的,导致作业之间可能会互相干扰。如果为同一个 JobDetail 指定两个触发器,则可能会在第一个作业完成之前启动第二个作业。如果 JobDetail 类实现 Stateful 接口,则不会发生这种情况。在第一个作业完成之前,第二个作业不会开始。要使 MethodInvokingJobDetailFactoryBean 生成的作业成为非并发作业,请将并发标志设置为 false。

It is possible with Quartz, using a MethodInvokingJobDetailFactoryBean, for instance:

<bean id="myjob"
    class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject">
        <ref bean="someBean" />
    </property>
    <property name="targetMethod" value="someMethod" />
    <!-- The concurrent property is already true by default
    <property name="concurrent" value="true" />
     -->
</bean>

Citing the Spring documentation

By default, Quartz Jobs are stateless, resulting in the possibility of jobs interfering with each other. If you specify two triggers for the same JobDetail, it might be possible that before the first job has finished, the second one will start. If JobDetail classes implement the Stateful interface, this won't happen. The second job will not start before the first one has finished. To make jobs resulting from the MethodInvokingJobDetailFactoryBean non-concurrent, set the concurrent flag to false.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文