如何通过 java 配置没有 StepExecutionListener 实现的侦听器注册,既没有 StepListenerExecutionSupport 扩展
how register via java config a listener without StepExecutionListener implementation neither StepListenerExecutionSupport extending
我正在学习 Spring Batch in Action 一书,我在第 114/115 页找到了 StopListener 示例。如何注册一个既不实现 StepExecutionListener 又不扩展 StepListenerExecutionSupport 的侦听器 class?我没有使用 Spring Xml 配置,所以我必须将 xml 中提供的示例转换为 java 配置。
据书
public class StopListener {
private StepExecution stepExecution;
@BeforeStep
public void beforeStep(
StepExecution stepExecution) {
this.stepExecution = stepExecution;
}
@AfterRead
public void afterRead() {
if(stopConditionsMet()) {
stepExecution.setTerminateOnly();
}
}...
<bean id="stopListener" class="com.manning.sbia.ch04.stop.StopListener" />
<batch:job id="importProductsJob">
<batch:step id="importProductsStep">
<batch:tasklet>
<batch:chunk reader="reader" writer="writer" commit-interval="100"/>
<batch:listeners>
<batch:listener ref="stopListener" />
</batch:listeners>
</batch:tasklet>
</batch:step>
</batch:job>
正如我所尝试的:
...
@Bean
public StepExecutionListener stepListener(){
return new StopListener(); //obviously, it raises casting issues
}
@Bean
public Step stepA(ItemReader<String> readerA, ItemWriter<String> writerA,
StepExecutionListener stepListener) {
return stepBuilderFactory.get("stepA").<String, String> chunk(1)
.reader(readerA).writer(writerA).allowStartIfComplete(false)
.startLimit(1).listener(stepListener).build();
}
@Component
public class StopListener {
private StepExecution stepExecution;
private static final Logger log = LoggerFactory.getLogger(StopListener.class);
@BeforeStep
public void beforeStep(StepExecution stepExecution) {
log.info("StopListener.beforeStep()");
this.stepExecution = stepExecution;
}
@AfterRead
public void afterRead() {
if (true) {
stepExecution.setTerminateOnly();
}
}
}
像这样。
@豆子
public StopListener stepListener(){
return 新的 StopListener(); //显然,它引发了转换问题
}
@Bean
public Step stepA(ItemReader<String> readerA, ItemWriter<String> writerA) {
return stepBuilderFactory.get("stepA").<String, String> chunk(1)
.reader(readerA).writer(writerA).allowStartIfComplete(false)
.startLimit(1).listener(stepListener()).build();
}
listener
方法采用 Object
并检测类型或注释方法。
我正在学习 Spring Batch in Action 一书,我在第 114/115 页找到了 StopListener 示例。如何注册一个既不实现 StepExecutionListener 又不扩展 StepListenerExecutionSupport 的侦听器 class?我没有使用 Spring Xml 配置,所以我必须将 xml 中提供的示例转换为 java 配置。
据书
public class StopListener {
private StepExecution stepExecution;
@BeforeStep
public void beforeStep(
StepExecution stepExecution) {
this.stepExecution = stepExecution;
}
@AfterRead
public void afterRead() {
if(stopConditionsMet()) {
stepExecution.setTerminateOnly();
}
}...
<bean id="stopListener" class="com.manning.sbia.ch04.stop.StopListener" />
<batch:job id="importProductsJob">
<batch:step id="importProductsStep">
<batch:tasklet>
<batch:chunk reader="reader" writer="writer" commit-interval="100"/>
<batch:listeners>
<batch:listener ref="stopListener" />
</batch:listeners>
</batch:tasklet>
</batch:step>
</batch:job>
正如我所尝试的:
...
@Bean
public StepExecutionListener stepListener(){
return new StopListener(); //obviously, it raises casting issues
}
@Bean
public Step stepA(ItemReader<String> readerA, ItemWriter<String> writerA,
StepExecutionListener stepListener) {
return stepBuilderFactory.get("stepA").<String, String> chunk(1)
.reader(readerA).writer(writerA).allowStartIfComplete(false)
.startLimit(1).listener(stepListener).build();
}
@Component
public class StopListener {
private StepExecution stepExecution;
private static final Logger log = LoggerFactory.getLogger(StopListener.class);
@BeforeStep
public void beforeStep(StepExecution stepExecution) {
log.info("StopListener.beforeStep()");
this.stepExecution = stepExecution;
}
@AfterRead
public void afterRead() {
if (true) {
stepExecution.setTerminateOnly();
}
}
}
像这样。
@豆子 public StopListener stepListener(){ return 新的 StopListener(); //显然,它引发了转换问题 }
@Bean
public Step stepA(ItemReader<String> readerA, ItemWriter<String> writerA) {
return stepBuilderFactory.get("stepA").<String, String> chunk(1)
.reader(readerA).writer(writerA).allowStartIfComplete(false)
.startLimit(1).listener(stepListener()).build();
}
listener
方法采用 Object
并检测类型或注释方法。