1.入口
@KafkaListener(topics = {"TRADE_SYNCHRONIZE_2200C"})
public void TradeSynchronizeConsumer(ConsumerRecord<String, String> data, Acknowledgment ack) {
List<CardTrade> list = JSONArray.parseArray(data.value(), CardTrade.class);
try {
receiveTradeDataFacade.icCardDataSave(list);
ack.acknowledge();
} catch (Exception e) {
//另存备注处理或者重新生产 后者容易死循环
ack.acknowledge();
}
}
2.消费异常数据库回滚
@Transactional(rollbackFor = RollbackException.class)
@Override
public Object icCardDataSave(List<CardTrade> list) throws BusinessException {
//业务
}
3.配置文件
kafka:
consumer:
//手动提交必须设置为false 要不不生效
enable-auto-commit: false
listener:
//手动提交模式
ack-mode: manual