繁体   English   中英

Spring Batch 作业未结束

[英]Spring Batch job Not ending

我刚刚开始使用 Spring 批处理并陷入了这个问题。 我的工作永无止境,无限循环。 以下是代码:-

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

}

这是我的读者班

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

这是我的作家课

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

}

这是我的处理器类

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());
}

}

有没有我在这里缺少的配置? 还是我的代码有问题?

我已经用谷歌搜索了一段时间,但找不到任何建设性的东西。

非常感谢这里的任何帮助。

谢谢,

阿马尔

你的读者永远不会返回 null。 Spring Batch 中ItemReader的契约是读取,直到读取器返回 null(表示输入已用完)。 由于您从不从ItemReader返回 null ...您的工作将永远阅读。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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