2012-10-19 58 views
1

我們使用CommandLineJobRunner來執行Spring Batch作業。我們使用 - 下的命令行:SpringBatch CommandLineJobRunner -next using old run.id

java -Dlog4j.configuration=file:./prop/log4j.properties -Dlogfile=logfile_load_data -jar EtlLoadData.jar loaddata_etl_config.xml loaddata_etl_job -next 

我們終於開始融化了一個錯誤:

Job Terminated in error: A job instance already exists and is complete for parameters={run.id=10}. If you want to run this job again, change the parameters. 

org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException: A job instance already exists and is complete for parameters={run.id=10}. If you want to run this job again, change the parameters. 
    at org.springframework.batch.core.repository.support.SimpleJobRepository.createJobExecution(SimpleJobRepository.java:122) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
at java.lang.reflect.Method.invoke(Method.java:597) 
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) 
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
at org.springframework.batch.core.repository.support.AbstractJobRepositoryFactoryBean$1.invoke(AbstractJobRepositoryFactoryBean.java:168) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) 
at $Proxy14.createJobExecution(Unknown Source) 
at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:111) 
at org.springframework.batch.core.launch.support.CommandLineJobRunner.start(CommandLineJobRunner.java:349) 
at org.springframework.batch.core.launch.support.CommandLineJobRunner.main(CommandLineJobRunner.java:574) 

我認爲,如果你用的-next選項運行,這不應該是一個問題。有任何想法嗎?

作爲一個臨時的解決方法,我清理了數據庫中的SpringBatch表,但我不希望這種情況再次發生。

回答

1

不知道下一個選項。

最快的方法是將當前時間戳作爲參數傳遞給您的工作。這樣它將是獨一無二的,不會干擾你的工作。

+0

感謝您的提示。我不確定這是如何工作的。這只是你添加到命令行的東西嗎?您可以在[CommandLineJobRunner文檔](http://static.springsource.org/spring-batch/apidocs/org/springframework/batch/core/launch/support/CommandLineJobRunner.html) – Ruminator

+0

瞭解'-next'選項是你在命令行中添加的東西。正確。你會像通常會傳遞的另一個JobParameter一樣傳遞它。要使用-next命令,您需要配置一個JobParameterIncrementer。 RunIdIncrementer是開箱即用的。 <作業id = 「JOB1」 增量= 「jobParametersIncrementer」> <豆ID = 「jobParametersIncrementer」 類= 「org.springframework.batch.core.launch.support.RunIdIncrementer」/> runIdincremeter允許用戶設置密鑰的名稱也是如此。我讀了一些在哪裏可能存在與現有參數保持一致的問題....所以要小心......否則寫你自己的。 –

+0

我爲增量器創建了一個新的類,它複製了RunIdIncrementer,但也添加了當前的日期/時間。這清理了這個問題。 – Ruminator

0

隨着德 - 下的選擇,你應該設定一個JobParametersIncrementer實施,

在這個例子中我使用 - >新RunIdIncrementer避免消息:作業實例已經存在,並且是完整的參數

@Autowired 
private JobBuilderFactory jobs; 
... 
@Bean(name=JOB_NAME) 
public Job job() {   

    return jobs.get(JOB_NAME) 
       .start(this.login()) 
       .next(this.launchDuke()) 
       .incrementer(new RunIdIncrementer()) 
       .build(); 
}