use of org.apache.rocketmq.common.message.MessageExt in project paascloud-master by paascloud.
the class OptPushMessageListener method consumeMessage.
/**
* Consume message consume concurrently status.
*
* @param messageExtList the message ext list
* @param consumeConcurrentlyContext the consume concurrently context
*
* @return the consume concurrently status
*/
@Override
@MqConsumerStore
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> messageExtList, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
MessageExt msg = messageExtList.get(0);
String body = new String(msg.getBody());
String topicName = msg.getTopic();
String tags = msg.getTags();
String keys = msg.getKeys();
log.info("MQ消费Topic={},tag={},key={}", topicName, tags, keys);
ValueOperations<String, String> ops = srt.opsForValue();
// 控制幂等性使用的key
try {
MqMessage.checkMessage(body, topicName, tags, keys);
String mqKV = null;
if (srt.hasKey(keys)) {
mqKV = ops.get(keys);
}
if (PublicUtil.isNotEmpty(mqKV)) {
log.error("MQ消费Topic={},tag={},key={}, 重复消费", topicName, tags, keys);
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
if (AliyunMqTopicConstants.MqTopicEnum.SEND_SMS_TOPIC.getTopic().equals(topicName)) {
optSendSmsTopicService.handlerSendSmsTopic(body, topicName, tags, keys);
}
if (AliyunMqTopicConstants.MqTopicEnum.SEND_EMAIL_TOPIC.getTopic().equals(topicName)) {
optSendEmailTopicService.handlerSendEmailTopic(body, topicName, tags, keys);
}
if (AliyunMqTopicConstants.MqTopicEnum.TPC_TOPIC.getTopic().equals(topicName)) {
mqMessageService.deleteMessageTopic(body, tags);
}
if (AliyunMqTopicConstants.MqTopicEnum.MDC_TOPIC.getTopic().equals(topicName)) {
mdcTopicConsumer.handlerSendSmsTopic(body, topicName, tags, keys);
} else {
log.info("OPC订单信息消 topicName={} 不存在", topicName);
}
} catch (IllegalArgumentException ex) {
log.error("校验MQ message 失败 ex={}", ex.getMessage(), ex);
} catch (Exception e) {
log.error("处理MQ message 失败 topicName={}, keys={}, ex={}", topicName, keys, e.getMessage(), e);
return ConsumeConcurrentlyStatus.RECONSUME_LATER;
}
ops.set(keys, keys, 10, TimeUnit.DAYS);
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
use of org.apache.rocketmq.common.message.MessageExt in project spring-boot-starter-samples by vindell.
the class BroadcastConsumer method main.
public static void main(String[] args) throws Exception {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("example_group_name");
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
// set to broadcast mode
consumer.setMessageModel(MessageModel.BROADCASTING);
consumer.subscribe("TopicTest", "TagA || TagC || TagD");
consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
System.out.printf(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n");
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
consumer.start();
System.out.printf("Broadcast Consumer Started.%n");
}
use of org.apache.rocketmq.common.message.MessageExt in project spring-boot-starter-samples by vindell.
the class OrderedConsumer method main.
public static void main(String[] args) throws Exception {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("example_group_name");
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
consumer.subscribe("TopicTest", "TagA || TagC || TagD");
consumer.registerMessageListener(new MessageListenerOrderly() {
AtomicLong consumeTimes = new AtomicLong(0);
@Override
public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
context.setAutoCommit(false);
System.out.printf(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n");
this.consumeTimes.incrementAndGet();
if ((this.consumeTimes.get() % 2) == 0) {
return ConsumeOrderlyStatus.SUCCESS;
} else if ((this.consumeTimes.get() % 3) == 0) {
return ConsumeOrderlyStatus.ROLLBACK;
} else if ((this.consumeTimes.get() % 4) == 0) {
return ConsumeOrderlyStatus.COMMIT;
} else if ((this.consumeTimes.get() % 5) == 0) {
context.setSuspendCurrentQueueTimeMillis(3000);
return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT;
}
return ConsumeOrderlyStatus.SUCCESS;
}
});
consumer.start();
System.out.printf("Consumer Started.%n");
}
Aggregations