简体   繁体   中英

Spring Batch job Not ending

I have just started to use Spring batch and got stuck at this problem. My job never ends, its in an infinite loop. Below is the code:-

@SpringBootApplication
@EnableBatchProcessing
public class Main implements CommandLineRunner{

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private JobLauncher jobLauncher;

    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }
    @Override
    public void run(String... args) throws Exception {
        jobLauncher.run(flattenPersonJob(),new JobParameters());
        System.out.println("Done");

    }

    @Bean
    public ItemReader itemReader() {
        return new PersonReader();
    }

    @Bean
    public ItemProcessor itemProcessor() {
        return new PersonProcessor();
    }

    @Bean
    public ItemWriter itemWriter() {
        return new PersonWriter();
    }

    @Bean
    public Step flattenPersonStep() {
        return stepBuilderFactory.get("flattenPersonStep").
                chunk(1).
                reader(itemReader()).
                processor(itemProcessor()).
                writer(itemWriter()).
                build();
    }

    @Bean
    public JobListener jobListener() {
        return new JobListener();
    }

    @Bean
    public Job flattenPersonJob() {
        return jobBuilderFactory.get("flattenPersonJob").
                incrementer(new RunIdIncrementer()).
                listener(jobListener()).
                flow(flattenPersonStep()).
                end().
                build();
    }

}

This is my reader class

public class PersonReader implements ItemReader<List<Person>> {

    @Override
    public List<Person> read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
        System.out.println("This is the reader");
        List<Person> personList = new ArrayList<>();
        personList.add(new Person("","",""));
        Thread.sleep(5000);
        return personList;
    }
}

This is my writer class

public class PersonWriter implements ItemWriter<List<String>> {
@Override
public void write(List<? extends List<String>> items) throws Exception {
    System.out.println("This is the writer");
    //Thread.sleep(5000);
    items.forEach(System.out::println);
}

}

This is my processor class

public class PersonProcessor implements ItemProcessor<List<Person>, List<String>> {
@Override
public List<String> process(List<Person> item) throws Exception {
    System.out.println("This is the processor");
    //Thread.sleep(5000);
    return item.stream().map(n -> n.getName()).collect(Collectors.toList());
}

}

Is there any configuration that I am missing here ?? Or is there something wrong with my code ?

I have googled for some time now, but couldnot find anything constructive.

Any help here is much appreciated.

Thanks,

Amar

Your reader never returns null. The contract for the ItemReader within Spring Batch is to read until the reader returns null (indicating that the input has been exhausted). Since you never return null from your ItemReader ...your job will read for ever.

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