Spring Batch错误(已存在作业实例)和RunIdIncrementer仅生成一次

时间:2014-03-03 13:31:07

标签: spring-batch

我正在使用Spring Batch& Quartz从数据库表中读取并写入另一个表。数据库是Oracle,它是c3p0

问题是每个作业都必须有一个独特的参数,我试过RunIdIncrementer并尝试了这段代码:

public class JobRerunner implements JobParametersIncrementer {

    @Override
    public JobParameters getNext(JobParameters parameters) {
        System.out.println("got job parameters: " + parameters);
        if (parameters==null || parameters.isEmpty()) {
            return new JobParametersBuilder().addLong("run.id", System.currentTimeMillis()).toJobParameters();
        }
        long currentTime = parameters.getLong("run.id",System.currentTimeMillis()) + 1;
        return new JobParametersBuilder().addLong("run.id",currentTime).toJobParameters();
    }

}

但是我遇到了同样的问题,run.id只生成一次,当第二次运行作业时它根本没有参数,第三次也是(第二次和第三次运行JobParameter = null所以(工作实例已经存在)

工作环境

<batch:job id="readyReqPoolJob" restartable="true">
    <batch:step id="readyReqPoolStep">
        <batch:tasklet>
            <batch:chunk reader="readyReqPoolReader" writer="readyReqPoolWrtiter"
                commit-interval="100" />
        </batch:tasklet>
    </batch:step>
</batch:job>


<!-- ======================================================= -->
<!-- 6) READER -->
<!-- ======================================================= -->
<bean id="readyReqPoolReader"
    class="org.springframework.batch.item.database.JdbcCursorItemReader">
    <property name="dataSource" ref="dataSource" />
    <property name="sql" value="select * from SF_ILA_Ready_Request_Pool" />
    <property name="rowMapper" ref="ReadyReqPoolRowMapper" />
</bean>
<bean id="readyReqPoolWrtiter"
    class="com.housekeepingservice.readyrequestpoolarchive.ReadyReqPoolArchiveWriter" />


<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
    <property name="jobClass"
        value="org.springframework.batch.sample.quartz.JobLauncherDetails" />
    <property name="jobDataAsMap">
        <map>
            <entry key="jobName" value="readyReqPoolJob" />
            <entry key="jobLocator" value-ref="jobRegistry" />
            <entry key="jobLauncher" value-ref="jobLauncher" />
        </map>
    </property>
</bean>

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
        <bean id="cronTrigger"
            class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
            <property name="jobDetail" ref="jobDetail" />
            <property name="cronExpression" value="0 0/5 * * * ?" />
        </bean>
    </property>
</bean>

主要背景:

<import resource="classpath:spring/batch/config/readyReqPoolContext.xml"
<import resource="classpath:spring/batch/config/jdbc.commons.xml" />
    <!-- 1) USE ANNOTATIONS TO CONFIGURE SPRING BEANS -->
    <context:component-scan base-package="com.housekeepingservice" />


    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" />
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>

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

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

    <!-- 3) JOB REPOSITORY  -->

    <bean id="jobRepository" 
        class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
        <property name="transactionManager" ref="transactionManager" />
    </bean>

    <!-- 4) LAUNCH JOBS FROM A REPOSITORY -->
    <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.core.task.SimpleAsyncTaskExecutor" />

    <bean id="jobExplorer"
        class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean">
        <property name="dataSource" ref="dataSource" />
    </bean>
        <bean name="jobParamatersIncrementer" class="org.springframework.batch.core.launch.support.RunIdIncrementer">
    </bean>

Test.java

public class Test {
    public static void main(String[] args) {

        String[] springConfig = { "spring/batch/config/mainContext.xml" };

        ApplicationContext context = new ClassPathXmlApplicationContext(
                springConfig);
        JobRerunner rerun = new JobRerunner();

        JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");

        Job readyRequestPoolJob = (Job) context.getBean("readyReqPoolJob");


        try {


            JobParameters jobParameters = new JobParameters();
             JobExecution execution2 = jobLauncher.run(readyRequestPoolJob, rerun.getNext(jobParameters));

            System.out.println("Exit Status : " + execution2.getStatus());


        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("Done");

    }

}

log(在第一次运行和第二次运行中检查作业incetance参数):

17:00:27,053  INFO SimpleJobLauncher:132 - Job: [FlowJob: [name=readyReqPoolJob]] launched with the following parameters: **[{run.id=1393855226339}]**
17:00:27.085 [Timer-0] DEBUG org.quartz.utils.UpdateChecker - Checking for available updated version of Quartz...
17:00:27,272  INFO SimpleStepHandler:135 - Executing step: [readyReqPoolStep]
17:02:08,791  INFO SimpleJobLauncher:135 - Job: [FlowJob: [name=readyReqPoolJob]] completed with the following parameters: [{run.id=1393855226339}] and the following status: [COMPLETED]
17:10:00.005 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.jobDetail
17:10:00,008  INFO JobLauncherDetails:69 - Quartz trigger firing with Spring Batch jobName=readyReqPoolJob
17:10:00,036  INFO SimpleJobLauncher:132 - Job: [FlowJob: [name=readyReqPoolJob]] launched with the following parameters: **[{}]**
17:10:00,059  INFO SimpleStepHandler:135 - Executing step: [readyReqPoolStep]

3 个答案:

答案 0 :(得分:2)

要通过工作增量计划午餐,你需要做两件事

  1. 将RunIdIncremater附加到您的工作中。
  2. 使用知道使用增量器的启动器。
  3. 我认为您不需要使用现有的实施方案 将RunIdIncremater附加到您的工作中。

    <batch:job id="readyReqPoolJob" incrementer="runIdIncrementer" restartable="true">
    </batch:job>
    <bean id="runIdIncrementer" 
    class="org.springframework.batch.core.launch.support.RunIdIncrementer"/>
    

    使用启动器
    要启动它,您应该使用以下之一:
    选项1:带有-next选项的CommandLineJobRunner,请参阅API

    选项2:用户JobOperator

    <bean id="jobOperator"
            class="org.springframework.batch.core.launch.support.SimpleJobOperator">
            <property name="jobRepository" ref="jobRepository" />
            <property name="jobLauncher" ref="jobLauncher" />
            <property name="jobRegistry" ref="jobRegistry" />
            <property name="jobExplorer" ref="jobExplorer" />
    </bean>
    
    代码中的

    jobOperator.startNextInstance(jobName)
    

    选项3:在Junit中,您可以使用JobLauncherTestUtils。
    请注意,它有自己的ID增量器,并将忽略您使用的那个 另请参阅以下答案SpringBatch: Test a JobExecutionListener

答案 1 :(得分:0)

例如,添加名为'timestamp'的参数,或者 - 如果要使用run.id - 请将Job.jobParametersIncrementer设置为jobParamatersIncrementer bean定义。

答案 2 :(得分:0)

将步骤allowStartIfComplete标记设置为True