【问题标题】:Spring Batch job Not endingSpring Batch作业未结束
【发布时间】:2016-10-03 22:29:37
【问题描述】:

我刚开始使用 Spring Batch,遇到了这个问题。我的工作永远不会结束,它处于无限循环中。下面是代码:-

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

}

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

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

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

谢谢,

阿马尔

【问题讨论】:

    标签: java spring spring-boot spring-batch


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-09
      • 2014-12-24
      • 1970-01-01
      • 1970-01-01
      • 2019-06-10
      • 1970-01-01
      • 1970-01-01
      • 2015-11-25
      相关资源
      最近更新 更多