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

The following examples show how to use org.apache.rocketmq.client.consumer.PullResult. 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: 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 #2
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 #3
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 #4
Source File: PullAPIWrapper.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public PullResult pullKernelImpl(
    final MessageQueue mq,
    final String subExpression,
    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 {
    return pullKernelImpl(mq, subExpression, subVersion, offset, maxNums, sysFlag, commitOffset, brokerSuspendMaxTimeMillis,
        timeoutMillis, communicationMode, pullCallback, false);
}
 
Example #5
Source File: PullConsumerTest.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws MQClientException {
    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("please_rename_unique_group_name_5");
    consumer.start();

    try {
        MessageQueue mq = new MessageQueue();
        mq.setQueueId(0);
        mq.setTopic("TopicTest3");
        mq.setBrokerName("vivedeMacBook-Pro.local");

        long offset = 26;

        long beginTime = System.currentTimeMillis();
        PullResult pullResult = consumer.pullBlockIfNotFound(mq, null, offset, 32);
        System.out.printf("%s%n", System.currentTimeMillis() - beginTime);
        System.out.printf("%s%n", pullResult);
    } catch (Exception e) {
        e.printStackTrace();
    }

    consumer.shutdown();
}
 
Example #6
Source File: PullAPIWrapper.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
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 {
    return pullKernelImpl(mq, subExpression, expressionType, subVersion, offset, maxNums, sysFlag, commitOffset, brokerSuspendMaxTimeMillis,
        timeoutMillis, communicationMode, pullCallback, false);
}
 
Example #7
Source File: PullConsumerTest.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws MQClientException {
    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("please_rename_unique_group_name_5");
    consumer.setNamesrvAddr("127.0.0.1:9876");
    consumer.start();

    try {
        MessageQueue mq = new MessageQueue();
        mq.setQueueId(0);
        mq.setTopic("TopicTest3");
        mq.setBrokerName("vivedeMacBook-Pro.local");

        long offset = 26;

        long beginTime = System.currentTimeMillis();
        PullResult pullResult = consumer.pullBlockIfNotFound(mq, null, offset, 32);
        System.out.printf("%s%n", System.currentTimeMillis() - beginTime);
        System.out.printf("%s%n", pullResult);
    } catch (Exception e) {
        e.printStackTrace();
    }

    consumer.shutdown();
}
 
Example #8
Source File: PullConsumerTest.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws MQClientException {
    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("please_rename_unique_group_name_5");
    consumer.start();

    try {
        MessageQueue mq = new MessageQueue();
        mq.setQueueId(0);
        mq.setTopic("TopicTest3");
        mq.setBrokerName("vivedeMacBook-Pro.local");

        long offset = 26;

        long beginTime = System.currentTimeMillis();
        PullResult pullResult = consumer.pullBlockIfNotFound(mq, null, offset, 32);
        System.out.printf("%s%n", System.currentTimeMillis() - beginTime);
        System.out.printf("%s%n", pullResult);
    } catch (Exception e) {
        e.printStackTrace();
    }

    consumer.shutdown();
}
 
Example #9
Source File: PullConsumerTest.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws MQClientException {
    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("please_rename_unique_group_name_5");
    consumer.start();

    try {
        MessageQueue mq = new MessageQueue();
        mq.setQueueId(0);
        mq.setTopic("TopicTest3");
        mq.setBrokerName("vivedeMacBook-Pro.local");

        long offset = 26;

        long beginTime = System.currentTimeMillis();
        PullResult pullResult = consumer.pullBlockIfNotFound(mq, null, offset, 32);
        System.out.printf("%s%n", System.currentTimeMillis() - beginTime);
        System.out.printf("%s%n", pullResult);
    } catch (Exception e) {
        e.printStackTrace();
    }

    consumer.shutdown();
}
 
Example #10
Source File: PullAPIWrapper.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public PullResult pullKernelImpl(
    final MessageQueue mq,
    final String subExpression,
    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 {
    return pullKernelImpl(mq, subExpression, subVersion, offset, maxNums, sysFlag, commitOffset, brokerSuspendMaxTimeMillis,
        timeoutMillis, communicationMode, pullCallback, false);
}
 
Example #11
Source File: MQClientAPIImpl.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
/**
 * 拉取消息
 *
 * @param addr              broker地址
 * @param requestHeader     请求头
 * @param timeoutMillis     请求超时
 * @param communicationMode 通讯方式
 * @param pullCallback      回调
 * @return 消息。只有通讯模式为同步时,才返回结果,否则返回null。
 * @throws RemotingException 当远程调用发生异常时
 * @throws MQBrokerException 当 broker 发生异常时。只有通讯模式为同步时才会发生该异常。
 * @throws InterruptedException 当发生中断异常时
 */
public PullResult pullMessage(//
                              final String addr, //
                              final PullMessageRequestHeader requestHeader, //
                              final long timeoutMillis, //
                              final CommunicationMode communicationMode, //
                              final PullCallback pullCallback//
) throws RemotingException, MQBrokerException, InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.PULL_MESSAGE, requestHeader);

    switch (communicationMode) {
        case ONEWAY:
            assert false;
            return null;
        case ASYNC:
            this.pullMessageAsync(addr, request, timeoutMillis, pullCallback);
            return null;
        case SYNC:
            return this.pullMessageSync(addr, request, timeoutMillis);
        default:
            assert false;
            break;
    }

    return null;
}
 
Example #12
Source File: MQClientAPIImpl.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
private PullResult pullMessageSync(
        final String addr,
        final RemotingCommand request,
        final long timeoutMillis
    ) throws RemotingException, InterruptedException, MQBrokerException {
//        =》
        RemotingCommand response = this.remotingClient.invokeSync(addr, request, timeoutMillis);
        assert response != null;
//        =》
        return this.processPullResponse(response);
    }
 
Example #13
Source File: TransactionalMessageBridgeTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetHalfMessageNull() {
    when(messageStore
            .getMessage(anyString(), anyString(), anyInt(), anyLong(), anyInt(), ArgumentMatchers.nullable(MessageFilter.class)))
            .thenReturn(null);
    PullResult result = transactionBridge.getHalfMessage(0, 0, 1);
    assertThat(result).isNull();
}
 
Example #14
Source File: PullAPIWrapper.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public PullResult pullKernelImpl(
    final MessageQueue mq,
    final String subExpression,
    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,
    final boolean isSlaveFirst
) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
    return pullKernelImpl(
        mq,
        subExpression,
        ExpressionType.TAG,
        subVersion, offset,
        maxNums,
        sysFlag,
        commitOffset,
        brokerSuspendMaxTimeMillis,
        timeoutMillis,
        communicationMode,
        pullCallback,
        isSlaveFirst
    );
}
 
Example #15
Source File: PullConsumerImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private void registerPullTaskCallback(final String targetQueueName) {
    this.pullConsumerScheduleService.registerPullTaskCallback(targetQueueName, new PullTaskCallback() {
        @Override
        public void doPullTask(final MessageQueue mq, final PullTaskContext context) {
            MQPullConsumer consumer = context.getPullConsumer();
            try {
                long offset = localMessageCache.nextPullOffset(mq);

                PullResult pullResult = consumer.pull(mq, "*",
                    offset, localMessageCache.nextPullBatchNums());
                ProcessQueue pq = rocketmqPullConsumer.getDefaultMQPullConsumerImpl().getRebalanceImpl()
                    .getProcessQueueTable().get(mq);
                switch (pullResult.getPullStatus()) {
                    case FOUND:
                        if (pq != null) {
                            pq.putMessage(pullResult.getMsgFoundList());
                            for (final MessageExt messageExt : pullResult.getMsgFoundList()) {
                                localMessageCache.submitConsumeRequest(new ConsumeRequest(messageExt, mq, pq));
                            }
                        }
                        break;
                    default:
                        break;
                }
                localMessageCache.updatePullOffset(mq, pullResult.getNextBeginOffset());
            } catch (Exception e) {
                log.error("An error occurred in pull message process.", e);
            }
        }
    });
}
 
Example #16
Source File: PullConsumerWithNamespace.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private static void dealWithPullResult(PullResult pullResult) {
    if (null == pullResult || pullResult.getMsgFoundList().isEmpty()) {
        return;
    }
    pullResult.getMsgFoundList().stream().forEach(
        (msg) -> System.out.printf("Topic is:%s, msgId is:%s%n" , msg.getTopic(), msg.getMsgId()));
}
 
Example #17
Source File: TransactionalMessageServiceImpl.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
private GetResult getHalfMsg(MessageQueue messageQueue, long offset) {
    GetResult getResult = new GetResult();

    PullResult result = pullHalfMsg(messageQueue, offset, PULL_MSG_RETRY_NUMBER);
    getResult.setPullResult(result);
    List<MessageExt> messageExts = result.getMsgFoundList();
    if (messageExts == null) {
        return getResult;
    }
    getResult.setMsg(messageExts.get(0));
    return getResult;
}
 
Example #18
Source File: PullScheduleService.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws MQClientException {
    final MQPullConsumerScheduleService scheduleService = new MQPullConsumerScheduleService("GroupName1");

    scheduleService.setMessageModel(MessageModel.CLUSTERING);
    scheduleService.registerPullTaskCallback("TopicTest1", new PullTaskCallback() {

        @Override
        public void doPullTask(MessageQueue mq, PullTaskContext context) {
            MQPullConsumer consumer = context.getPullConsumer();
            try {

                long offset = consumer.fetchConsumeOffset(mq, false);
                if (offset < 0)
                    offset = 0;

                PullResult pullResult = consumer.pull(mq, "*", offset, 32);
                System.out.printf("%s%n", offset + "\t" + mq + "\t" + pullResult);
                switch (pullResult.getPullStatus()) {
                    case FOUND:
                        break;
                    case NO_MATCHED_MSG:
                        break;
                    case NO_NEW_MSG:
                    case OFFSET_ILLEGAL:
                        break;
                    default:
                        break;
                }
                consumer.updateConsumeOffset(mq, pullResult.getNextBeginOffset());

                context.setPullNextDelayTimeMillis(100);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    scheduleService.start();
}
 
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: TransactionalMessageServiceImplTest.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
private PullResult createImmunityPulResult(String topic, long queueOffset, String body, int size) {
    PullResult result = createPullResult(topic, queueOffset, body, size);
    List<MessageExt> msgs = result.getMsgFoundList();
    for (MessageExt msg : msgs) {
        msg.putUserProperty(MessageConst.PROPERTY_CHECK_IMMUNITY_TIME_IN_SECONDS, "0");
    }
    return result;
}
 
Example #21
Source File: ConsumeMessageCommand.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
private void pullMessageByQueue(MessageQueue mq, long minOffset, long maxOffset) {
    READQ:
    for (long offset = minOffset; offset <= maxOffset; ) {
        PullResult pullResult = null;
        try {
            pullResult = defaultMQPullConsumer.pull(mq, "*", offset, (int)(maxOffset - offset + 1));
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        if (pullResult != null) {
            offset = pullResult.getNextBeginOffset();
            switch (pullResult.getPullStatus()) {
                case FOUND:
                    System.out.print("Consume ok\n");
                    PrintMessageByQueueCommand.printMessage(pullResult.getMsgFoundList(), "UTF-8",
                        true, true);
                    break;
                case NO_MATCHED_MSG:
                    System.out.printf("%s no matched msg. status=%s, offset=%s\n", mq, pullResult.getPullStatus(),
                        offset);
                    break;
                case NO_NEW_MSG:
                case OFFSET_ILLEGAL:
                    System.out.printf("%s print msg finished. status=%s, offset=%s\n", mq,
                        pullResult.getPullStatus(), offset);
                    break READQ;
                default:
                    break;
            }
        }
    }
}
 
Example #22
Source File: PullConsumer.java    From blog with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void main(String[] args) throws MQClientException {
    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("please_rename_unique_group_name_5");
    consumer.setNamesrvAddr("127.0.0.1:9876");
    consumer.start();

    Set<MessageQueue> mqs = consumer.fetchSubscribeMessageQueues("broker-a");
    for (MessageQueue mq : mqs) {
        System.out.printf("Consume from the queue: %s%n", mq);
        SINGLE_MQ:
        while (true) {
            try {
                PullResult pullResult =
                    consumer.pullBlockIfNotFound(mq, null, getMessageQueueOffset(mq), 32);
                System.out.printf("%s%n", pullResult);
                putMessageQueueOffset(mq, pullResult.getNextBeginOffset());
                switch (pullResult.getPullStatus()) {
                    case FOUND:
                        break;
                    case NO_MATCHED_MSG:
                        break;
                    case NO_NEW_MSG:
                        break SINGLE_MQ;
                    case OFFSET_ILLEGAL:
                        break;
                    default:
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    consumer.shutdown();
}
 
Example #23
Source File: PullConsumerImpl.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
private void registerPullTaskCallback(final String targetQueueName) {
    this.pullConsumerScheduleService.registerPullTaskCallback(targetQueueName, new PullTaskCallback() {
        @Override
        public void doPullTask(final MessageQueue mq, final PullTaskContext context) {
            MQPullConsumer consumer = context.getPullConsumer();
            try {
                long offset = localMessageCache.nextPullOffset(mq);

                PullResult pullResult = consumer.pull(mq, "*",
                    offset, localMessageCache.nextPullBatchNums());
                ProcessQueue pq = rocketmqPullConsumer.getDefaultMQPullConsumerImpl().getRebalanceImpl()
                    .getProcessQueueTable().get(mq);
                switch (pullResult.getPullStatus()) {
                    case FOUND:
                        if (pq != null) {
                            pq.putMessage(pullResult.getMsgFoundList());
                            for (final MessageExt messageExt : pullResult.getMsgFoundList()) {
                                localMessageCache.submitConsumeRequest(new ConsumeRequest(messageExt, mq, pq));
                            }
                        }
                        break;
                    default:
                        break;
                }
                localMessageCache.updatePullOffset(mq, pullResult.getNextBeginOffset());
            } catch (Exception e) {
                log.error("A error occurred in pull message process.", e);
            }
        }
    });
}
 
Example #24
Source File: PullScheduleService.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws MQClientException {
    final MQPullConsumerScheduleService scheduleService = new MQPullConsumerScheduleService("GroupName1");

    scheduleService.setMessageModel(MessageModel.CLUSTERING);
    scheduleService.registerPullTaskCallback("TopicTest1", new PullTaskCallback() {

        @Override
        public void doPullTask(MessageQueue mq, PullTaskContext context) {
            MQPullConsumer consumer = context.getPullConsumer();
            try {

                long offset = consumer.fetchConsumeOffset(mq, false);
                if (offset < 0)
                    offset = 0;

                PullResult pullResult = consumer.pull(mq, "*", offset, 32);
                System.out.printf("%s%n", offset + "\t" + mq + "\t" + pullResult);
                switch (pullResult.getPullStatus()) {
                    case FOUND:
                        break;
                    case NO_MATCHED_MSG:
                        break;
                    case NO_NEW_MSG:
                    case OFFSET_ILLEGAL:
                        break;
                    default:
                        break;
                }
                consumer.updateConsumeOffset(mq, pullResult.getNextBeginOffset());

                context.setPullNextDelayTimeMillis(100);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    scheduleService.start();
}
 
Example #25
Source File: PullAPIWrapper.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
/**
 * 拉取消息
 * @param mq mq
 * @param subExpression tag
 * @param subVersion version
 * @param offset 偏移量
 * @param maxNums 最大量
 * @param sysFlag 系统flag
 * @param commitOffset commitoffset
 * @param brokerSuspendMaxTimeMillis broker挂起的最大时间
 * @param timeoutMillis 超时时间
 * @param communicationMode 模式
 * @param pullCallback 异步callback
 * @return ;
 * @throws MQClientException ;
 * @throws RemotingException ;
 * @throws MQBrokerException ;
 * @throws InterruptedException ;
 */
public PullResult pullKernelImpl(
    final MessageQueue mq,
    final String subExpression,
    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 {
    return pullKernelImpl(
        mq,
        subExpression,
        ExpressionType.TAG,
        subVersion,
            offset,
        maxNums,
        sysFlag,
        commitOffset,
        brokerSuspendMaxTimeMillis,
        timeoutMillis,
        communicationMode,
        pullCallback
    );
}
 
Example #26
Source File: MQClientAPIImpl.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
/**
 * 执行远程调用,拉取消息
 * @param addr addr
 * @param requestHeader requestHeader
 * @param timeoutMillis 超时时间
 * @param communicationMode 模式
 * @param pullCallback pulllcallback
 * @return ;
 * @throws RemotingException ;
 * @throws MQBrokerException ;
 * @throws InterruptedException ;
 */
public PullResult pullMessage(
    final String addr,
    final PullMessageRequestHeader requestHeader,
    final long timeoutMillis,
    final CommunicationMode communicationMode,
    final PullCallback pullCallback
) throws RemotingException, MQBrokerException, InterruptedException {

    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.PULL_MESSAGE, requestHeader);

    String acturallyAddr = getActurallyBrokerAddr(addr);
    switch (communicationMode) {
        case ONEWAY:
            assert false;
            return null;
        case ASYNC:
            this.pullMessageAsync(acturallyAddr, request, timeoutMillis, pullCallback);
            return null;
        case SYNC:
            return this.pullMessageSync(acturallyAddr, request, timeoutMillis);
        default:
            assert false;
            break;
    }

    return null;
}
 
Example #27
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 #28
Source File: PullAPIWrapper.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public PullResult pullKernelImpl(
    final MessageQueue mq,
    final String subExpression,
    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,
    final boolean isSlaveFirst
) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
    return pullKernelImpl(
        mq,
        subExpression,
        ExpressionType.TAG,
        subVersion, offset,
        maxNums,
        sysFlag,
        commitOffset,
        brokerSuspendMaxTimeMillis,
        timeoutMillis,
        communicationMode,
        pullCallback,
        isSlaveFirst
    );
}
 
Example #29
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 #30
Source File: TransactionalMessageServiceImplTest.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
private PullResult createImmunityPulResult(String topic, long queueOffset, String body, int size) {
    PullResult result = createPullResult(topic, queueOffset, body, size);
    List<MessageExt> msgs = result.getMsgFoundList();
    for (MessageExt msg : msgs) {
        msg.putUserProperty(MessageConst.PROPERTY_CHECK_IMMUNITY_TIME_IN_SECONDS, "0");
    }
    return result;
}