java - 每次我请求作业启动器创建新作业时如何重新启动我的批处理作业
问题描述
每当通过 API 调用从 UI 发出请求时,我已将我的 spring 批处理配置为触发作业。我面临的问题是,该工作仅在第一次工作正常,而对于其他尝试,只要拨打电话,工作没有以预期的方式响应。似乎他们正在尝试恢复,但我想再次重新启动整个执行。感谢您提前提供任何帮助。
主类
@SpringBootApplication
@EnableBatchProcessing
public class HelloWorldApplication
{
public static void main(String[] args)
{
SpringApplication.run(HelloWorldApplication.class, args);
}
}
配置类
@Configuration
public class ListenerJobConfiguration
{
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public ItemReader<String> reader()
{
return new ListItemReader<>
(Arrays.asList("one","two","three"));
}
@Bean
public ItemWriter<String> writer()
{
return new ItemWriter<String>()
{
@Override
public void write(List<? extends String> items) throws Exception
{
for(String item:items)
{
System.out.println("writing items "+item);
}
}
};
}
@Bean
public Step step1()
{
return stepBuilderFactory.get("step1")
.<String,String>chunk(2)
.faultTolerant()
.listener(new ChunkListener())
.reader(reader())
.writer(writer())
.build();
}
@Bean
public Job listenerJob()
{
return jobBuilderFactory.get("listenerJob"+new Date())
.start(step1())
.listener(new JobListener())
.build();
}
}
JobListener.class
public class JobListener implements JobExecutionListener
{
@Override
public void beforeJob(JobExecution jobExecution)
{
System.out.println("Before job");
}
@Override
public void afterJob(JobExecution jobExecution)
{
System.out.println("After job");
}
}
ChunkListener.class
public class ChunkListener
{
@BeforeChunk
public void beforeChunk(ChunkContext context)
{
System.out.println(">> Before the chunk");
}
@AfterChunk
public void afterChunk(ChunkContext context)
{
System.out.println("<< After the chunk");
}
}
控制器类
@RestController
public class BatchController
{
@Autowired
JobLauncher jobLauncher;
@Autowired
Job job;
@RequestMapping("/jobLauncher")
public void handle() throws Exception
{
JobParametersBuilder builder = new JobParametersBuilder();
builder.addDate("date", new Date());
jobLauncher.run(job, builder.toJobParameters());
}
@GetMapping(value = "/test")
public String test()
{
return "test success";
}
}
应用程序属性
spring.batch.job.enabled=false
第一次 API 请求时的响应
2019-05-24 01:03:53.578 INFO 5264 --- [nio-9999-exec-1] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=listenerJobFri May 24 01:03:42 IST 2019]] launched with the following parameters: [{date=1558640033401}]
Before job
2019-05-24 01:03:53.640 INFO 5264 --- [nio-9999-exec-1] o.s.batch.core.job.SimpleStepHandler : Executing step: [step1]
>> Before the chunk
writing items one
writing items two
<< After the chunk
>> Before the chunk
writing items three
<< After the chunk
After job
2019-05-24 01:03:53.722 INFO 5264 --- [nio-9999-exec-1] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=listenerJobFri May 24 01:03:42 IST 2019]] completed with the following parameters: [{date=1558640033401}] and the following status: [COMPLETED]
其他时间回复
2019-05-24 01:05:02.072 INFO 5264 --- [nio-9999-exec-4] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=listenerJobFri May 24 01:03:42 IST 2019]] launched with the following parameters: [{date=1558640102047}]
Before job
2019-05-24 01:05:02.107 INFO 5264 --- [nio-9999-exec-4] o.s.batch.core.job.SimpleStepHandler : Executing step: [step1]
>> Before the chunk
<< After the chunk
After job
2019-05-24 01:05:02.150 INFO 5264 --- [nio-9999-exec-4] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=listenerJobFri May 24 01:03:42 IST 2019]] completed with the following parameters: [{date=1558640102047}] and the following status: [COMPLETED]
每当发出请求时,我希望每次输出都相同(就像第一次,即正确执行)。
解决方案
主要原因应该是Reader Bean。但是,当您在那里提供数据时,它是一个单例。因此,一旦您使用了数据,它就什么也不提供了。
至于解决方案,您可以使用 @Scope("prototype")
或使用StepScope,因为它也不是单例的。
推荐阅读
- azure-devops - 不同 Git 存储库中的源代码和 YAML 文件
- flutter - 颤振升级后如何修复“发现重复类”错误?
- intellij-idea - 重置 Intellij / DataGrip 2020.1 字体更改
- php - 使用 Ajax 添加和删除 SQL
- postgresql - org.hibernate.type.SerializationException:无法反序列化错误,使用 CriteriaBuilder API 从 postgreSQL 读取 json 字段
- ios - 作为开发人员需要在 iOS13 SDK 的代码库中进行哪些更改
- ios - 是否可以多次触发推送通知的 requestAuthorization 系统对话框
- http - 如何在golang中为请求和重定向添加值
- image - 根据条件更改图像
- python - 除了启动 Orange 程序之外,如何使用 Python 命令 os.system() 来提供输入文件?