com.alibaba.rocketmq.client.consumer.PullCallback Java Examples

The following examples show how to use com.alibaba.rocketmq.client.consumer.PullCallback. 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 reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 6 votes vote down vote up
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 #2
Source File: RocketmqIT.java    From uavstack with Apache License 2.0 6 votes vote down vote up
public void doAsyncCap(int rc, PullCallback target, Method method) {

            Map<String, Object> params = new HashMap<String, Object>();

            params.put(CaptureConstants.INFO_CLIENT_TARGETSERVER, targetServer);
            params.put(CaptureConstants.INFO_CLIENT_RESPONSECODE, rc);

            if (logger.isDebugable()) {
                logger.debug("Invoke END: rc=" + rc + "," + targetServer, null);
            }

            UAVServer.instance().runMonitorAsyncCaptureOnServerCapPoint(CaptureConstants.CAPPOINT_APP_CLIENT,
                    Monitor.CapturePhase.DOCAP, params, ccMap);

            if (ivcContext != null) {
                ivcContext.putAll(params);
            }
            captureInvokeChain(RocketMQPullConsumerAdapter.class, InvokeChainConstants.CapturePhase.DOCAP, ivcContext,
                    null);
        }
 
Example #3
Source File: MQClientAPIImpl.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
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 #4
Source File: MQClientAPIImpl.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
/**
 * 异步拉取消息
 * 
 * @param addr
 * @param request
 * @param timeoutMillis
 * @param pullCallback
 * @throws RemotingException
 * @throws InterruptedException
 */
private void pullMessageAsync(final String addr, final RemotingCommand request, final long timeoutMillis,
        final PullCallback pullCallback) throws RemotingException, InterruptedException {
    this.remotingClient.invokeAsync(addr, request, timeoutMillis, new InvokeCallback() {
        @Override
        public void operationComplete(ResponseFuture responseFuture) {
            RemotingCommand response = responseFuture.getResponseCommand();
            if (response != null) {
                try {
                    PullResult pullResult = MQClientAPIImpl.this.processPullResponse(response);
                    assert pullResult != null;
                    pullCallback.onSuccess(pullResult);
                }
                catch (Exception e) {
                    pullCallback.onException(e);
                }
            }
            else {
                if (!responseFuture.isSendRequestOK()) {
                    pullCallback.onException(
                        new MQClientException("send request failed", responseFuture.getCause()));
                }
                else if (responseFuture.isTimeout()) {
                    pullCallback.onException(new MQClientException(
                        "wait response timeout " + responseFuture.getTimeoutMillis() + "ms",
                        responseFuture.getCause()));
                }
                else {
                    pullCallback
                        .onException(new MQClientException("unknow reseaon", responseFuture.getCause()));
                }
            }
        }
    });
}
 
Example #5
Source File: MQClientAPIImpl.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
/**
 * 客户端拉取消息通用方法
 * 
 * @param addr
 * @param requestHeader
 * @param timeoutMillis
 * @param communicationMode
 * @param pullCallback
 * @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 {
    if (!UtilAll.isBlank(projectGroupPrefix)) {
        requestHeader.setConsumerGroup(
            VirtualEnvUtil.buildWithProjectGroup(requestHeader.getConsumerGroup(), projectGroupPrefix));
        requestHeader
            .setTopic(VirtualEnvUtil.buildWithProjectGroup(requestHeader.getTopic(), projectGroupPrefix));
    }

    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 #6
Source File: MQClientAPIImpl.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 5 votes vote down vote up
private void pullMessageAsync(//
                              final String addr,// 1
                              final RemotingCommand request,//
                              final long timeoutMillis,//
                              final PullCallback pullCallback//
) throws RemotingException, InterruptedException {
    //this.remotingClient = new NettyRemotingClient(),这里实际执行 NettyRemotingClient.invokeAsync
    this.remotingClient.invokeAsync(addr, request, timeoutMillis, new InvokeCallback() {
        @Override
        public void operationComplete(ResponseFuture responseFuture) {

            RemotingCommand response = responseFuture.getResponseCommand();
            if (response != null) {
                try {
                    PullResult pullResult = MQClientAPIImpl.this.processPullResponse(response);
                    assert pullResult != null;
                    //DefaultMQPushConsumerImpl.pullMessage(pullCallback.onSuccess)中的回调执行
                    pullCallback.onSuccess(pullResult);
                }
                catch (Exception e) {
                    pullCallback.onException(e);
                }
            }
            else {
                if (!responseFuture.isSendRequestOK()) {
                    pullCallback.onException(new MQClientException("send request failed", responseFuture.getCause()));
                }
                else if (responseFuture.isTimeout()) {
                    pullCallback.onException(new MQClientException("wait response timeout " + responseFuture.getTimeoutMillis() + "ms",
                        responseFuture.getCause()));
                }
                else {
                    pullCallback.onException(new MQClientException("unknow reseaon", responseFuture.getCause()));
                }
            }
        }
    });
}
 
Example #7
Source File: RocketmqIT.java    From uavstack with Apache License 2.0 5 votes vote down vote up
@Override
public Object postProcess(Object res, PullCallback t, Object proxy, Method method, Object[] args) {

    if (method.getName().equals("onSuccess")) {
        doAsyncCap(1, t, method);
    }
    else if (method.getName().equals("onException")) {
        doAsyncCap(0, t, method);
    }

    return res;
}
 
Example #8
Source File: MQClientAPIImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private void pullMessageAsync(//
                              final String addr, // 1
                              final RemotingCommand request, //
                              final long timeoutMillis, //
                              final PullCallback pullCallback//
) throws RemotingException, InterruptedException {
    this.remotingClient.invokeAsync(addr, request, timeoutMillis, new InvokeCallback() {
        @Override
        public void operationComplete(ResponseFuture responseFuture) {
            RemotingCommand response = responseFuture.getResponseCommand();
            if (response != null) {
                try {
                    PullResult pullResult = MQClientAPIImpl.this.processPullResponse(response);
                    assert pullResult != null;
                    pullCallback.onSuccess(pullResult);
                } catch (Exception e) {
                    pullCallback.onException(e);
                }
            } else {
                if (!responseFuture.isSendRequestOK()) {
                    pullCallback.onException(new MQClientException("send request failed", responseFuture.getCause()));
                } else if (responseFuture.isTimeout()) {
                    pullCallback.onException(new MQClientException("wait response timeout " + responseFuture.getTimeoutMillis() + "ms",
                            responseFuture.getCause()));
                } else {
                    pullCallback.onException(new MQClientException("unknow reseaon", responseFuture.getCause()));
                }
            }
        }
    });
}
 
Example #9
Source File: DefaultMQPullConsumerImpl.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
public void pull(MessageQueue mq, String subExpression, long offset, int maxNums, PullCallback pullCallback, long timeout)
        throws MQClientException, RemotingException, InterruptedException {
    this.pullAsyncImpl(mq, subExpression, offset, maxNums, pullCallback, false, timeout);
}
 
Example #10
Source File: PullAPIWrapper.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
public PullResult pullKernelImpl(//
        final MessageQueue mq, // 1
        final String subExpression, // 2
        final long subVersion, // 3
        final long offset, // 4
        final int maxNums, // 5
        final int sysFlag, // 6
        final long commitOffset, // 7
        final long brokerSuspendMaxTimeMillis, // 8
        final long timeoutMillis, // 9
        final CommunicationMode communicationMode, // 10
        final PullCallback pullCallback// 11
) 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) {
        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);

        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);
}
 
Example #11
Source File: DefaultMQPullConsumerImpl.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
public void pullBlockIfNotFound(MessageQueue mq, String subExpression, long offset, int maxNums,
        PullCallback pullCallback) throws MQClientException, RemotingException, InterruptedException {
    this.pullAsyncImpl(mq, subExpression, offset, maxNums, pullCallback, true,
        this.getDefaultMQPullConsumer().getConsumerPullTimeoutMillis());
}
 
Example #12
Source File: DefaultMQPullConsumerImpl.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
public void pull(MessageQueue mq, String subExpression, long offset, int maxNums,
        PullCallback pullCallback, long timeout)
                throws MQClientException, RemotingException, InterruptedException {
    this.pullAsyncImpl(mq, subExpression, offset, maxNums, pullCallback, false, timeout);
}
 
Example #13
Source File: DefaultMQPullConsumerImpl.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
public void pull(MessageQueue mq, String subExpression, long offset, int maxNums,
        PullCallback pullCallback) throws MQClientException, RemotingException, InterruptedException {
    pull(mq, subExpression, offset, maxNums, pullCallback,
        this.defaultMQPullConsumer.getConsumerPullTimeoutMillis());
}
 
Example #14
Source File: PullAPIWrapper.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
public PullResult pullKernelImpl(//
                                 final MessageQueue mq,// 1
                                 final String subExpression,// 2
                                 final long subVersion,// 3
                                 final long offset,// 4
                                 final int maxNums,// 5
                                 final int sysFlag,// 6
                                 final long commitOffset,// 7
                                 final long brokerSuspendMaxTimeMillis,// 8
                                 final long timeoutMillis,// 9
                                 final CommunicationMode communicationMode,// 10
                                 final PullCallback pullCallback// 11
) 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) {
        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);

        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);
}
 
Example #15
Source File: DefaultMQPullConsumerImpl.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
public void pullBlockIfNotFound(MessageQueue mq, String subExpression, long offset, int maxNums, PullCallback pullCallback)
        throws MQClientException, RemotingException, InterruptedException {
    this.pullAsyncImpl(mq, subExpression, offset, maxNums, pullCallback, true,
            this.getDefaultMQPullConsumer().getConsumerPullTimeoutMillis());
}
 
Example #16
Source File: DefaultMQPullConsumerImpl.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 4 votes vote down vote up
public void pull(MessageQueue mq, String subExpression, long offset, int maxNums,
                 PullCallback pullCallback) throws MQClientException, RemotingException, InterruptedException {
    pull(mq, subExpression, offset, maxNums, pullCallback,
            this.defaultMQPullConsumer.getConsumerPullTimeoutMillis());
}
 
Example #17
Source File: DefaultMQPullConsumerImpl.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
public void pull(MessageQueue mq, String subExpression, long offset, int maxNums, PullCallback pullCallback)
        throws MQClientException, RemotingException, InterruptedException {
    pull(mq, subExpression, offset, maxNums, pullCallback, this.defaultMQPullConsumer.getConsumerPullTimeoutMillis());
}
 
Example #18
Source File: PullAPIWrapper.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 4 votes vote down vote up
public PullResult pullKernelImpl(//
        final MessageQueue mq,// 1
        final String subExpression,// 2
        final long subVersion,// 3
        final long offset,// 4
        final int maxNums,// 5
        final int sysFlag,// 6
        final long commitOffset,// 7
        final long brokerSuspendMaxTimeMillis,// 8
        final long timeoutMillis,// 9
        final CommunicationMode communicationMode,// 10
        final PullCallback pullCallback// 11
) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
    FindBrokerResult findBrokerResult =
            this.mQClientFactory.findBrokerAddressInSubscribe(mq.getBrokerName(),
                this.recalculatePullFromWhichNode(mq), false);
    if (null == findBrokerResult) {
        //PullAPIWrapper.pullKernelImpl中调用updateTopicRouteInfoFromNameServer进行TopicRoute信息更新,最终保存在MQClientInstance.topicRouteTable
        this.mQClientFactory.updateTopicRouteInfoFromNameServer(mq.getTopic());
        findBrokerResult =
                this.mQClientFactory.findBrokerAddressInSubscribe(mq.getBrokerName(),
                    this.recalculatePullFromWhichNode(mq), false);
    }

    if (findBrokerResult != null) {
        int sysFlagInner = sysFlag;

        if (findBrokerResult.isSlave()) { //拉取broker选择了slave, 清理掉提交消费位点的系统标识位。
            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);

        String brokerAddr = findBrokerResult.getBrokerAddr();
        if (PullSysFlag.hasClassFilterFlag(sysFlagInner)) {
            brokerAddr = computPullFromWhichFilterServer(mq.getTopic(), brokerAddr);
        }

        //开始从broker对应的brokerAddr拉取消息
        PullResult pullResult = this.mQClientFactory.getMQClientAPIImpl().pullMessage(//
                brokerAddr,//
                requestHeader,//
                timeoutMillis,//
                communicationMode,//同步从broker拉取消息还是异步拉取消息
                pullCallback);

        return pullResult;
    }

    throw new MQClientException("The broker[" + mq.getBrokerName() + "] not exist", null);
}
 
Example #19
Source File: DefaultMQPullConsumerImpl.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 4 votes vote down vote up
public void pullBlockIfNotFound(MessageQueue mq, String subExpression, long offset, int maxNums,
                                PullCallback pullCallback) throws MQClientException, RemotingException, InterruptedException {
    this.pullAsyncImpl(mq, subExpression, offset, maxNums, pullCallback, true, this
            .getDefaultMQPullConsumer().getConsumerPullTimeoutMillis());
}
 
Example #20
Source File: DefaultMQPullConsumerImpl.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 4 votes vote down vote up
public void pull(MessageQueue mq, String subExpression, long offset, int maxNums,
                 PullCallback pullCallback, long timeout) throws MQClientException, RemotingException,
        InterruptedException {
    this.pullAsyncImpl(mq, subExpression, offset, maxNums, pullCallback, false, timeout);
}
 
Example #21
Source File: RocketmqIT.java    From uavstack with Apache License 2.0 2 votes vote down vote up
@Override
public void catchInvokeException(PullCallback t, Object proxy, Method method, Object[] args, Throwable e) {

    doAsyncCap(0, t, method);

}
 
Example #22
Source File: RocketmqIT.java    From uavstack with Apache License 2.0 2 votes vote down vote up
@Override
public void preProcess(PullCallback t, Object proxy, Method method, Object[] args) {

}