Spring批处理:在监听器中获取ExecutionContext

huangapple go评论79阅读模式
英文:

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(&quot;stepMain&quot;).&lt;T, T&gt; 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&lt;T&gt; {

	@Value(&quot;#{jobExecution.executionContext}&quot;)
    private ExecutionContext executionContext;

    @Override
    public void afterRead(T item) {
        Integer read = (Integer) executionContext.get(&quot;reportRead&quot;);
		read++;
    	executionContext.put(&quot;reportRead&quot;, read);
	}

	@Override
	public void onReadError(Exception ex) {
		Integer error = (Integer) executionContext.get(&quot;reportError&quot;);
    	error++;
	    executionContext.put(&quot;reportError&quot;, error);
	}

}

But in ListenerReader i've no visibility of executionContext field.
How can i solve?

答案1

得分: 3

你可以这样做:

  1. 定义一个带有 JobScope 的 Bean
  2. 像往常一样在步骤中使用它
  3. 通过监听器进行注入

以下是一个示例:

@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

  1. Define a Bean with JobScope
  2. Use it in Step as usual
  3. Inject it via Listener.

Below is an example

@Bean
@JobScope
public SimpleReaderListener simpleReaderListener() {
      return new SimpleReaderListener();
}

@Bean
public Step step1() {
	return stepBuilderFactory.get(&quot;step1&quot;).&lt;SoccerTeam, SoccerTeam&gt; chunk(1)
			.reader(simpleReader()).listener(simpleReaderListener()).processor(new SimpleProcessor())
			.writer(new SimpleWriter()).build();
}

public class SimpleReaderListener implements ItemReadListener&lt;SoccerTeam&gt; {

	@Value(&quot;#{jobExecution.executionContext}&quot;)
	private ExecutionContext executionContext;

	@Override
	public void afterRead(SoccerTeam soccerTeam) {

	}

huangapple
  • 本文由 发表于 2020年9月17日 19:00:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63936622.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定