Search in sources :

Example 16 with MessageExt

use of org.apache.rocketmq.common.message.MessageExt in project rocketmq-externals by apache.

the class BinlogPositionManager method initPositionFromMqTail.

private void initPositionFromMqTail() throws Exception {
    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("BINLOG_CONSUMER_GROUP");
    consumer.setNamesrvAddr(config.mqNamesrvAddr);
    consumer.setMessageModel(MessageModel.valueOf("BROADCASTING"));
    consumer.start();
    Set<MessageQueue> queues = consumer.fetchSubscribeMessageQueues(config.mqTopic);
    MessageQueue queue = queues.iterator().next();
    if (queue != null) {
        Long offset = consumer.maxOffset(queue);
        if (offset > 0)
            offset--;
        PullResult pullResult = consumer.pull(queue, "*", offset, 100);
        if (pullResult.getPullStatus() == PullStatus.FOUND) {
            MessageExt msg = pullResult.getMsgFoundList().get(0);
            String json = new String(msg.getBody(), "UTF-8");
            JSONObject js = JSON.parseObject(json);
            binlogFilename = (String) js.get("binlogFilename");
            nextPosition = js.getLong("nextPosition");
        }
    }
}
Also used : MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) JSONObject(com.alibaba.fastjson.JSONObject) DefaultMQPullConsumer(org.apache.rocketmq.client.consumer.DefaultMQPullConsumer) PullResult(org.apache.rocketmq.client.consumer.PullResult)

Example 17 with MessageExt

use of org.apache.rocketmq.common.message.MessageExt in project rocketmq-externals by apache.

the class TestController method list.

@RequestMapping(value = "/runTask.do", method = RequestMethod.GET)
@ResponseBody
public Object list() throws MQClientException, RemotingException, InterruptedException {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(testTopic + "Group");
    consumer.setNamesrvAddr(rMQConfigure.getNamesrvAddr());
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    consumer.subscribe(testTopic, "*");
    consumer.registerMessageListener(new MessageListenerConcurrently() {

        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
            logger.info("receiveMessage msgSize={}", msgs.size());
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });
    consumer.start();
    final DefaultMQProducer producer = new DefaultMQProducer(testTopic + "Group");
    producer.setInstanceName(String.valueOf(System.currentTimeMillis()));
    producer.setNamesrvAddr(rMQConfigure.getNamesrvAddr());
    producer.start();
    new Thread(new Runnable() {

        @Override
        public void run() {
            int i = 0;
            while (true) {
                try {
                    Message msg = new Message(testTopic, "TagA" + i, "KEYS" + i, ("Hello RocketMQ " + i).getBytes());
                    Thread.sleep(1000L);
                    SendResult sendResult = producer.send(msg);
                    logger.info("sendMessage={}", JsonUtil.obj2String(sendResult));
                } catch (Exception e) {
                    e.printStackTrace();
                    try {
                        Thread.sleep(1000);
                    } catch (Exception ignore) {
                    }
                }
            }
        }
    }).start();
    return true;
}
Also used : MessageListenerConcurrently(org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently) Message(org.apache.rocketmq.common.message.Message) ConsumeConcurrentlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) MQClientException(org.apache.rocketmq.client.exception.MQClientException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) ConsumeConcurrentlyContext(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext) MessageExt(org.apache.rocketmq.common.message.MessageExt) SendResult(org.apache.rocketmq.client.producer.SendResult) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 18 with MessageExt

use of org.apache.rocketmq.common.message.MessageExt in project rocketmq-externals by apache.

the class RocketMQReceiver method onStart.

@Override
public void onStart() {
    Validate.notEmpty(properties, "Consumer properties can not be empty");
    ordered = RocketMqUtils.getBoolean(properties, RocketMQConfig.CONSUMER_MESSAGES_ORDERLY, false);
    consumer = new DefaultMQPushConsumer();
    RocketMQConfig.buildConsumerConfigs(properties, (DefaultMQPushConsumer) consumer);
    if (ordered) {
        consumer.registerMessageListener(new MessageListenerOrderly() {

            @Override
            public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
                if (process(msgs)) {
                    return ConsumeOrderlyStatus.SUCCESS;
                } else {
                    return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT;
                }
            }
        });
    } else {
        consumer.registerMessageListener(new MessageListenerConcurrently() {

            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                if (process(msgs)) {
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                } else {
                    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                }
            }
        });
    }
    try {
        consumer.start();
    } catch (MQClientException e) {
        throw new RuntimeException(e);
    }
}
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) MessageListenerOrderly(org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly) ConsumeOrderlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus) ConsumeConcurrentlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) ConsumeOrderlyContext(org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 19 with MessageExt

use of org.apache.rocketmq.common.message.MessageExt in project rocketmq-externals by apache.

the class RocketMqUtilsTest method testGetOffsets.

@Test
public void testGetOffsets() throws MQBrokerException, MQClientException, InterruptedException, UnsupportedEncodingException {
    Map<String, String> optionParams = new HashMap<>();
    optionParams.put(RocketMQConfig.NAME_SERVER_ADDR, NAME_SERVER);
    SparkConf sparkConf = new SparkConf().setAppName("JavaCustomReceiver").setMaster("local[*]");
    JavaStreamingContext sc = new JavaStreamingContext(sparkConf, new Duration(1000));
    List<String> topics = new ArrayList<>();
    topics.add(TOPIC_DEFAULT);
    LocationStrategy locationStrategy = LocationStrategy.PreferConsistent();
    JavaInputDStream<MessageExt> dStream = RocketMqUtils.createJavaMQPullStream(sc, UUID.randomUUID().toString(), topics, ConsumerStrategy.earliest(), false, false, false, locationStrategy, optionParams);
    // hold a reference to the current offset ranges, so it can be used downstream
    final AtomicReference<Map<TopicQueueId, OffsetRange[]>> offsetRanges = new AtomicReference<>();
    final Set<MessageExt> result = Collections.synchronizedSet(new HashSet<MessageExt>());
    dStream.transform(new Function<JavaRDD<MessageExt>, JavaRDD<MessageExt>>() {

        @Override
        public JavaRDD<MessageExt> call(JavaRDD<MessageExt> v1) throws Exception {
            Map<TopicQueueId, OffsetRange[]> offsets = ((HasOffsetRanges) v1.rdd()).offsetRanges();
            offsetRanges.set(offsets);
            return v1;
        }
    }).foreachRDD(new VoidFunction<JavaRDD<MessageExt>>() {

        @Override
        public void call(JavaRDD<MessageExt> messageExtJavaRDD) throws Exception {
            result.addAll(messageExtJavaRDD.collect());
        }
    });
    sc.start();
    long startTime = System.currentTimeMillis();
    boolean matches = false;
    while (!matches && System.currentTimeMillis() - startTime < 20000) {
        matches = MESSAGE_NUM == result.size();
        Thread.sleep(50);
    }
    sc.stop();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HasOffsetRanges(org.apache.rocketmq.spark.HasOffsetRanges) JavaStreamingContext(org.apache.spark.streaming.api.java.JavaStreamingContext) VoidFunction(org.apache.spark.api.java.function.VoidFunction) Function(org.apache.spark.api.java.function.Function) Duration(org.apache.spark.streaming.Duration) AtomicReference(java.util.concurrent.atomic.AtomicReference) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JavaRDD(org.apache.spark.api.java.JavaRDD) OffsetRange(org.apache.rocketmq.spark.OffsetRange) MessageExt(org.apache.rocketmq.common.message.MessageExt) LocationStrategy(org.apache.rocketmq.spark.LocationStrategy) SparkConf(org.apache.spark.SparkConf) HashMap(java.util.HashMap) Map(java.util.Map) TopicQueueId(org.apache.rocketmq.spark.TopicQueueId) Test(org.junit.Test)

Example 20 with MessageExt

use of org.apache.rocketmq.common.message.MessageExt in project paascloud-master by paascloud.

the class UacPushMessageListener 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();
    try {
        MqMessage.checkMessage(body, topicName, tags, keys);
        String mqKV = redisService.getKey(keys);
        if (PublicUtil.isNotEmpty(mqKV)) {
            log.error("MQ消费Topic={},tag={},key={}, 重复消费", topicName, tags, keys);
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
        if (AliyunMqTopicConstants.MqTopicEnum.TPC_TOPIC.getTopic().equals(topicName)) {
            mqMessageService.deleteMessageTopic(body, tags);
        } 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;
    }
    redisService.setKey(keys, keys, 10, TimeUnit.DAYS);
    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
Also used : MessageExt(org.apache.rocketmq.common.message.MessageExt) MqConsumerStore(com.paascloud.provider.annotation.MqConsumerStore)

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