简体   繁体   中英

How to restart my batch job everytime i request for job launcher to create a new job

I had configured my spring batch to trigger jobs whenever a request is made from UI through API calls. The problem i'm facing is that the job is working fine only for first time and for other tries whenever calls are made the jobs aren't responding in expected manner. Seems like they are trying to resume but i want to restart the whole execution again. Thanks for any help in advance.

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

Response when first time API request is made

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]

Response at other times

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]

I want the output to be same everytime(like the first time, ie proper execution) whenever a request is made.

The main reason should be the Reader Bean. As you offer data there, however, it is a singleton. So it offer nothing once data has been consumed by you.

As for solution, You can use @Scope("prototype")

or use StepScope as it is also not singleton.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM