每当我请求作业启动器创建新作业时,如何重新启动批处理作业

时间:2019-05-23 19:36:41

标签: java spring-batch

我已将spring批处理配置为每当通过API调用从UI发出请求时触发作业。我面临的问题是,该工作仅在第一次工作以及每次尝试进行其他尝试时都可以正常工作,这些工作均未以预期的方式响应。好像他们正在尝试恢复,但我想再次重新启动整个执行过程。感谢您的任何提前帮助。

Main.class

@SpringBootApplication
@EnableBatchProcessing
public class HelloWorldApplication 
{
    public static void main(String[] args) 
    {
        SpringApplication.run(HelloWorldApplication.class, args);
    }
}

configuration.class

@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.class

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

}

application.properties

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]

我希望每次发出请求时输出都一样(就像第一次,即正确执行)。

1 个答案:

答案 0 :(得分:0)

主要原因应该是Reader Bean。但是,当您在此处提供数据时,它是一个单例。因此,一旦您消耗了数据,它就什么也不提供。

对于解决方案,您可以使用@Scope(“ prototype”)

或使用StepScope,因为它也不是单例。

相关问题