Search in sources :

Example 31 with RemotingException

use of org.apache.rocketmq.remoting.exception.RemotingException in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class MQClientAPIImpl method onExceptionImpl.

private // 
void onExceptionImpl(// 
final String brokerName, // 
final Message msg, // 
final long timeoutMillis, // 
final RemotingCommand request, // 
final SendCallback sendCallback, // 
final TopicPublishInfo topicPublishInfo, // 
final MQClientInstance instance, // 
final int timesTotal, // 
final AtomicInteger curTimes, // 
final Exception e, // 
final SendMessageContext context, // 
final boolean needRetry, // 12
final DefaultMQProducerImpl producer) {
    int tmp = curTimes.incrementAndGet();
    if (needRetry && tmp <= timesTotal) {
        // by default, it will send to the same broker
        String retryBrokerName = brokerName;
        if (topicPublishInfo != null) {
            // select one message queue accordingly, in order to determine which broker to send
            MessageQueue mqChosen = producer.selectOneMessageQueue(topicPublishInfo, brokerName);
            retryBrokerName = mqChosen.getBrokerName();
        }
        String addr = instance.findBrokerAddressInPublish(retryBrokerName);
        log.info("async send msg by retry {} times. topic={}, brokerAddr={}, brokerName={}", tmp, msg.getTopic(), addr, retryBrokerName);
        try {
            request.setOpaque(RemotingCommand.createNewRequestId());
            sendMessageAsync(addr, retryBrokerName, msg, timeoutMillis, request, sendCallback, topicPublishInfo, instance, timesTotal, curTimes, context, producer);
        } catch (InterruptedException e1) {
            onExceptionImpl(retryBrokerName, msg, timeoutMillis, request, sendCallback, topicPublishInfo, instance, timesTotal, curTimes, e1, context, false, producer);
        } catch (RemotingConnectException e1) {
            producer.updateFaultItem(brokerName, 3000, true);
            onExceptionImpl(retryBrokerName, msg, timeoutMillis, request, sendCallback, topicPublishInfo, instance, timesTotal, curTimes, e1, context, true, producer);
        } catch (RemotingTooMuchRequestException e1) {
            onExceptionImpl(retryBrokerName, msg, timeoutMillis, request, sendCallback, topicPublishInfo, instance, timesTotal, curTimes, e1, context, false, producer);
        } catch (RemotingException e1) {
            producer.updateFaultItem(brokerName, 3000, true);
            onExceptionImpl(retryBrokerName, msg, timeoutMillis, request, sendCallback, topicPublishInfo, instance, timesTotal, curTimes, e1, context, true, producer);
        }
    } else {
        if (context != null) {
            context.setException(e);
            context.getProducer().executeSendMessageHookAfter(context);
        }
        try {
            sendCallback.onException(e);
        } catch (Exception ignored) {
        }
    }
}
Also used : MessageQueue(org.apache.rocketmq.common.message.MessageQueue) RemotingConnectException(org.apache.rocketmq.remoting.exception.RemotingConnectException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) MQClientException(org.apache.rocketmq.client.exception.MQClientException) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) RemotingTooMuchRequestException(org.apache.rocketmq.remoting.exception.RemotingTooMuchRequestException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) RemotingConnectException(org.apache.rocketmq.remoting.exception.RemotingConnectException) RemotingSendRequestException(org.apache.rocketmq.remoting.exception.RemotingSendRequestException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException) RemotingTooMuchRequestException(org.apache.rocketmq.remoting.exception.RemotingTooMuchRequestException)

Example 32 with RemotingException

use of org.apache.rocketmq.remoting.exception.RemotingException in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class MQClientAPIImpl method sendMessageAsync.

private // 
void sendMessageAsync(// 
final String addr, // 
final String brokerName, // 
final Message msg, // 
final long timeoutMillis, // 
final RemotingCommand request, // 
final SendCallback sendCallback, // 
final TopicPublishInfo topicPublishInfo, // 
final MQClientInstance instance, // 
final int retryTimesWhenSendFailed, // 
final AtomicInteger times, // 
final SendMessageContext context, // 
final DefaultMQProducerImpl producer) throws InterruptedException, RemotingException {
    this.remotingClient.invokeAsync(addr, request, timeoutMillis, new InvokeCallback() {

        @Override
        public void operationComplete(ResponseFuture responseFuture) {
            RemotingCommand response = responseFuture.getResponseCommand();
            if (null == sendCallback && response != null) {
                try {
                    SendResult sendResult = MQClientAPIImpl.this.processSendResponse(brokerName, msg, response);
                    if (context != null && sendResult != null) {
                        context.setSendResult(sendResult);
                        context.getProducer().executeSendMessageHookAfter(context);
                    }
                } catch (Throwable e) {
                // 
                }
                producer.updateFaultItem(brokerName, System.currentTimeMillis() - responseFuture.getBeginTimestamp(), false);
                return;
            }
            if (response != null) {
                try {
                    SendResult sendResult = MQClientAPIImpl.this.processSendResponse(brokerName, msg, response);
                    assert sendResult != null;
                    if (context != null) {
                        context.setSendResult(sendResult);
                        context.getProducer().executeSendMessageHookAfter(context);
                    }
                    try {
                        sendCallback.onSuccess(sendResult);
                    } catch (Throwable e) {
                    }
                    producer.updateFaultItem(brokerName, System.currentTimeMillis() - responseFuture.getBeginTimestamp(), false);
                } catch (Exception e) {
                    producer.updateFaultItem(brokerName, System.currentTimeMillis() - responseFuture.getBeginTimestamp(), true);
                    onExceptionImpl(brokerName, msg, 0L, request, sendCallback, topicPublishInfo, instance, retryTimesWhenSendFailed, times, e, context, false, producer);
                }
            } else {
                producer.updateFaultItem(brokerName, System.currentTimeMillis() - responseFuture.getBeginTimestamp(), true);
                if (!responseFuture.isSendRequestOK()) {
                    MQClientException ex = new MQClientException("send request failed", responseFuture.getCause());
                    onExceptionImpl(brokerName, msg, 0L, request, sendCallback, topicPublishInfo, instance, retryTimesWhenSendFailed, times, ex, context, true, producer);
                } else if (responseFuture.isTimeout()) {
                    MQClientException ex = new MQClientException("wait response timeout " + responseFuture.getTimeoutMillis() + "ms", responseFuture.getCause());
                    onExceptionImpl(brokerName, msg, 0L, request, sendCallback, topicPublishInfo, instance, retryTimesWhenSendFailed, times, ex, context, true, producer);
                } else {
                    MQClientException ex = new MQClientException("unknow reseaon", responseFuture.getCause());
                    onExceptionImpl(brokerName, msg, 0L, request, sendCallback, topicPublishInfo, instance, retryTimesWhenSendFailed, times, ex, context, true, producer);
                }
            }
        }
    });
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) InvokeCallback(org.apache.rocketmq.remoting.InvokeCallback) SendResult(org.apache.rocketmq.client.producer.SendResult) ResponseFuture(org.apache.rocketmq.remoting.netty.ResponseFuture) MQClientException(org.apache.rocketmq.client.exception.MQClientException) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) RemotingTooMuchRequestException(org.apache.rocketmq.remoting.exception.RemotingTooMuchRequestException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) RemotingConnectException(org.apache.rocketmq.remoting.exception.RemotingConnectException) RemotingSendRequestException(org.apache.rocketmq.remoting.exception.RemotingSendRequestException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 33 with RemotingException

use of org.apache.rocketmq.remoting.exception.RemotingException in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class DefaultMQPullConsumerImpl method pullAsyncImpl.

private // 
void pullAsyncImpl(// 
final MessageQueue mq, // 
final String subExpression, // 
final long offset, // 
final int maxNums, // 
final PullCallback pullCallback, // 
final boolean block, final long timeout) throws MQClientException, RemotingException, InterruptedException {
    this.makeSureStateOK();
    if (null == mq) {
        throw new MQClientException("mq is null", null);
    }
    if (offset < 0) {
        throw new MQClientException("offset < 0", null);
    }
    if (maxNums <= 0) {
        throw new MQClientException("maxNums <= 0", null);
    }
    if (null == pullCallback) {
        throw new MQClientException("pullCallback is null", null);
    }
    this.subscriptionAutomatically(mq.getTopic());
    try {
        int sysFlag = PullSysFlag.buildSysFlag(false, block, true, false);
        final SubscriptionData subscriptionData;
        try {
            subscriptionData = // 
            FilterAPI.buildSubscriptionData(// 
            this.defaultMQPullConsumer.getConsumerGroup(), mq.getTopic(), subExpression);
        } catch (Exception e) {
            throw new MQClientException("parse subscription error", e);
        }
        long timeoutMillis = block ? this.defaultMQPullConsumer.getConsumerTimeoutMillisWhenSuspend() : timeout;
        // 
        this.pullAPIWrapper.pullKernelImpl(// 1
        mq, // 2
        subscriptionData.getSubString(), // 3
        0L, // 4
        offset, // 5
        maxNums, // 6
        sysFlag, // 7
        0, // 8
        this.defaultMQPullConsumer.getBrokerSuspendMaxTimeMillis(), // 9
        timeoutMillis, // 10
        CommunicationMode.ASYNC, new PullCallback() {

            @Override
            public void onSuccess(PullResult pullResult) {
                pullCallback.onSuccess(DefaultMQPullConsumerImpl.this.pullAPIWrapper.processPullResult(mq, pullResult, subscriptionData));
            }

            @Override
            public void onException(Throwable e) {
                pullCallback.onException(e);
            }
        });
    } catch (MQBrokerException e) {
        throw new MQClientException("pullAsync unknow exception", e);
    }
}
Also used : PullCallback(org.apache.rocketmq.client.consumer.PullCallback) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) SubscriptionData(org.apache.rocketmq.common.protocol.heartbeat.SubscriptionData) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) PullResult(org.apache.rocketmq.client.consumer.PullResult)

Example 34 with RemotingException

use of org.apache.rocketmq.remoting.exception.RemotingException in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class DefaultMQPullConsumerImpl method pullSyncImpl.

private PullResult pullSyncImpl(MessageQueue mq, String subExpression, long offset, int maxNums, boolean block, long timeout) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
    this.makeSureStateOK();
    if (null == mq) {
        throw new MQClientException("mq is null", null);
    }
    if (offset < 0) {
        throw new MQClientException("offset < 0", null);
    }
    if (maxNums <= 0) {
        throw new MQClientException("maxNums <= 0", null);
    }
    this.subscriptionAutomatically(mq.getTopic());
    int sysFlag = PullSysFlag.buildSysFlag(false, block, true, false);
    SubscriptionData subscriptionData;
    try {
        subscriptionData = // 
        FilterAPI.buildSubscriptionData(// 
        this.defaultMQPullConsumer.getConsumerGroup(), mq.getTopic(), subExpression);
    } catch (Exception e) {
        throw new MQClientException("parse subscription error", e);
    }
    long timeoutMillis = block ? this.defaultMQPullConsumer.getConsumerTimeoutMillisWhenSuspend() : timeout;
    PullResult pullResult = // 
    this.pullAPIWrapper.pullKernelImpl(// 1
    mq, // 2
    subscriptionData.getSubString(), // 3
    0L, // 4
    offset, // 5
    maxNums, // 6
    sysFlag, // 7
    0, // 8
    this.defaultMQPullConsumer.getBrokerSuspendMaxTimeMillis(), // 9
    timeoutMillis, // 10
    CommunicationMode.SYNC, // 11
    null);
    this.pullAPIWrapper.processPullResult(mq, pullResult, subscriptionData);
    if (!this.consumeMessageHookList.isEmpty()) {
        ConsumeMessageContext consumeMessageContext = null;
        consumeMessageContext = new ConsumeMessageContext();
        consumeMessageContext.setConsumerGroup(this.groupName());
        consumeMessageContext.setMq(mq);
        consumeMessageContext.setMsgList(pullResult.getMsgFoundList());
        consumeMessageContext.setSuccess(false);
        this.executeHookBefore(consumeMessageContext);
        consumeMessageContext.setStatus(ConsumeConcurrentlyStatus.CONSUME_SUCCESS.toString());
        consumeMessageContext.setSuccess(true);
        this.executeHookAfter(consumeMessageContext);
    }
    return pullResult;
}
Also used : ConsumeMessageContext(org.apache.rocketmq.client.hook.ConsumeMessageContext) SubscriptionData(org.apache.rocketmq.common.protocol.heartbeat.SubscriptionData) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) PullResult(org.apache.rocketmq.client.consumer.PullResult)

Example 35 with RemotingException

use of org.apache.rocketmq.remoting.exception.RemotingException in project rocketmq by apache.

the class MonitorService method reportConsumerRunningInfo.

public void reportConsumerRunningInfo(final String consumerGroup) throws InterruptedException, MQBrokerException, RemotingException, MQClientException {
    ConsumerConnection cc = defaultMQAdminExt.examineConsumerConnectionInfo(consumerGroup);
    TreeMap<String, ConsumerRunningInfo> infoMap = new TreeMap<String, ConsumerRunningInfo>();
    for (Connection c : cc.getConnectionSet()) {
        String clientId = c.getClientId();
        if (c.getVersion() < MQVersion.Version.V3_1_8_SNAPSHOT.ordinal()) {
            continue;
        }
        try {
            ConsumerRunningInfo info = defaultMQAdminExt.getConsumerRunningInfo(consumerGroup, clientId, false);
            infoMap.put(clientId, info);
        } catch (Exception e) {
        }
    }
    if (!infoMap.isEmpty()) {
        this.monitorListener.reportConsumerRunningInfo(infoMap);
    }
}
Also used : Connection(org.apache.rocketmq.common.protocol.body.Connection) ConsumerConnection(org.apache.rocketmq.common.protocol.body.ConsumerConnection) ConsumerConnection(org.apache.rocketmq.common.protocol.body.ConsumerConnection) ConsumerRunningInfo(org.apache.rocketmq.common.protocol.body.ConsumerRunningInfo) TreeMap(java.util.TreeMap) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException)

Aggregations

RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)45 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)39 MQClientException (org.apache.rocketmq.client.exception.MQClientException)39 SendResult (org.apache.rocketmq.client.producer.SendResult)13 Message (org.apache.rocketmq.common.message.Message)13 RemotingTimeoutException (org.apache.rocketmq.remoting.exception.RemotingTimeoutException)13 UnsupportedEncodingException (java.io.UnsupportedEncodingException)9 RemotingConnectException (org.apache.rocketmq.remoting.exception.RemotingConnectException)9 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)8 Test (org.junit.Test)8 RemotingCommandException (org.apache.rocketmq.remoting.exception.RemotingCommandException)7 RemotingSendRequestException (org.apache.rocketmq.remoting.exception.RemotingSendRequestException)7 IOException (java.io.IOException)6 SendMessageContext (org.apache.rocketmq.client.hook.SendMessageContext)6 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)6 SendMessageRequestHeader (org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader)6 SubscriptionData (org.apache.rocketmq.common.protocol.heartbeat.SubscriptionData)6 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)6 SubCommandException (org.apache.rocketmq.tools.command.SubCommandException)6 MessageExt (org.apache.rocketmq.common.message.MessageExt)5