如何在 itermWriterListener Spring 批处理中访问 stepExecution 内容
How to access stepExecution content in itermWriterListener Spring batch
我想在 org.springframework.batch.core.ItemWriteListener 中访问 StepExecution 但不能这样做,我尝试定义局部变量:
private StepExecution StepExecution;
并添加以下代码:
@BeforeStep
public void beforeStep(StepExecution stepExecution) {
this.StepExecution = stepExecution;
}
似乎从未执行过 beforeStep 代码。无论如何都可以访问上下文吗?
此致
如果你想使用基于注解的方法,你不需要实现监听器接口,SpringBatch 会根据注解方法为每个相应的监听器接口创建一个代理。这是一个简单的例子:
import java.util.Arrays;
import java.util.List;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.annotation.AfterWrite;
import org.springframework.batch.core.annotation.BeforeStep;
import org.springframework.batch.core.annotation.BeforeWrite;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableBatchProcessing
public class MyJobConfiguration {
@Bean
public Job job(JobBuilderFactory jobs, StepBuilderFactory steps) {
return jobs.get("myJob")
.start(steps.get("myStep")
.<Integer, Integer>chunk(5)
.reader(new ListItemReader<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)))
.writer(items -> items.forEach(System.out::println))
.listener(new MyListener())
.build())
.build();
}
static class MyListener {
private StepExecution stepExecution;
@BeforeStep
public void beforeStep(StepExecution stepExecution) {
this.stepExecution = stepExecution;
}
@BeforeWrite
public void beforeWrite(List<? extends Integer> items) {
System.out.println("About to write items " + items + " in step " + stepExecution.getStepName());
}
@AfterWrite
public void afterWrite(List<? extends Integer> items) {
System.out.println("Successfully wrote items " + items + " in step " + stepExecution.getStepName());
}
}
public static void main(String[] args) throws Exception {
ApplicationContext context = new AnnotationConfigApplicationContext(MyJobConfiguration.class);
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
Job job = context.getBean(Job.class);
jobLauncher.run(job, new JobParameters());
}
}
这会打印:
About to write items [1, 2, 3, 4, 5] in step myStep
1
2
3
4
5
Successfully wrote items [1, 2, 3, 4, 5] in step myStep
About to write items [6, 7, 8, 9, 10] in step myStep
6
7
8
9
10
Successfully wrote items [6, 7, 8, 9, 10] in step myStep
在此示例中,Spring Batch 将创建一个多态代理(它实现了 ItemWriteListener
和 StepExecutionListener
)并且您只能在该步骤中注册一次。
我想在 org.springframework.batch.core.ItemWriteListener 中访问 StepExecution 但不能这样做,我尝试定义局部变量:
private StepExecution StepExecution;
并添加以下代码:
@BeforeStep
public void beforeStep(StepExecution stepExecution) {
this.StepExecution = stepExecution;
}
似乎从未执行过 beforeStep 代码。无论如何都可以访问上下文吗?
此致
如果你想使用基于注解的方法,你不需要实现监听器接口,SpringBatch 会根据注解方法为每个相应的监听器接口创建一个代理。这是一个简单的例子:
import java.util.Arrays;
import java.util.List;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.annotation.AfterWrite;
import org.springframework.batch.core.annotation.BeforeStep;
import org.springframework.batch.core.annotation.BeforeWrite;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableBatchProcessing
public class MyJobConfiguration {
@Bean
public Job job(JobBuilderFactory jobs, StepBuilderFactory steps) {
return jobs.get("myJob")
.start(steps.get("myStep")
.<Integer, Integer>chunk(5)
.reader(new ListItemReader<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)))
.writer(items -> items.forEach(System.out::println))
.listener(new MyListener())
.build())
.build();
}
static class MyListener {
private StepExecution stepExecution;
@BeforeStep
public void beforeStep(StepExecution stepExecution) {
this.stepExecution = stepExecution;
}
@BeforeWrite
public void beforeWrite(List<? extends Integer> items) {
System.out.println("About to write items " + items + " in step " + stepExecution.getStepName());
}
@AfterWrite
public void afterWrite(List<? extends Integer> items) {
System.out.println("Successfully wrote items " + items + " in step " + stepExecution.getStepName());
}
}
public static void main(String[] args) throws Exception {
ApplicationContext context = new AnnotationConfigApplicationContext(MyJobConfiguration.class);
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
Job job = context.getBean(Job.class);
jobLauncher.run(job, new JobParameters());
}
}
这会打印:
About to write items [1, 2, 3, 4, 5] in step myStep
1
2
3
4
5
Successfully wrote items [1, 2, 3, 4, 5] in step myStep
About to write items [6, 7, 8, 9, 10] in step myStep
6
7
8
9
10
Successfully wrote items [6, 7, 8, 9, 10] in step myStep
在此示例中,Spring Batch 将创建一个多态代理(它实现了 ItemWriteListener
和 StepExecutionListener
)并且您只能在该步骤中注册一次。