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();
}
}
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);
}
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));
}
Aggregations