2012-10-26 46 views
1

我已經定義了一次運行的Spring Batch作業。但由於JOB_INSTANCE_ID的主鍵中存在重複項,因此無法再次運行;如何重新運行Spring批處理作業?

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> 

理想情況下,我想要的是作業的每次運行與運行日期/時間被持久化。有沒有辦法配置啓動器自動執行此操作?

回答

7

我也把這個問題放在官方的Spring論壇上。我最終找到了自己的答案(一路上有提示);

http://forum.springsource.org/showthread.php?131347-How-to-re-run-a-job&p=428484#post428484

總之,問題是,我會從這些三個表中刪除的值「0」;

  • BATCH_STEP_EXECUTION_SEQ
  • BATCH_JOB_EXECUTION_SEQ
  • BATCH_JOB_SEQ

所以清理出來的時候,Spring Batch的元數據表(例如,使用截斷或刪除...)請確保以下命令之後運行;

INSERT INTO BATCH_STEP_EXECUTION_SEQ values(0); 
INSERT INTO BATCH_JOB_EXECUTION_SEQ values(0); 
INSERT INTO BATCH_JOB_SEQ values(0); 
相關問題