gpt4 book ai didi

java - 每次我请求作业启动器创建新作业时如何重新启动批处理作业

转载 作者:行者123 更新时间:2023-12-02 05:18:05 25 4
gpt4 key购买 nike

我已将 Spring Batch 配置为每当通过 API 调用从 UI 发出请求时就会触发作业。我面临的问题是,该工作仅在第一次时工作正常,而对于其他尝试,每当调用电话时,工作都不会按预期方式响应。似乎他们正在尝试恢复,但我想再次重新启动整个执行过程。感谢您提前提供的任何帮助。

主类

@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");
}

}

Controller 类

@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因为它也不是单例。

关于java - 每次我请求作业启动器创建新作业时如何重新启动批处理作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56281903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com