Can we create multiple instances of a same java(spring) batch job?

拈花ヽ惹草 提交于 2019-12-11 10:12:43

问题


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 ?


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/8092790/can-we-create-multiple-instances-of-a-same-javaspring-batch-job

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!