@组件 公共类 FirstItemWriter {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HHmmss");
String timestamp = dateFormat.format(new Date());
String path = "C:\\Workspace\\AutoDialer\\CmeAutoDialerJson_" + timestamp + ".json";
@StepScope
@Bean
public JsonFileItemWriter<Long> jsonFileItemWriter() {
FileSystemResource fileSystemResource=new FileSystemResource(path);
JsonFileItemWriter<Long> jsonFileItemWriter = new JsonFileItemWriter<>(fileSystemResource,
new JacksonJsonObjectMarshaller<Long>());
return jsonFileItemWriter;
}
}
@服务 公共类 SecondJobScheduler {
@Autowired
JobLauncher jobLauncher;
@Qualifier("secondJob")
@Autowired
Job chunkJob;
@Scheduled(cron = "0 0/1 * 1/1 * ?")
public void secondJobStarter() {
Map<String, JobParameter> params = new HashMap<>();
params.put("currentTime", new JobParameter(System.currentTimeMillis()));
JobParameters jobParameters = new JobParameters(params);
try {
JobExecution jobExecution =
jobLauncher.run(chunkJob, jobParameters);
System.out.println("Job Execution ID = " + jobExecution.getId());
}catch(Exception e) {
System.out.println("Exception while starting job");
}
}
}
我创建了一个 Spring Batch 应用程序,每 2 分钟运行一次,创建一个 json 文件 josn_2024_04_29_222936 但在第二次运行中,它不是创建新的 json,而是编辑旧的 json 文件,如何在第二批运行中创建新的 json 文件。
在
jsonFileItemWriter()
方法中移动下面的变量。
String path = "C:\\Workspace\\AutoDialer\\CmeAutoDialerJson_" + timestamp + ".json";