Spring batch 如何访问itermWriterListener Spring批处理中的stepExecution内容
我想访问org.springframework.batch.core.ItemWriteListener中的StepExecution,但无法访问,我已尝试定义局部变量:Spring batch 如何访问itermWriterListener Spring批处理中的stepExecution内容,spring-batch,Spring Batch,我想访问org.springframework.batch.core.ItemWriteListener中的StepExecution,但无法访问,我已尝试定义局部变量: private-StepExecution-StepExecution; 并添加以下代码: @BeforeStep 预处理前的公共无效(步骤执行步骤执行){ this.StepExecution=StepExecution; } 似乎beforstep代码从未执行过。是否仍有访问上下文的权限 关于如果您想使用基于注释的方法
private-StepExecution-StepExecution;
并添加以下代码:
@BeforeStep
预处理前的公共无效(步骤执行步骤执行){
this.StepExecution=StepExecution;
}
似乎beforstep代码从未执行过。是否仍有访问上下文的权限
关于如果您想使用基于注释的方法,您不需要实现侦听器接口,Spring Batch将基于注释方法为每个对应的侦听器接口创建一个代理。下面是一个简单的例子:
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());
}
}
在本例中,Spring Batch将创建一个polymorph代理(它同时实现ItemWriteListener
和StepExecutionListener
),您只能在该步骤中注册一次
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