# Spring Batch 事件监听器

> 原文： [https://howtodoinjava.com/spring-batch/spring-batch-event-listeners/](https://howtodoinjava.com/spring-batch/spring-batch-event-listeners/)

学习创建和配置 Spring Batch 的`JobExecutionListener`（作业前后），`StepExecutionListener`（步骤前后），`ItemReadListener`，`ItemProcessListener`，`ItemWriteListener`和`SkipListener`实现。

## `JobExecutionListener`

#### `JobExecutionListener`监听器示例

`JobExecutionListener Example`

```java
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;

public class JobResultListener implements JobExecutionListener {

	public void beforeJob(JobExecution jobExecution) {
		System.out.println("Called beforeJob().");
	}

	public void afterJob(JobExecution jobExecution) {
		System.out.println("Called afterJob().");
	}
}

```

#### 如何配置`JobExecutionListener`

`JobExecutionListener Configuration`

```java
@Bean
public Job demoJob(){
    return jobs.get("demoJob")
            .incrementer(new RunIdIncrementer())
            .listener(new JobResultListener())
            .start(stepOne())
            .next(stepTwo())
            .build();
}

```

## `StepExecutionListener`

#### `StepExecutionListener`监听器示例

`StepExecutionListener Example`

```java
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;

public class StepResultListener implements StepExecutionListener {

	@Override
	public void beforeStep(StepExecution stepExecution) {
		System.out.println("Called beforeStep().");
	}

	@Override
	public ExitStatus afterStep(StepExecution stepExecution) {
		System.out.println("Called afterStep().");
		return ExitStatus.COMPLETED;
	}
}

```

#### 如何配置`StepExecutionListener`

`StepExecutionListener Configuration`

```java
@Bean
public Step stepOne(){
    return steps.get("stepOne")
            .tasklet(new MyTaskOne())
            .listener(new StepResultListener())
            .build();
}

@Bean
public Step stepTwo(){
    return steps.get("stepTwo")
            .tasklet(new MyTaskTwo())
            .listener(new StepResultListener())
            .build();
}  

```

## `ItemReadListener`

#### `ItemReadListener`监听器示例

`ItemReadListener Example`

```java
import org.springframework.batch.core.ItemReadListener;

public class StepItemReadListener implements ItemReadListener<String> {

	@Override
	public void beforeRead() {
		System.out.println("ItemReadListener - beforeRead");
	}

	@Override
	public void afterRead(String item) {
		System.out.println("ItemReadListener - afterRead");
	}

	@Override
	public void onReadError(Exception ex) {
		System.out.println("ItemReadListener - onReadError");
	}
}

```

#### 如何配置`ItemReadListener`

`ItemReadListener Configuration`

```java
@Bean
public Step stepOne(){
    return steps.get("stepOne")
            .tasklet(new MyTaskOne())
            .listener(new StepItemReadListener())
            .build();
}

```

## `ItemProcessListener`

#### `ItemProcessListener`监听器示例

`ItemProcessListener Example`

```java
import org.springframework.batch.core.ItemProcessListener;

public class StepItemProcessListener implements ItemProcessListener<String, Number> {

	@Override
	public void beforeProcess(String item) {
		System.out.println("ItemProcessListener - beforeProcess");
	}

	@Override
	public void afterProcess(String item, Number result) {
		System.out.println("ItemProcessListener - afterProcess");
	}

	@Override
	public void onProcessError(String item, Exception e) {
		System.out.println("ItemProcessListener - onProcessError");
	}
}

```

#### 如何配置`ItemProcessListener`

`ItemProcessListener Configuration`

```java
@Bean
public Step stepOne(){
    return steps.get("stepOne")
            .tasklet(new MyTaskOne())
            .listener(new StepItemProcessListener())
            .build();
}

```

## `ItemWriteListener`

#### `ItemWriteListener`监听器示例

`ItemWriteListener Example`

```java
import java.util.List;
import org.springframework.batch.core.ItemWriteListener;

public class StepItemWriteListener implements ItemWriteListener<Number> {

	@Override
	public void beforeWrite(List<? extends Number> items) {
		System.out.println("ItemWriteListener - beforeWrite");
	}

	@Override
	public void afterWrite(List<? extends Number> items) {
		System.out.println("ItemWriteListener - afterWrite");
	}

	@Override
	public void onWriteError(Exception exception, List<? extends Number> items) {
		System.out.println("ItemWriteListener - onWriteError");
	}
}

```

#### 如何配置`ItemWriteListener`

`ItemWriteListener Configuration`

```java
@Bean
public Step stepOne(){
    return steps.get("stepOne")
            .tasklet(new MyTaskOne())
            .listener(new StepItemWriteListener())
            .build();
}

```

## `SkipListener`

#### `SkipListener`监听器示例

`SkipListener Example`

```java
import org.springframework.batch.core.SkipListener;

public class StepSkipListener implements SkipListener<String, Number> {

	@Override
	public void onSkipInRead(Throwable t) {
		System.out.println("StepSkipListener - onSkipInRead");
	}

	@Override
	public void onSkipInWrite(Number item, Throwable t) {
		System.out.println("StepSkipListener - afterWrite");
	}

	@Override
	public void onSkipInProcess(String item, Throwable t) {
		System.out.println("StepSkipListener - onWriteError");
	}
}

```

#### 如何配置`SkipListener`

`SkipListener Configuration`

```java
@Bean
public Step stepOne(){
    return steps.get("stepOne")
            .tasklet(new MyTaskOne())
            .listener(new StepSkipListener())
            .build();
}

```

使用和实现非常简单。 在评论部分让我知道您的问题。

学习愉快！

参考文献：

1.  [`JobExecutionListener` JavaDoc](https://docs.spring.io/spring-batch/4.0.x/api/org/springframework/batch/core/JobExecutionListener.html)
2.  [`StepExecutionListener` JavaDoc](https://docs.spring.io/spring-batch/4.0.x/api/org/springframework/batch/core/StepExecutionListener.html)
3.  [`ItemReadListener` JavaDoc](https://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/core/ItemReadListener.html)
4.  [`ItemProcessListener` JavaDoc](https://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/core/ItemProcessListener.html)
5.  [`ItemWriteListener` JavaDoc](https://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/core/ItemWriteListener.html)
6.  [`StepSkipListener` JavaDoc](https://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/core/SkipListener.html)