Search in sources :

Example 1 with FindBrokerResult

use of org.apache.rocketmq.client.impl.FindBrokerResult in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class RebalanceImpl method unlock.

public void unlock(final MessageQueue mq, final boolean oneway) {
    FindBrokerResult findBrokerResult = this.mQClientFactory.findBrokerAddressInSubscribe(mq.getBrokerName(), MixAll.MASTER_ID, true);
    if (findBrokerResult != null) {
        UnlockBatchRequestBody requestBody = new UnlockBatchRequestBody();
        requestBody.setConsumerGroup(this.consumerGroup);
        requestBody.setClientId(this.mQClientFactory.getClientId());
        requestBody.getMqSet().add(mq);
        try {
            this.mQClientFactory.getMQClientAPIImpl().unlockBatchMQ(findBrokerResult.getBrokerAddr(), requestBody, 1000, oneway);
            // 
            log.warn(// 
            "unlock messageQueue. group:{}, clientId:{}, mq:{}", // 
            this.consumerGroup, // 
            this.mQClientFactory.getClientId(), mq);
        } catch (Exception e) {
            log.error("unlockBatchMQ exception, " + mq, e);
        }
    }
}
Also used : FindBrokerResult(org.apache.rocketmq.client.impl.FindBrokerResult) UnlockBatchRequestBody(org.apache.rocketmq.common.protocol.body.UnlockBatchRequestBody)

Example 2 with FindBrokerResult

use of org.apache.rocketmq.client.impl.FindBrokerResult in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class DefaultMQPullConsumerTest method init.

@Before
public void init() throws Exception {
    pullConsumer = new DefaultMQPullConsumer(consumerGroup);
    pullConsumer.setNamesrvAddr("127.0.0.1:9876");
    pullConsumer.start();
    PullAPIWrapper pullAPIWrapper = pullConsumer.getDefaultMQPullConsumerImpl().getPullAPIWrapper();
    Field field = PullAPIWrapper.class.getDeclaredField("mQClientFactory");
    field.setAccessible(true);
    field.set(pullAPIWrapper, mQClientFactory);
    field = MQClientInstance.class.getDeclaredField("mQClientAPIImpl");
    field.setAccessible(true);
    field.set(mQClientFactory, mQClientAPIImpl);
    when(mQClientFactory.findBrokerAddressInSubscribe(anyString(), anyLong(), anyBoolean())).thenReturn(new FindBrokerResult("127.0.0.1:10911", false));
}
Also used : PullAPIWrapper(org.apache.rocketmq.client.impl.consumer.PullAPIWrapper) Field(java.lang.reflect.Field) MQClientInstance(org.apache.rocketmq.client.impl.factory.MQClientInstance) FindBrokerResult(org.apache.rocketmq.client.impl.FindBrokerResult) Before(org.junit.Before)

Example 3 with FindBrokerResult

use of org.apache.rocketmq.client.impl.FindBrokerResult 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 FindBrokerResult

use of org.apache.rocketmq.client.impl.FindBrokerResult in project rocketmq by apache.

the class RebalanceImpl method unlockAll.

public void unlockAll(final boolean oneway) {
    HashMap<String, Set<MessageQueue>> brokerMqs = this.buildProcessQueueTableByBrokerName();
    for (final Map.Entry<String, Set<MessageQueue>> entry : brokerMqs.entrySet()) {
        final String brokerName = entry.getKey();
        final Set<MessageQueue> mqs = entry.getValue();
        if (mqs.isEmpty())
            continue;
        FindBrokerResult findBrokerResult = this.mQClientFactory.findBrokerAddressInSubscribe(brokerName, MixAll.MASTER_ID, true);
        if (findBrokerResult != null) {
            UnlockBatchRequestBody requestBody = new UnlockBatchRequestBody();
            requestBody.setConsumerGroup(this.consumerGroup);
            requestBody.setClientId(this.mQClientFactory.getClientId());
            requestBody.setMqSet(mqs);
            try {
                this.mQClientFactory.getMQClientAPIImpl().unlockBatchMQ(findBrokerResult.getBrokerAddr(), requestBody, 1000, oneway);
                for (MessageQueue mq : mqs) {
                    ProcessQueue processQueue = this.processQueueTable.get(mq);
                    if (processQueue != null) {
                        processQueue.setLocked(false);
                        log.info("the message queue unlock OK, Group: {} {}", this.consumerGroup, mq);
                    }
                }
            } catch (Exception e) {
                log.error("unlockBatchMQ exception, " + mqs, e);
            }
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) FindBrokerResult(org.apache.rocketmq.client.impl.FindBrokerResult) UnlockBatchRequestBody(org.apache.rocketmq.common.protocol.body.UnlockBatchRequestBody)

Example 5 with FindBrokerResult

use of org.apache.rocketmq.client.impl.FindBrokerResult 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

FindBrokerResult (org.apache.rocketmq.client.impl.FindBrokerResult)21 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)8 HashSet (java.util.HashSet)6 MQClientException (org.apache.rocketmq.client.exception.MQClientException)6 Before (org.junit.Before)6 Field (java.lang.reflect.Field)4 Set (java.util.Set)4 PullAPIWrapper (org.apache.rocketmq.client.impl.consumer.PullAPIWrapper)4 MQClientInstance (org.apache.rocketmq.client.impl.factory.MQClientInstance)4 LockBatchRequestBody (org.apache.rocketmq.common.protocol.body.LockBatchRequestBody)4 UnlockBatchRequestBody (org.apache.rocketmq.common.protocol.body.UnlockBatchRequestBody)4 PullMessageRequestHeader (org.apache.rocketmq.common.protocol.header.PullMessageRequestHeader)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ConcurrentMap (java.util.concurrent.ConcurrentMap)3 InetSocketAddress (java.net.InetSocketAddress)2 Entry (java.util.Map.Entry)2 ClientConfig (org.apache.rocketmq.client.ClientConfig)2 PullResult (org.apache.rocketmq.client.consumer.PullResult)2