org.apache.rocketmq.client.consumer.PullStatus Java Examples

The following examples show how to use org.apache.rocketmq.client.consumer.PullStatus. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RmqAdminServiceImpl.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
private Message queryMessageByOffset(String nameServer, String topic, String brokerName, Integer qid, long offset) throws Exception {
    DefaultMQPullConsumer mqPullConsumer = getMqPullConsumer(nameServer);

    MessageQueue mq = new MessageQueue();
    mq.setTopic(topic);
    mq.setBrokerName(brokerName);
    mq.setQueueId(qid);

    PullResult pullResult = mqPullConsumer.pull(mq, "*", offset, 1, 5000);
    if (pullResult == null || pullResult.getPullStatus() != PullStatus.FOUND) {
        throw new MqException(String.format("[RMQ] message not exsit, nsrv:%s, topic:%s, brokerName:%s, qid:%s, offset:%d", nameServer, topic, brokerName, qid, offset));
    }

    MessageExt messageExt = pullResult.getMsgFoundList().get(0);
    if (messageExt.getBody().length == 0) {
        return null;
    }

    String msg;
    if (CodecsUtils.isUtf8(messageExt.getBody())) {
        msg = new String(messageExt.getBody(), "UTF-8");
    } else {
        msg = java.util.Base64.getEncoder().encodeToString(messageExt.getBody());
    }
    return new Message(brokerName + "_" + qid, offset, msg, messageExt.getTags(), messageExt.getKeys(), messageExt.getStoreSize(), messageExt.getBornTimestamp());
}
 
Example #2
Source File: ConsumeMessageCommandTest.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void init() throws MQClientException, RemotingException, MQBrokerException, InterruptedException,
    NoSuchFieldException, IllegalAccessException {
    consumeMessageCommand = new ConsumeMessageCommand();
    DefaultMQPullConsumer defaultMQPullConsumer = mock(DefaultMQPullConsumer.class);
    MessageExt msg = new MessageExt();
    msg.setBody(new byte[] {'a'});
    List<MessageExt> msgFoundList = new ArrayList<>();
    msgFoundList.add(msg);
    final PullResult pullResult = new PullResult(PullStatus.FOUND, 2, 0, 1, msgFoundList);

    when(defaultMQPullConsumer.pull(any(MessageQueue.class), anyString(), anyLong(), anyInt())).thenReturn(pullResult);
    when(defaultMQPullConsumer.minOffset(any(MessageQueue.class))).thenReturn(Long.valueOf(0));
    when(defaultMQPullConsumer.maxOffset(any(MessageQueue.class))).thenReturn(Long.valueOf(1));

    final Set<MessageQueue> mqList = new HashSet<>();
    mqList.add(new MessageQueue());
    when(defaultMQPullConsumer.fetchSubscribeMessageQueues(anyString())).thenReturn(mqList);

    Field producerField = ConsumeMessageCommand.class.getDeclaredField("defaultMQPullConsumer");
    producerField.setAccessible(true);
    producerField.set(consumeMessageCommand, defaultMQPullConsumer);
}
 
Example #3
Source File: MQClientAPIImpl.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
private PullResult processPullResponse(final RemotingCommand response) throws MQBrokerException, RemotingCommandException {
    PullStatus pullStatus = PullStatus.NO_NEW_MSG;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS:
            pullStatus = PullStatus.FOUND;
            break;
        case ResponseCode.PULL_NOT_FOUND:
            pullStatus = PullStatus.NO_NEW_MSG;
            break;
        case ResponseCode.PULL_RETRY_IMMEDIATELY:
            pullStatus = PullStatus.NO_MATCHED_MSG;
            break;
        case ResponseCode.PULL_OFFSET_MOVED:
            pullStatus = PullStatus.OFFSET_ILLEGAL;
            break;

        default:
            throw new MQBrokerException(response.getCode(), response.getRemark());
    }

    PullMessageResponseHeader responseHeader =
        (PullMessageResponseHeader) response.decodeCommandCustomHeader(PullMessageResponseHeader.class);

    return new PullResultExt(pullStatus, responseHeader.getNextBeginOffset(), responseHeader.getMinOffset(),
        responseHeader.getMaxOffset(), null, responseHeader.getSuggestWhichBrokerId(), response.getBody());
}
 
Example #4
Source File: RmqAdminServiceImpl.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
private Message queryMessageByOffset(String nameServer, String topic, String brokerName, Integer qid, long offset) throws Exception {
    DefaultMQPullConsumer mqPullConsumer = getMqPullConsumer(nameServer);

    MessageQueue mq = new MessageQueue();
    mq.setTopic(topic);
    mq.setBrokerName(brokerName);
    mq.setQueueId(qid);

    PullResult pullResult = mqPullConsumer.pull(mq, "*", offset, 1, 5000);
    if (pullResult == null || pullResult.getPullStatus() != PullStatus.FOUND) {
        throw new MqException(String.format("[RMQ] message not exsit, nsrv:%s, topic:%s, brokerName:%s, qid:%s, offset:%d", nameServer, topic, brokerName, qid, offset));
    }

    MessageExt messageExt = pullResult.getMsgFoundList().get(0);
    if (messageExt.getBody().length == 0) {
        return null;
    }

    String msg;
    if (CodecsUtils.isUtf8(messageExt.getBody())) {
        msg = new String(messageExt.getBody(), "UTF-8");
    } else {
        msg = java.util.Base64.getEncoder().encodeToString(messageExt.getBody());
    }
    return new Message(brokerName + "_" + qid, offset, msg, messageExt.getTags(), messageExt.getKeys(), messageExt.getStoreSize(), messageExt.getBornTimestamp());
}
 
Example #5
Source File: MQClientAPIImpl.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
private PullResult processPullResponse(final RemotingCommand response) throws MQBrokerException, RemotingCommandException {
    PullStatus pullStatus = PullStatus.NO_NEW_MSG;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS:
            pullStatus = PullStatus.FOUND;
            break;
        case ResponseCode.PULL_NOT_FOUND:
            pullStatus = PullStatus.NO_NEW_MSG;
            break;
        case ResponseCode.PULL_RETRY_IMMEDIATELY:
            pullStatus = PullStatus.NO_MATCHED_MSG;
            break;
        case ResponseCode.PULL_OFFSET_MOVED:
            pullStatus = PullStatus.OFFSET_ILLEGAL;
            break;

        default:
            throw new MQBrokerException(response.getCode(), response.getRemark());
    }

    PullMessageResponseHeader responseHeader =
        (PullMessageResponseHeader)response.decodeCommandCustomHeader(PullMessageResponseHeader.class);

    return new PullResultExt(pullStatus, responseHeader.getNextBeginOffset(), responseHeader.getMinOffset(),
        responseHeader.getMaxOffset(), null, responseHeader.getSuggestWhichBrokerId(), response.getBody());
}
 
Example #6
Source File: ConsumeMessageCommandTest.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void init() throws MQClientException, RemotingException, MQBrokerException, InterruptedException,
    NoSuchFieldException, IllegalAccessException {
    consumeMessageCommand = new ConsumeMessageCommand();
    DefaultMQPullConsumer defaultMQPullConsumer = mock(DefaultMQPullConsumer.class);
    MessageExt msg = new MessageExt();
    msg.setBody(new byte[] {'a'});
    List<MessageExt> msgFoundList = new ArrayList<>();
    msgFoundList.add(msg);
    final PullResult pullResult = new PullResult(PullStatus.FOUND, 2, 0, 1, msgFoundList);

    when(defaultMQPullConsumer.pull(any(MessageQueue.class), anyString(), anyLong(), anyInt())).thenReturn(pullResult);
    when(defaultMQPullConsumer.minOffset(any(MessageQueue.class))).thenReturn(Long.valueOf(0));
    when(defaultMQPullConsumer.maxOffset(any(MessageQueue.class))).thenReturn(Long.valueOf(1));

    final Set<MessageQueue> mqList = new HashSet<>();
    mqList.add(new MessageQueue());
    when(defaultMQPullConsumer.fetchSubscribeMessageQueues(anyString())).thenReturn(mqList);

    Field producerField = ConsumeMessageCommand.class.getDeclaredField("defaultMQPullConsumer");
    producerField.setAccessible(true);
    producerField.set(consumeMessageCommand, defaultMQPullConsumer);
}
 
Example #7
Source File: MQClientAPIImpl.java    From rocketmq_trans_message with Apache License 2.0 6 votes vote down vote up
private PullResult processPullResponse(final RemotingCommand response) throws MQBrokerException, RemotingCommandException {
    PullStatus pullStatus = PullStatus.NO_NEW_MSG;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS:
            pullStatus = PullStatus.FOUND;
            break;
        case ResponseCode.PULL_NOT_FOUND:
            pullStatus = PullStatus.NO_NEW_MSG;
            break;
        case ResponseCode.PULL_RETRY_IMMEDIATELY:
            pullStatus = PullStatus.NO_MATCHED_MSG;
            break;
        case ResponseCode.PULL_OFFSET_MOVED:
            pullStatus = PullStatus.OFFSET_ILLEGAL;
            break;

        default:
            throw new MQBrokerException(response.getCode(), response.getRemark());
    }

    PullMessageResponseHeader responseHeader =
        (PullMessageResponseHeader) response.decodeCommandCustomHeader(PullMessageResponseHeader.class);

    return new PullResultExt(pullStatus, responseHeader.getNextBeginOffset(), responseHeader.getMinOffset(),
        responseHeader.getMaxOffset(), null, responseHeader.getSuggestWhichBrokerId(), response.getBody());
}
 
Example #8
Source File: ConsumeMessageCommandTest.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void init() throws MQClientException, RemotingException, MQBrokerException, InterruptedException,
    NoSuchFieldException, IllegalAccessException {
    consumeMessageCommand = new ConsumeMessageCommand();
    DefaultMQPullConsumer defaultMQPullConsumer = mock(DefaultMQPullConsumer.class);
    MessageExt msg = new MessageExt();
    msg.setBody(new byte[] {'a'});
    List<MessageExt> msgFoundList = new ArrayList<>();
    msgFoundList.add(msg);
    final PullResult pullResult = new PullResult(PullStatus.FOUND, 2, 0, 1, msgFoundList);

    when(defaultMQPullConsumer.pull(any(MessageQueue.class), anyString(), anyLong(), anyInt())).thenReturn(pullResult);
    when(defaultMQPullConsumer.minOffset(any(MessageQueue.class))).thenReturn(Long.valueOf(0));
    when(defaultMQPullConsumer.maxOffset(any(MessageQueue.class))).thenReturn(Long.valueOf(1));

    final Set<MessageQueue> mqList = new HashSet<>();
    mqList.add(new MessageQueue());
    when(defaultMQPullConsumer.fetchSubscribeMessageQueues(anyString())).thenReturn(mqList);

    Field producerField = ConsumeMessageCommand.class.getDeclaredField("defaultMQPullConsumer");
    producerField.setAccessible(true);
    producerField.set(consumeMessageCommand, defaultMQPullConsumer);
}
 
Example #9
Source File: MQClientAPIImpl.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private PullResult processPullResponse(
    final RemotingCommand response) throws MQBrokerException, RemotingCommandException {
    PullStatus pullStatus = PullStatus.NO_NEW_MSG;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS:
            pullStatus = PullStatus.FOUND;
            break;
        case ResponseCode.PULL_NOT_FOUND:
            pullStatus = PullStatus.NO_NEW_MSG;
            break;
        case ResponseCode.PULL_RETRY_IMMEDIATELY:
            pullStatus = PullStatus.NO_MATCHED_MSG;
            break;
        case ResponseCode.PULL_OFFSET_MOVED:
            pullStatus = PullStatus.OFFSET_ILLEGAL;
            break;

        default:
            throw new MQBrokerException(response.getCode(), response.getRemark());
    }

    PullMessageResponseHeader responseHeader =
        (PullMessageResponseHeader) response.decodeCommandCustomHeader(PullMessageResponseHeader.class);

    return new PullResultExt(pullStatus, responseHeader.getNextBeginOffset(), responseHeader.getMinOffset(),
        responseHeader.getMaxOffset(), null, responseHeader.getSuggestWhichBrokerId(), response.getBody());
}
 
Example #10
Source File: ConsumerLagMonitor.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private long getRmqConsumeDelay(RocketMQProduceOffsetFetcher fetcher, MessageQueue mq, long produceOffset, long consumeOffset, String group) {
    try {
        if (produceOffset == 0L || produceOffset <= consumeOffset) {
            // 未生产 or 消费完毕.
            return 0L;
        }

        PullResult consumePullResult = fetcher.queryMsgByOffset(mq, consumeOffset);

        if (consumePullResult != null && consumePullResult.getPullStatus() == PullStatus.FOUND) {
            return TimeUtils.getElapseTime(consumePullResult.getMsgFoundList().get(0).getStoreTimestamp());
        } else if (consumePullResult.getPullStatus() == PullStatus.NO_MATCHED_MSG) {
            LOGGER.error("failed to getRmqConsumeDelay. mq: {}, namesvr: {}, group: {}, produceOffset: {}, consumeOffset: {}, consumePullResult: {}", mq, fetcher.getNamesrvAddr(), group, produceOffset, consumeOffset, consumePullResult);
            return 0; // 拿不到消息,认为没有延迟.
        } else if (consumePullResult.getPullStatus() == PullStatus.OFFSET_ILLEGAL) {
            LOGGER.error("failed to getRmqConsumeDelay. mq: {}, namesvr: {}, group: {}, produceOffset: {}, consumeOffset: {}, consumePullResult: {}", mq, fetcher.getNamesrvAddr(), group, produceOffset, consumeOffset, consumePullResult);

            PullResult pullResult = fetcher.queryMsgByOffset(mq, consumePullResult.getMinOffset());
            if (pullResult != null && pullResult.getPullStatus() == PullStatus.FOUND) {
                return TimeUtils.getElapseTime(pullResult.getMsgFoundList().get(0).getStoreTimestamp());
            }

            return MSG_NOT_FOUND;
        } else {
            return 0;
        }
    } catch (Exception e) {
        LOGGER.error("queryMsgByOffset failed,", e);
        return -1L;
    }
}
 
Example #11
Source File: MQClientAPIImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private PullResult processPullResponse(
    final RemotingCommand response) throws MQBrokerException, RemotingCommandException {
    PullStatus pullStatus = PullStatus.NO_NEW_MSG;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS:
            pullStatus = PullStatus.FOUND;
            break;
        case ResponseCode.PULL_NOT_FOUND:
            pullStatus = PullStatus.NO_NEW_MSG;
            break;
        case ResponseCode.PULL_RETRY_IMMEDIATELY:
            pullStatus = PullStatus.NO_MATCHED_MSG;
            break;
        case ResponseCode.PULL_OFFSET_MOVED:
            pullStatus = PullStatus.OFFSET_ILLEGAL;
            break;

        default:
            throw new MQBrokerException(response.getCode(), response.getRemark());
    }

    PullMessageResponseHeader responseHeader =
        (PullMessageResponseHeader) response.decodeCommandCustomHeader(PullMessageResponseHeader.class);

    return new PullResultExt(pullStatus, responseHeader.getNextBeginOffset(), responseHeader.getMinOffset(),
        responseHeader.getMaxOffset(), null, responseHeader.getSuggestWhichBrokerId(), response.getBody());
}
 
Example #12
Source File: MQClientAPIImpl.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
private PullResult processPullResponse(
    final RemotingCommand response) throws MQBrokerException, RemotingCommandException {
    PullStatus pullStatus = PullStatus.NO_NEW_MSG;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS:
            pullStatus = PullStatus.FOUND;
            break;
        case ResponseCode.PULL_NOT_FOUND:
            pullStatus = PullStatus.NO_NEW_MSG;
            break;
        case ResponseCode.PULL_RETRY_IMMEDIATELY:
            pullStatus = PullStatus.NO_MATCHED_MSG;
            break;
        case ResponseCode.PULL_OFFSET_MOVED:
            pullStatus = PullStatus.OFFSET_ILLEGAL;
            break;

        default:
            throw new MQBrokerException(response.getCode(), response.getRemark());
    }

    PullMessageResponseHeader responseHeader =
        (PullMessageResponseHeader) response.decodeCommandCustomHeader(PullMessageResponseHeader.class);

    return new PullResultExt(pullStatus, responseHeader.getNextBeginOffset(), responseHeader.getMinOffset(),
        responseHeader.getMaxOffset(), null, responseHeader.getSuggestWhichBrokerId(), response.getBody());
}
 
Example #13
Source File: DeFiBusMessageListenerConcurrentlyTest.java    From DeFiBus with Apache License 2.0 5 votes vote down vote up
private PullResultExt createPullResult(PullMessageRequestHeader requestHeader, PullStatus pullStatus,
    List<MessageExt> messageExtList) throws Exception {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    for (MessageExt messageExt : messageExtList) {
        outputStream.write(MessageDecoder.encode(messageExt, false));
    }
    return new PullResultExt(pullStatus, requestHeader.getQueueOffset() + messageExtList.size(), 123, 2048, messageExtList, 0, outputStream.toByteArray());
}
 
Example #14
Source File: DefaultMQConsumerWithTraceTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private PullResultExt createPullResult(PullMessageRequestHeader requestHeader, PullStatus pullStatus,
    List<MessageExt> messageExtList) throws Exception {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    for (MessageExt messageExt : messageExtList) {
        outputStream.write(MessageDecoder.encode(messageExt, false));
    }
    return new PullResultExt(pullStatus, requestHeader.getQueueOffset() + messageExtList.size(), 123, 2048, messageExtList, 0, outputStream.toByteArray());
}
 
Example #15
Source File: DeFiBusMessageListenerConcurrentlyOnceTest.java    From DeFiBus with Apache License 2.0 5 votes vote down vote up
private PullResultExt createPullResult(PullMessageRequestHeader requestHeader, PullStatus pullStatus,
    List<MessageExt> messageExtList) throws Exception {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    for (MessageExt messageExt : messageExtList) {
        outputStream.write(MessageDecoder.encode(messageExt, false));
    }
    return new PullResultExt(pullStatus, requestHeader.getQueueOffset() + messageExtList.size(), 123, 2048, messageExtList, 0, outputStream.toByteArray());
}
 
Example #16
Source File: MQClientAPIImpl.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
private PullResult processPullResponse(
    final RemotingCommand response) throws MQBrokerException, RemotingCommandException {
    PullStatus pullStatus = PullStatus.NO_NEW_MSG;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS:
            pullStatus = PullStatus.FOUND;
            break;
        case ResponseCode.PULL_NOT_FOUND:
            pullStatus = PullStatus.NO_NEW_MSG;
            break;
        case ResponseCode.PULL_RETRY_IMMEDIATELY:
            pullStatus = PullStatus.NO_MATCHED_MSG;
            break;
        case ResponseCode.PULL_OFFSET_MOVED:
            pullStatus = PullStatus.OFFSET_ILLEGAL;
            break;

        default:
            throw new MQBrokerException(response.getCode(), response.getRemark());
    }

    PullMessageResponseHeader responseHeader =
        (PullMessageResponseHeader) response.decodeCommandCustomHeader(PullMessageResponseHeader.class);

    return new PullResultExt(pullStatus, responseHeader.getNextBeginOffset(), responseHeader.getMinOffset(),
        responseHeader.getMaxOffset(), null, responseHeader.getSuggestWhichBrokerId(), response.getBody());
}
 
Example #17
Source File: TransactionalMessageBridgeTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetHalfMessageStatusFound() {
    when(messageStore
            .getMessage(anyString(), anyString(), anyInt(), anyLong(), anyInt(), ArgumentMatchers.nullable(MessageFilter.class)))
            .thenReturn(createGetMessageResult(GetMessageStatus.FOUND));
    PullResult result = transactionBridge.getHalfMessage(0, 0, 1);
    assertThat(result.getPullStatus()).isEqualTo(PullStatus.FOUND);
}
 
Example #18
Source File: MQClientAPIImpl.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private PullResult processPullResponse(
    final RemotingCommand response) throws MQBrokerException, RemotingCommandException {
    PullStatus pullStatus = PullStatus.NO_NEW_MSG;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS:
            pullStatus = PullStatus.FOUND;
            break;
        case ResponseCode.PULL_NOT_FOUND:
            pullStatus = PullStatus.NO_NEW_MSG;
            break;
        case ResponseCode.PULL_RETRY_IMMEDIATELY:
            pullStatus = PullStatus.NO_MATCHED_MSG;
            break;
        case ResponseCode.PULL_OFFSET_MOVED:
            pullStatus = PullStatus.OFFSET_ILLEGAL;
            break;

        default:
            throw new MQBrokerException(response.getCode(), response.getRemark());
    }

    PullMessageResponseHeader responseHeader =
        (PullMessageResponseHeader) response.decodeCommandCustomHeader(PullMessageResponseHeader.class);

    return new PullResultExt(pullStatus, responseHeader.getNextBeginOffset(), responseHeader.getMinOffset(),
        responseHeader.getMaxOffset(), null, responseHeader.getSuggestWhichBrokerId(), response.getBody());
}
 
Example #19
Source File: ConsumerLagMonitor.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private long getRmqConsumeDelay(RocketMQProduceOffsetFetcher fetcher, MessageQueue mq, long produceOffset, long consumeOffset, String group) {
    try {
        if (produceOffset == 0L || produceOffset <= consumeOffset) {
            // 未生产 or 消费完毕.
            return 0L;
        }

        PullResult consumePullResult = fetcher.queryMsgByOffset(mq, consumeOffset);

        if (consumePullResult != null && consumePullResult.getPullStatus() == PullStatus.FOUND) {
            return TimeUtils.getElapseTime(consumePullResult.getMsgFoundList().get(0).getStoreTimestamp());
        } else if (consumePullResult.getPullStatus() == PullStatus.NO_MATCHED_MSG) {
            LOGGER.error("failed to getRmqConsumeDelay. mq: {}, namesvr: {}, group: {}, produceOffset: {}, consumeOffset: {}, consumePullResult: {}", mq, fetcher.getNamesrvAddr(), group, produceOffset, consumeOffset, consumePullResult);
            return 0; // 拿不到消息,认为没有延迟.
        } else if (consumePullResult.getPullStatus() == PullStatus.OFFSET_ILLEGAL) {
            LOGGER.error("failed to getRmqConsumeDelay. mq: {}, namesvr: {}, group: {}, produceOffset: {}, consumeOffset: {}, consumePullResult: {}", mq, fetcher.getNamesrvAddr(), group, produceOffset, consumeOffset, consumePullResult);

            PullResult pullResult = fetcher.queryMsgByOffset(mq, consumePullResult.getMinOffset());
            if (pullResult != null && pullResult.getPullStatus() == PullStatus.FOUND) {
                return TimeUtils.getElapseTime(pullResult.getMsgFoundList().get(0).getStoreTimestamp());
            }

            return MSG_NOT_FOUND;
        } else {
            return 0;
        }
    } catch (Exception e) {
        LOGGER.error("queryMsgByOffset failed,", e);
        return -1L;
    }
}
 
Example #20
Source File: DefaultMQConsumerWithTraceTest.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
@Before
public void init() throws Exception {
    consumerGroup = "FooBarGroup" + System.currentTimeMillis();
    pushConsumer = new DefaultMQPushConsumer(consumerGroup, true, "");
    consumerGroupNormal = "FooBarGroup" + System.currentTimeMillis();
    normalPushConsumer = new DefaultMQPushConsumer(consumerGroupNormal, false, "");
    customTraceTopicpushConsumer = new DefaultMQPushConsumer(consumerGroup, true, customerTraceTopic);
    pushConsumer.setNamesrvAddr("127.0.0.1:9876");
    pushConsumer.setPullInterval(60 * 1000);

    asyncTraceDispatcher = (AsyncTraceDispatcher) pushConsumer.getTraceDispatcher();
    traceProducer = asyncTraceDispatcher.getTraceProducer();

    pushConsumer.registerMessageListener(new MessageListenerConcurrently() {
        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
            ConsumeConcurrentlyContext context) {
            return null;
        }
    });

    PowerMockito.suppress(PowerMockito.method(DefaultMQPushConsumerImpl.class, "updateTopicSubscribeInfoWhenSubscriptionChanged"));
    DefaultMQPushConsumerImpl pushConsumerImpl = pushConsumer.getDefaultMQPushConsumerImpl();
    rebalancePushImpl = spy(new RebalancePushImpl(pushConsumer.getDefaultMQPushConsumerImpl()));
    Field field = DefaultMQPushConsumerImpl.class.getDeclaredField("rebalanceImpl");
    field.setAccessible(true);
    field.set(pushConsumerImpl, rebalancePushImpl);
    pushConsumer.subscribe(topic, "*");

    pushConsumer.start();

    mQClientFactory = spy(pushConsumerImpl.getmQClientFactory());
    mQClientTraceFactory = spy(pushConsumerImpl.getmQClientFactory());

    field = DefaultMQPushConsumerImpl.class.getDeclaredField("mQClientFactory");
    field.setAccessible(true);
    field.set(pushConsumerImpl, mQClientFactory);

    field = MQClientInstance.class.getDeclaredField("mQClientAPIImpl");
    field.setAccessible(true);
    field.set(mQClientFactory, mQClientAPIImpl);

    Field fieldTrace = DefaultMQProducerImpl.class.getDeclaredField("mQClientFactory");
    fieldTrace.setAccessible(true);
    fieldTrace.set(traceProducer.getDefaultMQProducerImpl(), mQClientTraceFactory);

    fieldTrace = MQClientInstance.class.getDeclaredField("mQClientAPIImpl");
    fieldTrace.setAccessible(true);
    fieldTrace.set(mQClientTraceFactory, mQClientTraceAPIImpl);

    pullAPIWrapper = spy(new PullAPIWrapper(mQClientFactory, consumerGroup, false));
    field = DefaultMQPushConsumerImpl.class.getDeclaredField("pullAPIWrapper");
    field.setAccessible(true);
    field.set(pushConsumerImpl, pullAPIWrapper);

    pushConsumer.getDefaultMQPushConsumerImpl().getRebalanceImpl().setmQClientFactory(mQClientFactory);
    mQClientFactory.registerConsumer(consumerGroup, pushConsumerImpl);

    when(mQClientFactory.getMQClientAPIImpl().pullMessage(anyString(), any(PullMessageRequestHeader.class),
        anyLong(), any(CommunicationMode.class), nullable(PullCallback.class)))
        .thenAnswer(new Answer<Object>() {
            @Override
            public Object answer(InvocationOnMock mock) throws Throwable {
                PullMessageRequestHeader requestHeader = mock.getArgument(1);
                MessageClientExt messageClientExt = new MessageClientExt();
                messageClientExt.setTopic(topic);
                messageClientExt.setQueueId(0);
                messageClientExt.setMsgId("123");
                messageClientExt.setBody(new byte[] {'a'});
                messageClientExt.setOffsetMsgId("234");
                messageClientExt.setBornHost(new InetSocketAddress(8080));
                messageClientExt.setStoreHost(new InetSocketAddress(8080));
                PullResult pullResult = createPullResult(requestHeader, PullStatus.FOUND, Collections.<MessageExt>singletonList(messageClientExt));
                ((PullCallback) mock.getArgument(4)).onSuccess(pullResult);
                return pullResult;
            }
        });

    doReturn(new FindBrokerResult("127.0.0.1:10911", false)).when(mQClientFactory).findBrokerAddressInSubscribe(anyString(), anyLong(), anyBoolean());
    Set<MessageQueue> messageQueueSet = new HashSet<MessageQueue>();
    messageQueueSet.add(createPullRequest().getMessageQueue());
    pushConsumer.getDefaultMQPushConsumerImpl().updateTopicSubscribeInfo(topic, messageQueueSet);
}
 
Example #21
Source File: TransactionalMessageServiceImpl.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
/**
 * Read op message, parse op message, and fill removeMap
 *
 * @param removeMap Half message to be remove, key:halfOffset, value: opOffset.
 * @param opQueue Op message queue.
 * @param pullOffsetOfOp The begin offset of op message queue.
 * @param miniOffset The current minimum offset of half message queue.
 * @param doneOpOffset Stored op messages that have been processed.
 * @return Op message result.
 */
private PullResult fillOpRemoveMap(HashMap<Long, Long> removeMap,
    MessageQueue opQueue, long pullOffsetOfOp, long miniOffset, List<Long> doneOpOffset) {
    PullResult pullResult = pullOpMsg(opQueue, pullOffsetOfOp, 32);
    if (null == pullResult) {
        return null;
    }
    if (pullResult.getPullStatus() == PullStatus.OFFSET_ILLEGAL
        || pullResult.getPullStatus() == PullStatus.NO_MATCHED_MSG) {
        log.warn("The miss op offset={} in queue={} is illegal, pullResult={}", pullOffsetOfOp, opQueue,
            pullResult);
        transactionalMessageBridge.updateConsumeOffset(opQueue, pullResult.getNextBeginOffset());
        return pullResult;
    } else if (pullResult.getPullStatus() == PullStatus.NO_NEW_MSG) {
        log.warn("The miss op offset={} in queue={} is NO_NEW_MSG, pullResult={}", pullOffsetOfOp, opQueue,
            pullResult);
        return pullResult;
    }
    List<MessageExt> opMsg = pullResult.getMsgFoundList();
    if (opMsg == null) {
        log.warn("The miss op offset={} in queue={} is empty, pullResult={}", pullOffsetOfOp, opQueue, pullResult);
        return pullResult;
    }
    for (MessageExt opMessageExt : opMsg) {
        Long queueOffset = getLong(new String(opMessageExt.getBody(), TransactionalMessageUtil.charset));
        log.debug("Topic: {} tags: {}, OpOffset: {}, HalfOffset: {}", opMessageExt.getTopic(),
            opMessageExt.getTags(), opMessageExt.getQueueOffset(), queueOffset);
        if (TransactionalMessageUtil.REMOVETAG.equals(opMessageExt.getTags())) {
            if (queueOffset < miniOffset) {
                doneOpOffset.add(opMessageExt.getQueueOffset());
            } else {
                removeMap.put(queueOffset, opMessageExt.getQueueOffset());
            }
        } else {
            log.error("Found a illegal tag in opMessageExt= {} ", opMessageExt);
        }
    }
    log.debug("Remove map: {}", removeMap);
    log.debug("Done op list: {}", doneOpOffset);
    return pullResult;
}
 
Example #22
Source File: PullResultExt.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
public PullResultExt(PullStatus pullStatus, long nextBeginOffset, long minOffset, long maxOffset,
    List<MessageExt> msgFoundList, final long suggestWhichBrokerId, final byte[] messageBinary) {
    super(pullStatus, nextBeginOffset, minOffset, maxOffset, msgFoundList);
    this.suggestWhichBrokerId = suggestWhichBrokerId;
    this.messageBinary = messageBinary;
}
 
Example #23
Source File: TransactionalMessageBridgeTest.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetHalfMessage() {
    when(messageStore.getMessage(anyString(), anyString(), anyInt(), anyLong(), anyInt(),  ArgumentMatchers.nullable(MessageFilter.class))).thenReturn(createGetMessageResult(GetMessageStatus.NO_MESSAGE_IN_QUEUE));
    PullResult result = transactionBridge.getHalfMessage(0, 0, 1);
    assertThat(result.getPullStatus()).isEqualTo(PullStatus.NO_NEW_MSG);
}
 
Example #24
Source File: TransactionalMessageBridgeTest.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetOpMessage() {
    when(messageStore.getMessage(anyString(), anyString(), anyInt(), anyLong(), anyInt(),  ArgumentMatchers.nullable(MessageFilter.class))).thenReturn(createGetMessageResult(GetMessageStatus.NO_MESSAGE_IN_QUEUE));
    PullResult result = transactionBridge.getOpMessage(0, 0, 1);
    assertThat(result.getPullStatus()).isEqualTo(PullStatus.NO_NEW_MSG);
}
 
Example #25
Source File: PullResultExt.java    From rocketmq_trans_message with Apache License 2.0 4 votes vote down vote up
public PullResultExt(PullStatus pullStatus, long nextBeginOffset, long minOffset, long maxOffset,
    List<MessageExt> msgFoundList, final long suggestWhichBrokerId, final byte[] messageBinary) {
    super(pullStatus, nextBeginOffset, minOffset, maxOffset, msgFoundList);
    this.suggestWhichBrokerId = suggestWhichBrokerId;
    this.messageBinary = messageBinary;
}
 
Example #26
Source File: PullResultExt.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 4 votes vote down vote up
public PullResultExt(PullStatus pullStatus, long nextBeginOffset, long minOffset, long maxOffset,
    List<MessageExt> msgFoundList, final long suggestWhichBrokerId, final byte[] messageBinary) {
    super(pullStatus, nextBeginOffset, minOffset, maxOffset, msgFoundList);
    this.suggestWhichBrokerId = suggestWhichBrokerId;
    this.messageBinary = messageBinary;
}
 
Example #27
Source File: DLedgerProduceAndConsumeIT.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
@Test
public void testProduceAndConsume() throws Exception {
    String cluster = UUID.randomUUID().toString();
    String brokerName = UUID.randomUUID().toString();
    String selfId = "n0";
    String peers = String.format("n0-localhost:%d", nextPort());
    BrokerConfig brokerConfig = buildBrokerConfig(cluster, brokerName);
    MessageStoreConfig storeConfig = buildStoreConfig(brokerName, peers, selfId);
    BrokerController brokerController = IntegrationTestBase.createAndStartBroker(storeConfig, brokerConfig);
    Thread.sleep(3000);

    Assert.assertEquals(BrokerRole.SYNC_MASTER, storeConfig.getBrokerRole());


    String topic = UUID.randomUUID().toString();
    String consumerGroup = UUID.randomUUID().toString();
    IntegrationTestBase.initTopic(topic, BaseConf.nsAddr, cluster, 1);
    DefaultMQProducer producer = ProducerFactory.getRMQProducer(BaseConf.nsAddr);
    DefaultMQPullConsumer consumer = ConsumerFactory.getRMQPullConsumer(BaseConf.nsAddr, consumerGroup);

    for (int i = 0; i < 10; i++) {
        Message message = new Message();
        message.setTopic(topic);
        message.setBody(("Hello" + i).getBytes());
        SendResult sendResult = producer.send(message);
        Assert.assertEquals(SendStatus.SEND_OK, sendResult.getSendStatus());
        Assert.assertEquals(0, sendResult.getMessageQueue().getQueueId());
        Assert.assertEquals(brokerName, sendResult.getMessageQueue().getBrokerName());
        Assert.assertEquals(i, sendResult.getQueueOffset());
        Assert.assertNotNull(sendResult.getMsgId());
        Assert.assertNotNull(sendResult.getOffsetMsgId());
    }

    Thread.sleep(500);
    Assert.assertEquals(0, brokerController.getMessageStore().getMinOffsetInQueue(topic, 0));
    Assert.assertEquals(10, brokerController.getMessageStore().getMaxOffsetInQueue(topic, 0));

    MessageQueue messageQueue = new MessageQueue(topic, brokerName, 0);
    PullResult pullResult= consumer.pull(messageQueue, "*", 0, 32);
    Assert.assertEquals(PullStatus.FOUND, pullResult.getPullStatus());
    Assert.assertEquals(10, pullResult.getMsgFoundList().size());

    for (int i = 0; i < 10; i++) {
        MessageExt messageExt = pullResult.getMsgFoundList().get(i);
        Assert.assertEquals(i, messageExt.getQueueOffset());
        Assert.assertArrayEquals(("Hello" + i).getBytes(), messageExt.getBody());
    }

    producer.shutdown();
    consumer.shutdown();
    brokerController.shutdown();
}
 
Example #28
Source File: RocketMQMessageSource.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@Override
protected synchronized Object doReceive() {
	if (messageQueueChooser.getMessageQueues() == null
			|| messageQueueChooser.getMessageQueues().size() == 0) {
		return null;
	}
	try {
		int count = 0;
		while (count < messageQueueChooser.getMessageQueues().size()) {
			MessageQueue messageQueue;
			synchronized (this.consumerMonitor) {
				messageQueue = messageQueueChooser.choose();
				messageQueueChooser.increment();
			}

			long offset = consumer.fetchConsumeOffset(messageQueue,
					rocketMQConsumerProperties.getExtension().isFromStore());

			log.debug("topic='{}', group='{}', messageQueue='{}', offset now='{}'",
					this.topic, this.group, messageQueue, offset);

			PullResult pullResult;
			if (messageSelector != null) {
				pullResult = consumer.pull(messageQueue, messageSelector, offset, 1);
			}
			else {
				pullResult = consumer.pull(messageQueue, (String) null, offset, 1);
			}

			if (pullResult.getPullStatus() == PullStatus.FOUND) {
				List<MessageExt> messageExtList = pullResult.getMsgFoundList();

				Message message = RocketMQUtil
						.convertToSpringMessage(messageExtList.get(0));

				AcknowledgmentCallback ackCallback = this.ackCallbackFactory
						.createCallback(new RocketMQAckInfo(messageQueue, pullResult,
								consumer, offset));

				Message messageResult = MessageBuilder.fromMessage(message).setHeader(
						IntegrationMessageHeaderAccessor.ACKNOWLEDGMENT_CALLBACK,
						ackCallback).build();
				return messageResult;
			}
			else {
				log.debug("messageQueue='{}' PullResult='{}' with topic `{}`",
						messageQueueChooser.getMessageQueues(),
						pullResult.getPullStatus(), topic);
			}
			count++;
		}
	}
	catch (Exception e) {
		log.error("Consumer pull error: " + e.getMessage(), e);
	}
	return null;
}
 
Example #29
Source File: PullResultExt.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
public PullResultExt(PullStatus pullStatus, long nextBeginOffset, long minOffset, long maxOffset,
    List<MessageExt> msgFoundList, final long suggestWhichBrokerId, final byte[] messageBinary) {
    super(pullStatus, nextBeginOffset, minOffset, maxOffset, msgFoundList);
    this.suggestWhichBrokerId = suggestWhichBrokerId;
    this.messageBinary = messageBinary;
}
 
Example #30
Source File: Utils.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
/**
 * 检查消费
 *
 * @param nameSvr
 * @param address
 * @throws MQClientException
 * @throws NoSuchFieldException
 * @throws SecurityException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws InterruptedException
 * @throws RemotingException
 * @throws MQBrokerException
 */
public static long checkReceive(String cluster, String nameSvr, String address)
        throws MQClientException, NoSuchFieldException, SecurityException, IllegalArgumentException,
        IllegalAccessException, InterruptedException, RemotingException, MQBrokerException {

    DefaultMQPullConsumer consumer = getReceiveCheckConsumer(nameSvr, cluster, address);
    Field f1 = DefaultMQPullConsumerImpl.class.getDeclaredField("mQClientFactory");
    f1.setAccessible(true);

    MQClientInstance instance = (MQClientInstance) f1.get(consumer.getDefaultMQPullConsumerImpl());

    Field f = MQClientInstance.class.getDeclaredField("brokerAddrTable");
    f.setAccessible(true);

    Field f2 = MQClientInstance.class.getDeclaredField("scheduledExecutorService");
    f2.setAccessible(true);

    ScheduledExecutorService service = (ScheduledExecutorService) f2.get(instance);
    service.shutdown();
    service.awaitTermination(1000, TimeUnit.SECONDS);

    ConcurrentHashMap<String, HashMap<Long, String>> map = (ConcurrentHashMap<String, HashMap<Long, String>>) f.get(instance);
    HashMap<Long, String> addresses = new HashMap<>();
    addresses.put(0L, address);
    map.put("rmqmonitor_" + address, addresses);

    MessageQueue queue = new MessageQueue("SELF_TEST_TOPIC", "rmqmonitor_" + address, 0);

    boolean pullOk = false;
    long maxOffset = -1;
    for (int i = 0; i < 2; ++i) {
        try {
            maxOffset = consumer.getDefaultMQPullConsumerImpl().maxOffset(queue);
            PullResult result = consumer.pull(queue, "*", maxOffset > 100 ? maxOffset - 10 : 0, 1);
            if (result.getPullStatus() == PullStatus.FOUND) {
                pullOk = true;
                break;
            } else if(result.getPullStatus() == PullStatus.NO_NEW_MSG) {
                checkSend(cluster, nameSvr, address);
                continue;
            }

            logger.warn("pull result failed, PullResult={}, cluster={}, namesvr={}, address={}", result, cluster, nameSvr, address);
        } catch (Throwable e) {
            logger.error("pull exception, cluster={}, namesvr={}, address={}", cluster, nameSvr, address, e);
        }
        Thread.sleep(1000);
    }
    if (!pullOk) {
        logger.error(String.format("[AlarmPullErr] cluster=%s, broker=%s", cluster, address));
    } else {
        logger.info("AlarmPullCheck cluster={}, broker={}", cluster, address);
    }
    return maxOffset;
}