Search in sources :

Example 21 with MessageExt

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;
}
Also used : MessageExt(org.apache.rocketmq.common.message.MessageExt) MqConsumerStore(com.paascloud.provider.annotation.MqConsumerStore)

Example 22 with MessageExt

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");
}
Also used : ConsumeConcurrentlyContext(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext) MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageListenerConcurrently(org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently) ConsumeConcurrentlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer)

Example 23 with MessageExt

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");
}
Also used : MessageExt(org.apache.rocketmq.common.message.MessageExt) AtomicLong(java.util.concurrent.atomic.AtomicLong) MessageListenerOrderly(org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly) ConsumeOrderlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) ConsumeOrderlyContext(org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext)

Aggregations

MessageExt (org.apache.rocketmq.common.message.MessageExt)23 DefaultMQPushConsumer (org.apache.rocketmq.client.consumer.DefaultMQPushConsumer)7 PullResult (org.apache.rocketmq.client.consumer.PullResult)7 MQClientException (org.apache.rocketmq.client.exception.MQClientException)7 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)7 Test (org.junit.Test)7 ConsumeConcurrentlyContext (org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext)6 ConsumeConcurrentlyStatus (org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus)6 MessageListenerConcurrently (org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently)6 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 List (java.util.List)4 DefaultMQPullConsumer (org.apache.rocketmq.client.consumer.DefaultMQPullConsumer)4 MqConsumerStore (com.paascloud.provider.annotation.MqConsumerStore)3 Date (java.util.Date)3 Event (org.apache.flume.Event)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Map (java.util.Map)2 Context (org.apache.flume.Context)2 Sink (org.apache.flume.Sink)2