英文:
Spring batch: get ExecutionContext in the listener
问题
我是Spring Batch的新手。
我需要计算已读取、已写入和发生错误的元素。
我已经像这样定义了一个步骤:
/*...*/
@Bean
public Step stepMain(StepBuilderFactory stepBuilderFactory) {
return stepBuilderFactory.get("stepMain").<T, T>chunk(this.chuckSize).reader(reader(null, null)).processor(new Processor()).writer(writer()).faultTolerant().skipPolicy(new AlwaysSkipItemSkipPolicy()).listener(new ListenerReader()).listener(new ListenerProcessor()).listener(new ListenerWriter()).listener(new ListenerChunk()).build();
}
/*...*/
例如,一个类似这样的ListenerReader:
@Log4j2
public class ListenerReader implements ItemReadListener<T> {
@Value("#{jobExecution.executionContext}")
private ExecutionContext executionContext;
@Override
public void afterRead(T item) {
Integer read = (Integer) executionContext.get("reportRead");
read++;
executionContext.put("reportRead", read);
}
@Override
public void onReadError(Exception ex) {
Integer error = (Integer) executionContext.get("reportError");
error++;
executionContext.put("reportError", error);
}
}
但在ListenerReader中,我无法访问executionContext字段。
我该如何解决?
英文:
I am new to Spring batch.
I need to count the element read, written and that have gone in error.
I've defined a step like this:
/*...*/
@Bean
public Step stepMain(StepBuilderFactory stepBuilderFactory) {
return stepBuilderFactory.get("stepMain").<T, T> chunk(this.chuckSize).reader(reader(null, null)).processor(new Processor()).writer(writer()).faultTolerant().skipPolicy(new AlwaysSkipItemSkipPolicy()).listener(new ListenerReader()).listener(new ListenerProcessor()).listener(new ListenerWriter()).listener(new ListenerChunk()).build();
}
/*...*/
And, for example, an ListenerReader like this:
@Log4j2
public class ListenerReader implements ItemReadListener<T> {
@Value("#{jobExecution.executionContext}")
private ExecutionContext executionContext;
@Override
public void afterRead(T item) {
Integer read = (Integer) executionContext.get("reportRead");
read++;
executionContext.put("reportRead", read);
}
@Override
public void onReadError(Exception ex) {
Integer error = (Integer) executionContext.get("reportError");
error++;
executionContext.put("reportError", error);
}
}
But in ListenerReader i've no visibility of executionContext field.
How can i solve?
答案1
得分: 3
你可以这样做:
- 定义一个带有 JobScope 的 Bean
- 像往常一样在步骤中使用它
- 通过监听器进行注入
以下是一个示例:
@Bean
@JobScope
public SimpleReaderListener simpleReaderListener() {
return new SimpleReaderListener();
}
@Bean
public Step step1() {
return stepBuilderFactory.get("step1").<SoccerTeam, SoccerTeam> chunk(1)
.reader(simpleReader()).listener(simpleReaderListener()).processor(new SimpleProcessor())
.writer(new SimpleWriter()).build();
}
public class SimpleReaderListener implements ItemReadListener<SoccerTeam> {
@Value("#{jobExecution.executionContext}")
private ExecutionContext executionContext;
@Override
public void afterRead(SoccerTeam soccerTeam) {
}
}
英文:
You can do it like
- Define a Bean with JobScope
- Use it in Step as usual
- Inject it via Listener.
Below is an example
@Bean
@JobScope
public SimpleReaderListener simpleReaderListener() {
return new SimpleReaderListener();
}
@Bean
public Step step1() {
return stepBuilderFactory.get("step1").<SoccerTeam, SoccerTeam> chunk(1)
.reader(simpleReader()).listener(simpleReaderListener()).processor(new SimpleProcessor())
.writer(new SimpleWriter()).build();
}
public class SimpleReaderListener implements ItemReadListener<SoccerTeam> {
@Value("#{jobExecution.executionContext}")
private ExecutionContext executionContext;
@Override
public void afterRead(SoccerTeam soccerTeam) {
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论