Search in sources :

Example 1 with MessageView

use of org.apache.rocketmq.console.model.MessageView 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 2 with MessageView

use of org.apache.rocketmq.console.model.MessageView in project rocketmq-externals by apache.

the class MessageServiceImplTest method viewMessage.

@Test
public void viewMessage() throws Exception {
    final String messageId = msgId;
    Pair<MessageView, List<MessageTrack>> messageViewListPair = new RetryTempLate<Pair<MessageView, List<MessageTrack>>>() {

        @Override
        protected Pair<MessageView, List<MessageTrack>> process() throws Exception {
            return messageService.viewMessage(TEST_CONSOLE_TOPIC, messageId);
        }
    }.execute(10, 1000);
    MessageView messageView = messageViewListPair.getObject1();
    Assert.assertEquals(messageView.getMessageBody(), TEST_TOPIC_MESSAGE_BODY);
}
Also used : MessageView(org.apache.rocketmq.console.model.MessageView) MessageTrack(org.apache.rocketmq.tools.admin.api.MessageTrack) List(java.util.List) Pair(org.apache.rocketmq.common.Pair) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with MessageView

use of org.apache.rocketmq.console.model.MessageView in project rocketmq-externals by apache.

the class MessageServiceImplTest method messageTrackDetail.

@Test
public void messageTrackDetail() throws Exception {
    final String messageId = msgId;
    Pair<MessageView, List<MessageTrack>> messageViewListPair = new RetryTempLate<Pair<MessageView, List<MessageTrack>>>() {

        @Override
        protected Pair<MessageView, List<MessageTrack>> process() throws Exception {
            return messageService.viewMessage(TEST_CONSOLE_TOPIC, messageId);
        }
    }.execute(10, // make the topic can be found
    1000);
    final MessageExt messageExt = mqAdminExt.viewMessage(TEST_CONSOLE_TOPIC, messageId);
    Assert.assertNotNull(messageService.messageTrackDetail(messageExt));
}
Also used : MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageView(org.apache.rocketmq.console.model.MessageView) MessageTrack(org.apache.rocketmq.tools.admin.api.MessageTrack) List(java.util.List) Pair(org.apache.rocketmq.common.Pair) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

List (java.util.List)3 MessageView (org.apache.rocketmq.console.model.MessageView)3 Pair (org.apache.rocketmq.common.Pair)2 MessageExt (org.apache.rocketmq.common.message.MessageExt)2 MessageTrack (org.apache.rocketmq.tools.admin.api.MessageTrack)2 Test (org.junit.Test)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 Function (com.google.common.base.Function)1 Date (java.util.Date)1 DefaultMQPullConsumer (org.apache.rocketmq.client.consumer.DefaultMQPullConsumer)1 PullResult (org.apache.rocketmq.client.consumer.PullResult)1 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)1