javascript
batch spring 重复执行_重复的Spring Batch作业实例
我有一個小的示例Spring Batch應用程序,該應用程序在首次啟動時可以正常運行,但是每當我關閉該應用程序并重新啟動jar時,我總是會收到此錯誤:
Caused by: org.springframework.dao.DuplicateKeyException: PreparedStatementCallback; SQL [INSERT into BATCH_JOB_INSTANCE(JOB_INSTANCE_ID, JOB_NAME, JOB_KEY, VERSION) values (?, ?, ?, ?)]; Duplicate entry '1' for key 1; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 1
我不確定作業增量器設置是否錯誤。但是就像我說的那樣,我可以啟動它,然后使用Web服務url
/jobLauncher.html任意次調用批處理。只有在我關閉應用程序并重新啟動它之后,我才收到此錯誤。它想為作業執行表使用ID
1,但以前的運行中已經有ID 1。
主班
@EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, new String[]{ "date=" + System.currentTimeMillis() });
}
}
Web服務類
@Controller
public class JobLauncherController {
@Autowired
JobLauncher jobLauncher;
@Autowired
Job job;
@RequestMapping("/jobLauncher.html")
@ResponseBody
public String handle() throws Exception{
jobLauncher.run(job, new JobParametersBuilder().addString("date", System.currentTimeMillis() + "").toJobParameters());
return "Started the batch...";
}
}
Spring批處理類
@Configuration
@EnableBatchProcessing
public class SampleBatchApplication {
@Autowired
private JobBuilderFactory jobs;
@Autowired
private StepBuilderFactory steps;
@Bean
protected Tasklet tasklet() {
return new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution,
ChunkContext context) {
return RepeatStatus.FINISHED;
}
};
}
@Bean
public Job job() throws Exception {
return this.jobs.get("job")
.incrementer(new RunIdIncrementer())
.flow(this.step1())
.end()
.build();
}
@Bean
protected Step step1() throws Exception {
return this.steps.get("step1").tasklet(this.tasklet()).build();
}
@Bean
public DataSource dataSource() {
BasicDataSource ds = new BasicDataSource();
try {
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUsername("test");
ds.setPassword("test");
ds.setUrl("jdbc:mysql://127.0.0.1:3306/spring-batch");
} catch (Exception e) {
e.printStackTrace();
}
return ds;
}
}
總結
以上是生活随笔為你收集整理的batch spring 重复执行_重复的Spring Batch作业实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Nginx常见面试题整理---40题
- 下一篇: ajax中datatype是json,d