Search in sources :

Example 1 with PullMessageRequestHeader

use of org.apache.rocketmq.common.protocol.header.PullMessageRequestHeader in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class DefaultMQPullConsumerTest method testPullMessage_Success.

@Test
public void testPullMessage_Success() throws Exception {
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock mock) throws Throwable {
            PullMessageRequestHeader requestHeader = mock.getArgument(1);
            return createPullResult(requestHeader, PullStatus.FOUND, Collections.singletonList(new MessageExt()));
        }
    }).when(mQClientAPIImpl).pullMessage(anyString(), any(PullMessageRequestHeader.class), anyLong(), any(CommunicationMode.class), nullable(PullCallback.class));
    MessageQueue messageQueue = new MessageQueue(topic, brokerName, 0);
    PullResult pullResult = pullConsumer.pull(messageQueue, "*", 1024, 3);
    assertThat(pullResult).isNotNull();
    assertThat(pullResult.getPullStatus()).isEqualTo(PullStatus.FOUND);
    assertThat(pullResult.getNextBeginOffset()).isEqualTo(1024 + 1);
    assertThat(pullResult.getMinOffset()).isEqualTo(123);
    assertThat(pullResult.getMaxOffset()).isEqualTo(2048);
    assertThat(pullResult.getMsgFoundList()).isEqualTo(new ArrayList<Object>());
}
Also used : PullMessageRequestHeader(org.apache.rocketmq.common.protocol.header.PullMessageRequestHeader) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) MessageExt(org.apache.rocketmq.common.message.MessageExt) CommunicationMode(org.apache.rocketmq.client.impl.CommunicationMode) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 2 with PullMessageRequestHeader

use of org.apache.rocketmq.common.protocol.header.PullMessageRequestHeader in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class DefaultMQPullConsumerTest method testPullMessage_NotFound.

@Test
public void testPullMessage_NotFound() throws Exception {
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock mock) throws Throwable {
            PullMessageRequestHeader requestHeader = mock.getArgument(1);
            return createPullResult(requestHeader, PullStatus.NO_NEW_MSG, new ArrayList<MessageExt>());
        }
    }).when(mQClientAPIImpl).pullMessage(anyString(), any(PullMessageRequestHeader.class), anyLong(), any(CommunicationMode.class), nullable(PullCallback.class));
    MessageQueue messageQueue = new MessageQueue(topic, brokerName, 0);
    PullResult pullResult = pullConsumer.pull(messageQueue, "*", 1024, 3);
    assertThat(pullResult.getPullStatus()).isEqualTo(PullStatus.NO_NEW_MSG);
}
Also used : PullMessageRequestHeader(org.apache.rocketmq.common.protocol.header.PullMessageRequestHeader) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) CommunicationMode(org.apache.rocketmq.client.impl.CommunicationMode) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 3 with PullMessageRequestHeader

use of org.apache.rocketmq.common.protocol.header.PullMessageRequestHeader in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class PullAPIWrapper method pullKernelImpl.

// 拉数据核心
public PullResult pullKernelImpl(final MessageQueue mq, final String subExpression, final String expressionType, final long subVersion, final long offset, final int maxNums, final int sysFlag, final long commitOffset, final long brokerSuspendMaxTimeMillis, final long timeoutMillis, final CommunicationMode communicationMode, final PullCallback pullCallback) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
    FindBrokerResult findBrokerResult = this.mQClientFactory.findBrokerAddressInSubscribe(mq.getBrokerName(), this.recalculatePullFromWhichNode(mq), false);
    if (null == findBrokerResult) {
        this.mQClientFactory.updateTopicRouteInfoFromNameServer(mq.getTopic());
        findBrokerResult = this.mQClientFactory.findBrokerAddressInSubscribe(mq.getBrokerName(), this.recalculatePullFromWhichNode(mq), false);
    }
    if (findBrokerResult != null) {
        {
            // check version
            if (!ExpressionType.isTagType(expressionType) && findBrokerResult.getBrokerVersion() < MQVersion.Version.V4_1_0_SNAPSHOT.ordinal()) {
                throw new MQClientException("The broker[" + mq.getBrokerName() + ", " + findBrokerResult.getBrokerVersion() + "] does not upgrade to support for filter message by " + expressionType, null);
            }
        }
        int sysFlagInner = sysFlag;
        if (findBrokerResult.isSlave()) {
            sysFlagInner = PullSysFlag.clearCommitOffsetFlag(sysFlagInner);
        }
        // 构建PullMessageRequestHeader头信息
        PullMessageRequestHeader requestHeader = new PullMessageRequestHeader();
        requestHeader.setConsumerGroup(this.consumerGroup);
        requestHeader.setTopic(mq.getTopic());
        requestHeader.setQueueId(mq.getQueueId());
        requestHeader.setQueueOffset(offset);
        requestHeader.setMaxMsgNums(maxNums);
        requestHeader.setSysFlag(sysFlagInner);
        requestHeader.setCommitOffset(commitOffset);
        requestHeader.setSuspendTimeoutMillis(brokerSuspendMaxTimeMillis);
        requestHeader.setSubscription(subExpression);
        requestHeader.setSubVersion(subVersion);
        requestHeader.setExpressionType(expressionType);
        String brokerAddr = findBrokerResult.getBrokerAddr();
        if (PullSysFlag.hasClassFilterFlag(sysFlagInner)) {
            brokerAddr = computPullFromWhichFilterServer(mq.getTopic(), brokerAddr);
        }
        // 请求拉取
        PullResult pullResult = this.mQClientFactory.getMQClientAPIImpl().pullMessage(brokerAddr, requestHeader, timeoutMillis, communicationMode, pullCallback);
        return pullResult;
    }
    throw new MQClientException("The broker[" + mq.getBrokerName() + "] not exist", null);
}
Also used : PullMessageRequestHeader(org.apache.rocketmq.common.protocol.header.PullMessageRequestHeader) FindBrokerResult(org.apache.rocketmq.client.impl.FindBrokerResult) MQClientException(org.apache.rocketmq.client.exception.MQClientException) PullResult(org.apache.rocketmq.client.consumer.PullResult)

Example 4 with PullMessageRequestHeader

use of org.apache.rocketmq.common.protocol.header.PullMessageRequestHeader in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class PullMessageProcessorTest method createPullMsgCommand.

private RemotingCommand createPullMsgCommand(int requestCode) {
    PullMessageRequestHeader requestHeader = new PullMessageRequestHeader();
    requestHeader.setCommitOffset(123L);
    requestHeader.setConsumerGroup(group);
    requestHeader.setMaxMsgNums(100);
    requestHeader.setQueueId(1);
    requestHeader.setQueueOffset(456L);
    requestHeader.setSubscription("*");
    requestHeader.setTopic(topic);
    requestHeader.setSysFlag(0);
    requestHeader.setSubVersion(100L);
    RemotingCommand request = RemotingCommand.createRequestCommand(requestCode, requestHeader);
    request.makeCustomHeaderToNet();
    return request;
}
Also used : PullMessageRequestHeader(org.apache.rocketmq.common.protocol.header.PullMessageRequestHeader) RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand)

Example 5 with PullMessageRequestHeader

use of org.apache.rocketmq.common.protocol.header.PullMessageRequestHeader in project rocketmq by apache.

the class PullAPIWrapper method pullKernelImpl.

public PullResult pullKernelImpl(final MessageQueue mq, final String subExpression, final String expressionType, final long subVersion, final long offset, final int maxNums, final int sysFlag, final long commitOffset, final long brokerSuspendMaxTimeMillis, final long timeoutMillis, final CommunicationMode communicationMode, final PullCallback pullCallback) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
    FindBrokerResult findBrokerResult = this.mQClientFactory.findBrokerAddressInSubscribe(mq.getBrokerName(), this.recalculatePullFromWhichNode(mq), false);
    if (null == findBrokerResult) {
        this.mQClientFactory.updateTopicRouteInfoFromNameServer(mq.getTopic());
        findBrokerResult = this.mQClientFactory.findBrokerAddressInSubscribe(mq.getBrokerName(), this.recalculatePullFromWhichNode(mq), false);
    }
    if (findBrokerResult != null) {
        {
            // check version
            if (!ExpressionType.isTagType(expressionType) && findBrokerResult.getBrokerVersion() < MQVersion.Version.V4_1_0_SNAPSHOT.ordinal()) {
                throw new MQClientException("The broker[" + mq.getBrokerName() + ", " + findBrokerResult.getBrokerVersion() + "] does not upgrade to support for filter message by " + expressionType, null);
            }
        }
        int sysFlagInner = sysFlag;
        if (findBrokerResult.isSlave()) {
            sysFlagInner = PullSysFlag.clearCommitOffsetFlag(sysFlagInner);
        }
        PullMessageRequestHeader requestHeader = new PullMessageRequestHeader();
        requestHeader.setConsumerGroup(this.consumerGroup);
        requestHeader.setTopic(mq.getTopic());
        requestHeader.setQueueId(mq.getQueueId());
        requestHeader.setQueueOffset(offset);
        requestHeader.setMaxMsgNums(maxNums);
        requestHeader.setSysFlag(sysFlagInner);
        requestHeader.setCommitOffset(commitOffset);
        requestHeader.setSuspendTimeoutMillis(brokerSuspendMaxTimeMillis);
        requestHeader.setSubscription(subExpression);
        requestHeader.setSubVersion(subVersion);
        requestHeader.setExpressionType(expressionType);
        String brokerAddr = findBrokerResult.getBrokerAddr();
        if (PullSysFlag.hasClassFilterFlag(sysFlagInner)) {
            brokerAddr = computPullFromWhichFilterServer(mq.getTopic(), brokerAddr);
        }
        PullResult pullResult = this.mQClientFactory.getMQClientAPIImpl().pullMessage(brokerAddr, requestHeader, timeoutMillis, communicationMode, pullCallback);
        return pullResult;
    }
    throw new MQClientException("The broker[" + mq.getBrokerName() + "] not exist", null);
}
Also used : PullMessageRequestHeader(org.apache.rocketmq.common.protocol.header.PullMessageRequestHeader) FindBrokerResult(org.apache.rocketmq.client.impl.FindBrokerResult) MQClientException(org.apache.rocketmq.client.exception.MQClientException) PullResult(org.apache.rocketmq.client.consumer.PullResult)

Aggregations

PullMessageRequestHeader (org.apache.rocketmq.common.protocol.header.PullMessageRequestHeader)16 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)12 CommunicationMode (org.apache.rocketmq.client.impl.CommunicationMode)8 MessageExt (org.apache.rocketmq.common.message.MessageExt)8 InvocationOnMock (org.mockito.invocation.InvocationOnMock)8 ArrayList (java.util.ArrayList)6 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)6 Test (org.junit.Test)6 Mockito.doAnswer (org.mockito.Mockito.doAnswer)6 Answer (org.mockito.stubbing.Answer)6 PullResult (org.apache.rocketmq.client.consumer.PullResult)4 FindBrokerResult (org.apache.rocketmq.client.impl.FindBrokerResult)4 PullMessageResponseHeader (org.apache.rocketmq.common.protocol.header.PullMessageResponseHeader)4 ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelFutureListener (io.netty.channel.ChannelFutureListener)2 FileRegion (io.netty.channel.FileRegion)2 Field (java.lang.reflect.Field)2 InetSocketAddress (java.net.InetSocketAddress)2 HashSet (java.util.HashSet)2 ConsumerGroupInfo (org.apache.rocketmq.broker.client.ConsumerGroupInfo)2