Search in sources :

Example 1 with MessageExt

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

the class MessageServiceImpl method viewMessage.

public Pair<MessageView, List<MessageTrack>> viewMessage(String subject, final String msgId) {
    try {
        MessageExt messageExt = mqAdminExt.viewMessage(subject, msgId);
        List<MessageTrack> messageTrackList = messageTrackDetail(messageExt);
        return new Pair<>(MessageView.fromMessageExt(messageExt), messageTrackList);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageTrack(org.apache.rocketmq.tools.admin.api.MessageTrack) Pair(org.apache.rocketmq.common.Pair)

Example 2 with MessageExt

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

the class MessageServiceImpl method queryMessageByTopic.

@Override
public List<MessageView> queryMessageByTopic(String topic, final long begin, final long end) {
    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer(MixAll.TOOLS_CONSUMER_GROUP, null);
    List<MessageView> messageViewList = Lists.newArrayList();
    try {
        String subExpression = "*";
        consumer.start();
        Set<MessageQueue> mqs = consumer.fetchSubscribeMessageQueues(topic);
        for (MessageQueue mq : mqs) {
            long minOffset = consumer.searchOffset(mq, begin);
            long maxOffset = consumer.searchOffset(mq, end);
            READQ: for (long offset = minOffset; offset <= maxOffset; ) {
                try {
                    if (messageViewList.size() > 2000) {
                        break;
                    }
                    PullResult pullResult = consumer.pull(mq, subExpression, offset, 32);
                    offset = pullResult.getNextBeginOffset();
                    switch(pullResult.getPullStatus()) {
                        case FOUND:
                            List<MessageView> messageViewListByQuery = Lists.transform(pullResult.getMsgFoundList(), new Function<MessageExt, MessageView>() {

                                @Override
                                public MessageView apply(MessageExt messageExt) {
                                    messageExt.setBody(null);
                                    return MessageView.fromMessageExt(messageExt);
                                }
                            });
                            List<MessageView> filteredList = Lists.newArrayList(Iterables.filter(messageViewListByQuery, new Predicate<MessageView>() {

                                @Override
                                public boolean apply(MessageView messageView) {
                                    if (messageView.getStoreTimestamp() < begin || messageView.getStoreTimestamp() > end) {
                                        logger.info("begin={} end={} time not in range {} {}", begin, end, messageView.getStoreTimestamp(), new Date(messageView.getStoreTimestamp()).toString());
                                    }
                                    return messageView.getStoreTimestamp() >= begin && messageView.getStoreTimestamp() <= end;
                                }
                            }));
                            messageViewList.addAll(filteredList);
                            break;
                        case NO_MATCHED_MSG:
                        case NO_NEW_MSG:
                        case OFFSET_ILLEGAL:
                            break READQ;
                    }
                } catch (Exception e) {
                    break;
                }
            }
        }
        Collections.sort(messageViewList, new Comparator<MessageView>() {

            @Override
            public int compare(MessageView o1, MessageView o2) {
                if (o1.getStoreTimestamp() - o2.getStoreTimestamp() == 0) {
                    return 0;
                }
                return (o1.getStoreTimestamp() > o2.getStoreTimestamp()) ? -1 : 1;
            }
        });
        return messageViewList;
    } catch (Exception e) {
        throw Throwables.propagate(e);
    } finally {
        consumer.shutdown();
    }
}
Also used : DefaultMQPullConsumer(org.apache.rocketmq.client.consumer.DefaultMQPullConsumer) PullResult(org.apache.rocketmq.client.consumer.PullResult) Date(java.util.Date) Function(com.google.common.base.Function) MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageView(org.apache.rocketmq.console.model.MessageView) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) List(java.util.List)

Example 3 with MessageExt

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

the class RocketMQConsoleTestBase method startTestMQConsumer.

protected void startTestMQConsumer() {
    // test online consumer
    consumer = new DefaultMQPushConsumer(TEST_CONSUMER_GROUP);
    consumerConfigInfo.setBrokerNameList(Lists.newArrayList(TestConstant.TEST_BROKER_NAME));
    SubscriptionGroupConfig subscriptionGroupConfig = new SubscriptionGroupConfig();
    subscriptionGroupConfig.setGroupName(TEST_CONSUMER_GROUP);
    subscriptionGroupConfig.setRetryQueueNums(RETRY_QUEUE_NUMS);
    consumerConfigInfo.setSubscriptionGroupConfig(subscriptionGroupConfig);
    consumerService.createAndUpdateSubscriptionGroupConfig(consumerConfigInfo);
    consumer.setNamesrvAddr(TestConstant.NAME_SERVER_ADDRESS);
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    try {
        consumer.subscribe(TEST_CONSOLE_TOPIC, "*");
        consumer.registerMessageListener(new MessageListenerConcurrently() {

            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                consoleTestBaseLog.info("op=consumeMessage message={}", JsonUtil.obj2String(msgs));
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        consumer.start();
    } catch (Exception e) {
        throw Throwables.propagate(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) ConsumeConcurrentlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) SubscriptionGroupConfig(org.apache.rocketmq.common.subscription.SubscriptionGroupConfig)

Example 4 with MessageExt

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

the class RocketMQSourceTest method testSource.

@Test
public void testSource() throws Exception {
    List<MessageExt> msgFoundList = new ArrayList<>();
    MessageExt messageExt = new MessageExt();
    messageExt.setKeys("keys");
    messageExt.setBody("body data".getBytes());
    messageExt.setBornTimestamp(System.currentTimeMillis());
    msgFoundList.add(messageExt);
    PullResult pullResult = new PullResult(PullStatus.FOUND, 3, 1, 5, msgFoundList);
    when(consumer.fetchConsumeOffset(any(MessageQueue.class), anyBoolean())).thenReturn(2L);
    when(consumer.pull(any(MessageQueue.class), anyString(), anyLong(), anyInt())).thenReturn(pullResult);
    SourceContext context = mock(SourceContext.class);
    when(context.getCheckpointLock()).thenReturn(new Object());
    rocketMQSource.run(context);
    // schedule the pull task
    Set<MessageQueue> set = new HashSet();
    set.add(new MessageQueue(topic, "brk", 1));
    pullConsumerScheduleService.putTask(topic, set);
    MessageExt msg = pullResult.getMsgFoundList().get(0);
    // atLeastOnce: re-pulling immediately when messages found before
    verify(context, atLeastOnce()).collectWithTimestamp(deserializationSchema.deserializeKeyAndValue(msg.getKeys().getBytes(), msg.getBody()), msg.getBornTimestamp());
}
Also used : MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) ArrayList(java.util.ArrayList) SourceContext(org.apache.flink.streaming.api.functions.source.SourceFunction.SourceContext) PullResult(org.apache.rocketmq.client.consumer.PullResult) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with MessageExt

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

the class ConsumerTest method main.

public static void main(String[] args) {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("g00003");
    consumer.setNamesrvAddr("localhost:9876");
    try {
        consumer.subscribe("flink-sink2", "*");
    } catch (MQClientException e) {
        e.printStackTrace();
    }
    consumer.registerMessageListener(new MessageListenerConcurrently() {

        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
            for (MessageExt msg : msgs) {
                System.out.println(msg.getKeys() + ":" + new String(msg.getBody()));
            }
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });
    try {
        consumer.start();
    } catch (MQClientException e) {
        e.printStackTrace();
    }
}
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) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

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