【问题标题】:Spring Batch : Difference between spring batch admin and command line when jobs are run in parallelSpring Batch:并行运行作业时 Spring Batch 管理和命令行之间的区别
【发布时间】:2014-10-15 00:57:57
【问题描述】:

当我使用'Split flow'并行运行spring批处理作业时,我发现spring批处理管理和命令行之间存在差异。

我的工作流程如下:

Job1 -> Job2 -> Job3  
     -> Job4

当我从 Spring Batch Admin 运行这些作业时,“Job1”可以同时启动“Job2”和“Job4”,然后“Job2”可以启动“Job3”。

此外,在“step1”完成后,“Job1”就完成了。然后“Job2”和“Job4”继续并行处理。
“Job1”不等待“Job2”和“Job4”完成。

spring batch admin的app-context.xml和Job配置如下:
Job1.xml

<import resource="classpath*:META-INF/spring/batch/dependencies/parallel2.xml"/>
<import resource="classpath*:META-INF/spring/batch/dependencies/parallel3.xml"/>

<bean id="taskExecutor" class="org.springframework.core.task.SimpleAsyncTaskExecutor"/>

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

<bean id="job1.stp01" class="com.jobs.Job1Step1" />

<batch:job id="Job1" restartable="true" >
    <batch:step id="step1" next="split">
        <batch:tasklet ref="job1.stp01" />
    </batch:step>

    <batch:split id="split" next="step3">
        <batch:flow>
            <batch:step id="flow1" >
                <batch:job ref="Job2" job-launcher="simpleJobLauncher"/>
            </batch:step>
        </batch:flow>

        <batch:flow>
            <batch:step id="flow2">
                <batch:job ref="Job3" job-launcher="simpleJobLauncher"/>
            </batch:step>
        </batch:flow>
    </batch:split>          
</batch:job>

app-context.xml

<batch:job-repository id="jobRepository" />

<task:executor id="jobLauncherTaskExecutor" pool-size="10" rejection-policy="ABORT"/>

<bean id="jobRegistry" class="org.springframework.batch.core.configuration.support.MapJobRegistry" />

<bean class="org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor">
    <property name="jobRegistry" ref="jobRegistry"/>
</bean>

但在命令行中,与 spring batch admin 有一些不同。
app-context.xml 和命令行的作业配置如下:
Job1.xml

<import resource="app-context.xml" />
<import resource="parallel2.xml"/>
<import resource="parallel3.xml"/>

<bean id="job1.stp01" class="com.jobs.Job1Step1" />

<batch:job id="Job1" restartable="true" >
    <batch:step id="step1" next="split">
        <batch:tasklet ref="job1.stp01" />
    </batch:step>

    <batch:split id="split" task-executor="jobLauncherTaskExecutor" next="step3">
        <batch:flow>
            <batch:step id="flow1" >
                <batch:job ref="Job2" job-launcher="simpleJobLauncher"/>
            </batch:step>
        </batch:flow>

        <batch:flow>
            <batch:step id="flow2">
                <batch:job ref="Job3" job-launcher="simpleJobLauncher"/>
            </batch:step>
        </batch:flow>
    </batch:split>      
</batch:job>

app-context.xml

<bean id="springBatchDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    .......
</bean>

<bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

<bean id="jobRepository" class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
    <property name="databaseType" value="POSTGRES" />
    <property name="dataSource" ref="springBatchDataSource" />
    <property name="transactionManager" ref="transactionManager" />
</bean>

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

<bean id="jobLauncherTaskExecutor" class="org.springframework.core.task.SimpleAsyncTaskExecutor" />

当我从命令行运行作业时,“Job1”可以同时启动“Job2”和“Job4”,然后“Job2”可以启动“Job3”。

问题是

虽然 'step1' 已经完成,但 'Job1' 的状态仍然是 'Unknown'。只有在“Job2”和“Job4”完成后,状态才会变为“Completed”。
“Job1”正在等待“Job2”和“Job4”完成。

但在 spring 批处理管理中,'Job1' 不会等待 'Job2' 和 'Job4'。一旦“step1”完成,“Job1”的状态就会变为“Completed”。

我不希望“Job1”在命令行中等待“Job2”和“Job4”。
有没有办法这样做???

附言。很抱歉,问题很长,感谢您的帮助。

【问题讨论】:

    标签: spring spring-batch spring-batch-admin


    【解决方案1】:

    此方案的正确行为是,在这两种情况下,在 Job2 和 Job4 完成之前,不应将 Job1 标记为 COMPLETED。如果在其中一种情况下没有发生这种情况,则这是一个错误,应该登录 Jira (https://jira.spring.io)。当 Job1 标记为 COMPLETE 时,您确定 Spring Batch Admin 中的其他作业未完成吗?

    【讨论】:

    • 我想我找到了原因。这是因为上面的spring batch admin和命令行的配置不一样。在春季批处理管理中,我在simpleJobLauncher 中使用taskExecutor 使'Job2' 和'Job4' 并行运行。但是在命令行中,虽然我在simpleJobLauncher 中使用了taskExecutor,但这些作业不会并行运行。它们仅按顺序执行。所以,我在split flow 中使用taskExecutor 并且作业并行运行,但发生了上述情况(正如我在问题中解释的那样)。
    • (从上面的评论继续。)当我在'split flow`中使用taskExecutor再次测试spring batch admin时,它与命令行相同。 Job1 等待其他作业完成。所以,我想我问错了,我很抱歉。实际上,我应该问的问题是如何使作业在命令行中同时运行。有没有办法在命令行中并行运行作业??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-05
    • 2012-10-09
    • 1970-01-01
    相关资源
    最近更新 更多