如何重新运行Spring批处理作业?

如何重新运行Spring批处理作业?,spring,spring-batch,Spring,Spring Batch,我已经定义了一个Spring批处理作业,它可以正常运行一次。但是,由于JOB_INSTANCE_ID的主键中存在重复条目,因此无法再次运行 SEVERE: Job Terminated in error: PreparedStatementCallback; SQL [INSERT into BATCH_JOB_INSTANCE(JOB_INSTANCE_ID, JOB_NAME, JOB_KEY, VERSION) values (?, ?, ?, ?)]; Duplicate entr

我已经定义了一个Spring批处理作业,它可以正常运行一次。但是,由于JOB_INSTANCE_ID的主键中存在重复条目,因此无法再次运行

SEVERE: Job Terminated in error: PreparedStatementCallback; SQL [INSERT into    BATCH_JOB_INSTANCE(JOB_INSTANCE_ID, JOB_NAME, JOB_KEY, VERSION) values (?, ?, ?, ?)]; Duplicate entry '0' for key 'PRIMARY'; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '0' for key 'PRIMARY'
我已经读到,通过在作业中设置命令行参数可以解决这个问题,例如

java -cp ${CLASSPATH} org.springframework.batch.core.launch.support.CommandLineJobRunner myJob.xml myJob date=20121025154016 -next
无论是否包含-next参数,它都不会运行作业

我的Spring批处理配置如下所示:Batch/launchContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:batch="http://www.springframework.org/schema/batch"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jdbc="http://springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc     http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
    http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd
http://springframework.org/schema/jdbc http://springframework.org/schema/jdbc/spring-jdbc.xsd
    http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- Launch context: how to launch all of the jobs -->
    <!-- Expects a data source configuration file to be used, with a DataSource bean 
called "dataSource" -->

    <!-- The data source for the jobs status -->
    <import resource="dataSource.xml" />

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

    <bean
            id="incrementer"
            class="org.springframework.batch.core.launch.support.RunIdIncrementer" />

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

    <bean
            id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property
                    name="dataSource"
                    ref="dataSource" />
    </bean>

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

</beans>
在myJob.xml文件中,作业是这样的

....
<import resource="batch/launchContext.xml" />
...
    <batch:job
            id="myJob"
            job-repository="jobRepository"
            incrementer="incrementer">
            <batch:step id="step1">
                    <batch:tasklet transaction-manager="transactionManager">
                            <batch:chunk
                                    reader="myReader"
                                    processor="myProcessor"
                                    writer="myWriter"
                                    commit-interval="10" />
                    </batch:tasklet>
            </batch:step>
    </batch:job>

理想情况下,我希望作业的每次运行都与运行日期/时间保持一致。有没有办法将启动器配置为自动执行此操作?

我也在官方的Spring论坛上提出了这个问题。我最终找到了自己的答案,并在这一过程中给出了一些提示

简而言之,问题是我从这三个表中删除了值“0”

批处理步骤执行顺序 批处理作业执行顺序 批量作业 所以当清理Spring批处理元数据表时,例如使用truncate或delete from。。。确保随后运行以下命令

INSERT INTO BATCH_STEP_EXECUTION_SEQ values(0);
INSERT INTO BATCH_JOB_EXECUTION_SEQ values(0);
INSERT INTO BATCH_JOB_SEQ values(0);

我还把这个问题放到了春季官方论坛上。我最终找到了自己的答案,并在这一过程中给出了一些提示

简而言之,问题是我从这三个表中删除了值“0”

批处理步骤执行顺序 批处理作业执行顺序 批量作业 所以当清理Spring批处理元数据表时,例如使用truncate或delete from。。。确保随后运行以下命令

INSERT INTO BATCH_STEP_EXECUTION_SEQ values(0);
INSERT INTO BATCH_JOB_EXECUTION_SEQ values(0);
INSERT INTO BATCH_JOB_SEQ values(0);