当我们想在Writer之前完成些操作,例如聚合操作的时候可以使用Delegate模式将聚合操作在Delegate Writer操作中完成
public class SnglDenialsRptStatWriterConfig {
private static final String SNGL_DAILY_CTRL_DATA_INSERT_STATEMENT = DailyCtrlRptDataTempDao.class.getName() + ".insertSelective";
@Bean("snglDenialsRptStatWriter")
public ItemWriter<SnglDailyCtrlRptAggregate> snglDenialsRptStatWriter(SqlSessionFactory sqlSessionFactory) {
MyBatisBatchItemWriter<DailyCtrlRptDataTempPO> myBatisBatchItemWriter = new MyBatisBatchItemWriter<>();
myBatisBatchItemWriter.setSqlSessionFactory(sqlSessionFactory);
myBatisBatchItemWriter.setAssertUpdates(false);
myBatisBatchItemWriter.setStatementId(SNGL_DAILY_CTRL_DATA_INSERT_STATEMENT);
SnglDenialsRptStatWriter snglDenialsRptStatWriter = new SnglDenialsRptStatWriter();
// delegate to MyBatisBatchItemWriter
snglDenialsRptStatWriter.setDelegate(myBatisBatchItemWriter);
return snglDenialsRptStatWriter;
}
}
public class SnglDenialsRptStatWriter implements ItemWriter<SnglDailyCtrlRptAggregate> {
private ItemWriter<DailyCtrlRptDataTempPO> delegate;
public void setDelegate(ItemWriter<DailyCtrlRptDataTempPO> delegate) {
this.delegate = delegate;
}
@Autowired
private SnglDailyCtrlRptConverter snglDailyCtrlRptConverter;
@Override
public void write(List<? extends SnglDailyCtrlRptAggregate> list) throws Exception {
if (!list.isEmpty()) {
// map use for merge denials records by partition
Map<String, SnglDailyCtrlRptTempDO> denialsRecordsMergeMap = Maps.newHashMap();
list.stream().filter(Objects::nonNull).forEach(each -> {
log.info("====> process each record {}", each);
List<SnglDailyCtrlRptTempDO> denialsRecords = each.getDenialsRecords();
//merge denials records by partition
each.mergeByPartition(denialsRecordsMergeMap, denialsRecords);
});
List<SnglDailyCtrlRptTempDO> listDO = Lists.newArrayList(denialsRecordsMergeMap.values());
// get output list PO for MyBatisBatchItemWriter
List<DailyCtrlRptDataTempPO> outPutList = snglDailyCtrlRptConverter.map(listDO,
DailyCtrlRptDataTempPO.class);
// delegate to MyBatisBatchItemWriter
delegate.write(outPutList);
}
}
}
适配器代理模式样例
private static class GatewayFilterAdapter implements GatewayFilter {
private final GlobalFilter delegate;
GatewayFilterAdapter(GlobalFilter delegate) {
this.delegate = delegate;
}
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
return this.delegate.filter(exchange, chain);
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("GatewayFilterAdapter{");
sb.append("delegate=").append(delegate);
sb.append('}');
return sb.toString();
}
}
网友评论