【发布时间】:2020-08-10 02:09:30
【问题描述】:
我已经编写了一个 spring 批处理应用程序,并且项目阅读器正在抛出异常。
如何重试项目阅读器?
我已经添加了
@EnableRetry 在应用程序类和下面是阅读器代码
@Bean
@Retryable(include = { RuntimeException.class }, maxAttempts = 1000, backoff = @Backoff(delay = 0))
public ItemReader<Student> reader() {
return new InMemoryStudentReader();
}
下面是阅读器类
public class InMemoryStudentReader implements ItemReader<Student> {
@Autowired
private JdbcTemplate jdbcTemplate;
private int nextStudentIndex;
private List<Student> studentData;
public InMemoryStudentReader() {
initialize();
}
private void initialize() {
Student s1 = new Student(1, "ABC");
Student s2 = new Student(2, "DEF");
Student s3 = new Student(3, "GHI");
studentData = Collections.unmodifiableList(Arrays.asList(s1, s2,s3));
nextStudentIndex = 0;
}
@Override
public Student read() throws Exception {
Student nextStudent = null;
if (nextStudentIndex < studentData.size()) {
int a =jdbcTemplate.queryForObject("SELECT id FROM val LIMIT 1", Integer.class);
if(a == 2) {
throw new RuntimeException("Exception");
}
nextStudent = studentData.get(nextStudentIndex);
nextStudentIndex++;
} else {
nextStudentIndex = 0;
}
return nextStudent;
}
}
但即使在此之后,阅读器也不会重试并且作业失败
【问题讨论】:
-
你加
@EnableRetry吗?你确定你因为 RuntimeException 失败了吗? docs -
@WoAiNii 我认为它不起作用是因为stackoverflow.com/questions/38212471/…
-
@MahmoudBenHassine 我知道 faultTolerant 不适用于阅读器,但我在阅读器 bean 上特别指定了 Retryable。然后应该重试
-
我添加了一个答案。
标签: java spring spring-batch batch-processing