【发布时间】: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