首页 文章

Spring Batch:当并行运行作业时,Spring批处理管理和命令行之间的差异

提问于
浏览
0

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

我的工作流程如下:

Job1 -> Job2 -> Job3  
     -> Job4

当我从spring batch admin运行这些作业时,'Job1'可以启动'Job2'和'Job4',然后'Job2'可以启动'Job3' .

此外,'Job1'在'step1'完成后完成 . 然后'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' .

The problem is

虽然'step1'已完成,但'Job1'的状态仍为'Unknown' . 仅在'Job2'和'Job4'完成后,状态才会变为'Completed' .
'Job1'正在等待'Job2'和'Job4'完成 .

但是在 Spring 季批量管理中,'Job1'不会等待'Job2'和'Job4' . “step1”的状态在“step1”结束后立即变为“已完成” .

我不希望'Job1'在命令行中等待'Job2'和'Job4' .
有办法吗?

PS . 我很抱歉,因为长期的问题和感谢您的帮助 .

1 回答

  • 0

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

相关问题