当同步数据时reader 或者 writer 调用现有的 service时候, 会用到ItemReaderAdapter或者ItemWriterAdapter
例如
ItemReaderAdapter adapter = new ItemReaderAdapter(); adapter.setTargetObject(orderService); adapter.setTargetMethod("dealOrder");
但是上面的写法,会导致reader 和 writer 无限循环,不能停止因此需要reader 计数 重新实现 ItemStreampublic class OrderAdapterReader extends AbstractItemCountingItemStreamItemReader { @Autowired private OrderService orderService; @Autowired private HubOrderSqlOperation hubOrderSqlOperation; private List<MongoOrder> mongoOrderList; private final String ecName = "orderReaderContext"; public OrderAdapterReader() { setName(ecName); } @Override protected MongoOrder doRead() throws Exception { MongoOrder mongoOrder = mongoOrderList.get(getCurrentItemCount() - 1); return mongoOrder; } @Override protected void doOpen() throws Exception { mongoOrderList = (List<MongoOrder>) orderService.getDayOrders().getData(); if (CollectionUtils.isEmpty(mongoOrderList)){ setMaxItemCount(0); }else{ List<Long> orderIds = mongoOrderList.stream().map(MongoOrder::getId).collect(Collectors.toList()); hubOrderSqlOperation.deleteOrderByIds(orderIds); setMaxItemCount(mongoOrderList.size()); } } @Override protected void doClose() throws Exception { if (!CollectionUtils.isEmpty(mongoOrderList)){ mongoOrderList.clear(); } setMaxItemCount(0); setCurrentItemCount(0); } }