1、Consumer消费类
@Configuration
public class BasStoreRoomConsumer {
@Resource
private BasStoreRoomListener basStoreRoomListener;
@Bean("posConnectorOrderConsumer")
public OrderConsumer getOrderConsumer() {
Properties properties = new Properties();
// 您在控制台创建的 Group ID
properties.put(PropertyKeyConst.GROUP_ID, consumerGroup);
// AccessKeyId 阿里云身份验证,在阿里云服务器管理控制台创建
properties.put(PropertyKeyConst.AccessKey, accessKey);
// AccessKeySecret 阿里云身份验证,在阿里云服务器管理控制台创建
properties.put(PropertyKeyConst.SecretKey, secretKey);
// 设置 TCP 接入域名,进入控制台的实例管理页面的“获取接入点信息”区域查看
properties.put(PropertyKeyConst.NAMESRV_ADDR, namesrvAddr);
// 顺序消息消费失败进行重试前的等待时间,单位(毫秒),取值范围: 10 毫秒 ~ 30,000 毫秒
properties.put(PropertyKeyConst.SuspendTimeMillis, "1000");
// 消息消费失败时的最大重试次数
properties.put(PropertyKeyConst.MaxReconsumeTimes, "10");
//集群订阅方式 (默认)
properties.put(PropertyKeyConst.MessageModel, PropertyValueConst.CLUSTERING);
//开启顺序消费加速器
properties.put(PropertyKeyConst.ENABLE_ORDERLY_CONSUME_ACCELERATOR, true);
//设置消费线程数量
properties.put(PropertyKeyConst.ConsumeThreadNums,30);
// 在订阅消息前,必须调用 start 方法来启动 Consumer,只需调用一次即可。
OrderConsumer consumer = ONSFactory.createOrderedConsumer(properties);
consumer.subscribe(
// Message 所属的 Topic
roomTopic,
// 订阅指定 Topic 下的 Tags:
// 1. * 表示订阅所有消息
// 2. TagA || TagB || TagC 表示订阅 TagA 或 TagB 或 TagC 的消息
"*",
basStoreRoomListener
);
consumer.start();
return consumer;
}
}
2、listener监听类,实现MessageOrderListener 类,处理具体业务逻辑
@Slf4j
@Component
public class BasStoreRoomListener implements MessageOrderListener {
@Override
public OrderAction consume(Message message, ConsumeOrderContext consumeOrderContext) {
String body = new String(message.getBody());
try {
log.info("BasStoreRoomListener consume message tag: {},body: {}", message.getTag(), body);
String tag = message.getTag();
if (!MqConstant.ADD_ROOM_TAG.equals(tag) && !MqConstant.UPDATE_ROOM_TAG.equals(tag)
&& !MqConstant.DELETE_ROOM_TAG.equals(tag) && !MqConstant.ROOM_ORDER_COUNT_TAG.equals(tag)) {
return OrderAction.Success;
}
//订单量
if (MqConstant.ROOM_ORDER_COUNT_TAG.equals(tag)) {
log.info("BasStoreRoomListener consume tag 包厢订单量修改!msgId: {}", message.getMsgID());
List<BasRoomCountDTO> roomCountDTOList = JSONObject.parseArray(body, BasRoomCountDTO.class);
if (CollectionUtils.isEmpty(roomCountDTOList)) {
log.warn("BasStoreRoomListener consume roomCountDTOList is empty!msgId: {}", message.getMsgID());
return OrderAction.Success;
}
//修改订单数量
updateRoomOrderCount(roomCountDTOList);
} else {
JSONObject jsonObject = JSONObject.parseObject(body);
if (jsonObject == null) {
log.warn("BasStoreRoomListener consume jsonObject is null,msgId: {}", message.getMsgID());
return OrderAction.Success;
}
log.info("BasStoreRoomListener consume tag 包厢数据变更!msgId: {}", message.getMsgID());
BasRoomMqDTO roomMqDTO = jsonObject.toJavaObject(BasRoomMqDTO.class);
if (roomMqDTO == null) {
log.warn("BasStoreRoomListener consume roomMqDTO is null,msgId: {}", message.getMsgID());
return OrderAction.Success;
}
//新增
if (MqConstant.ADD_ROOM_TAG.equals(tag)) {
log.info("BasStoreRoomListener saveRoom start!msg body: {}", body);
saveOrUpdateRoom(roomMqDTO,Boolean.FALSE);
//修改
} else if (MqConstant.UPDATE_ROOM_TAG.equals(tag)) {
log.info("BasStoreRoomListener updateRoom start!msg body: {}", body);
saveOrUpdateRoom(roomMqDTO,Boolean.TRUE);
} else { //删除
log.info("BasStoreRoomListener deleteRoom start!roomId: {}", roomMqDTO.getRoomId());
deleteRoom(roomMqDTO.getRoomId());
}
}
} catch (Exception e) {
log.error("BasStoreRoomListener order message process error, msgId:{}, tag: {}, body:{}", message.getMsgID(), message.getTag(), body, e);
return OrderAction.Suspend;
}
return OrderAction.Success;
}
}