Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Batch job Not ending

Tags:

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

like image 425
Amar Dev Avatar asked Jun 03 '16 18:06

Amar Dev


1 Answers

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.

like image 144
Michael Minella Avatar answered Sep 28 '22 01:09

Michael Minella