[英]Spring Batch: Using a superclass as the writer step
我有一个 spring 批处理应用程序,它有两个单独的作业。 它们使用两种不同的读取器,但处理器和写入器实际上是相同的。 writer 的唯一区别是它们每个都应该有一个名为 jobName 的属性,该属性为每个 writer 保存不同的值。 为了实现这一点,我尝试使用 writer 超类。 有没有办法实现这个? 如何调用配置 class 中的作者? class 对于作者来说应该是什么样的签名? 由于两个编写器子类的 write() 方法完全相同,它们应该在超类还是子类中实现?
工作 1
public class JobConfig1{
// ...
@Bean
public Step jobStep(SynchronizedItemStreamReader<InputObj1> someReader1,
ItemWriter someWriter1) {
return stepBuilderFactory
.get("writeAdvantageStep")
.chunk(writeChunkSize)
.reader(someReader1)
.writer(someWriter1) //value of this writer is equal to someWriter1()
.processor(new SomeProcessor())
.faultTolerant()
.build();
}
// reader step ...
// processor step ...
// writer step
// somehow class one of the writer classes (I'll call it SomeWriter1)
}
工作 2
public class JobConfig2{
// ...
@Bean
public Step jobStep(SynchronizedItemStreamReader<InputObj1> someReader2,
ItemWriter someWriter2) {
return stepBuilderFactory
.get("writeAdvantageStep")
.chunk(writeChunkSize)
.reader(someReader2)
.writer(someWriter2) //value of this writer is equal to someWriter2()
.processor(new SomeProcessor())
.faultTolerant()
.build();
}
// reader step ...
// processor step ...
// writer step
// somehow class one of the writer classes (I'll call it SomeWriter2)
}
作家1:
public class SomeWriter1 extends SomeWriter {
private String jobName = "NIGHTLY"
}
作家2:
public class SomeWriter2 extends SomeWriter {
private String jobName = "INTRADAY"
}
作家超类:
//should this class contain the code to write to the destination of the job?
public class SomeWriter implements ItemWriter<DTOObject>{
@Override public void write(List<? extends DTOObject> item) throws Exception {
//do something that involves the jobName property
}
}
提前致谢!
下面的例子有效。 尝试时有任何错误。
超级作家class
@Slf4j
public abstract class GenericWriter implements ItemWrite<String>{
public abstract String jobName();
@Overrride
public void write(List<? extends String> list) throws Exception{
log.info("job name {}", jobName());
}
}
扩展 GenericWriter
public class Writer1 extends GenericWriter{
@Override
public String jobName(){
return "Writer 1";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.