com.alibaba.rocketmq.common.protocol.heartbeat.SubscriptionData Java Examples

The following examples show how to use com.alibaba.rocketmq.common.protocol.heartbeat.SubscriptionData. 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: DefaultMessageFilter.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean isMessageMatched(SubscriptionData subscriptionData, long tagsCode) {
    if (null == subscriptionData) {
        return true;
    }

    if (subscriptionData.isClassFilterMode())
        return true;

    if (subscriptionData.getSubString().equals(SubscriptionData.SUB_ALL)) {
        return true;
    }

    //这里的匹配机制很简单 ,只要tags 的hashcode一致, 就认为匹配; 然后把消息发送到client以后做msg tag的精确匹配。
    return subscriptionData.getCodeSet().contains((int) tagsCode);
}
 
Example #2
Source File: ConsumerManager.java    From RocketMQ-Master-analyze with Apache License 2.0 6 votes vote down vote up
/**
 * 注册消费者 返回是否有变化
 */
public boolean registerConsumer(final String group, final ClientChannelInfo clientChannelInfo,
        ConsumeType consumeType, MessageModel messageModel, ConsumeFromWhere consumeFromWhere,
        final Set<SubscriptionData> subList) {
    ConsumerGroupInfo consumerGroupInfo = this.consumerTable.get(group);
    if (null == consumerGroupInfo) {
        ConsumerGroupInfo tmp = new ConsumerGroupInfo(group, consumeType, messageModel, consumeFromWhere);
        ConsumerGroupInfo prev = this.consumerTable.putIfAbsent(group, tmp);
        consumerGroupInfo = prev != null ? prev : tmp;
    }

    boolean r1 = consumerGroupInfo.updateChannel(clientChannelInfo, consumeType, messageModel,
        consumeFromWhere);
    boolean r2 = consumerGroupInfo.updateSubscription(subList);

    if (r1 || r2) {
        // ConsumerId列表变化,通知所有Consumer
        this.consumerIdsChangeListener.consumerIdsChanged(group, consumerGroupInfo.getAllChannel());
    }

    return r1 || r2;
}
 
Example #3
Source File: DefaultMQPullConsumerImpl.java    From RocketMQ-Master-analyze with Apache License 2.0 6 votes vote down vote up
private void copySubscription() throws MQClientException {
    try {
        Set<String> registerTopics = this.defaultMQPullConsumer.getRegisterTopics();
        if (registerTopics != null) {
            for (final String topic : registerTopics) {
                SubscriptionData subscriptionData =
                        FilterAPI.buildSubscriptionData(this.defaultMQPullConsumer.getConsumerGroup(), //
                            topic, SubscriptionData.SUB_ALL);
                this.rebalanceImpl.getSubscriptionInner().put(topic, subscriptionData);
            }
        }
    }
    catch (Exception e) {
        throw new MQClientException("subscription exception", e);
    }
}
 
Example #4
Source File: RebalanceImpl.java    From RocketMQ-Master-analyze with Apache License 2.0 6 votes vote down vote up
public void doRebalance() {
    Map<String, SubscriptionData> subTable = this.getSubscriptionInner();
    if (subTable != null) {
        for (final Map.Entry<String, SubscriptionData> entry : subTable.entrySet()) {
            final String topic = entry.getKey();
            try {
                this.rebalanceByTopic(topic);
            }
            catch (Exception e) {
                if (!topic.startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX)) {
                    log.warn("rebalanceByTopic Exception", e);
                }
            }
        }
    }

    this.truncateMessageQueueNotMyTopic();
}
 
Example #5
Source File: DefaultMessageFilter.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isMessageMatched(SubscriptionData subscriptionData, Long tagsCode) {
    if (tagsCode == null) {
        return true;
    }

    if (null == subscriptionData) {
        return true;
    }

    if (subscriptionData.isClassFilterMode())
        return true;

    if (subscriptionData.getSubString().equals(SubscriptionData.SUB_ALL)) {
        return true;
    }

    return subscriptionData.getCodeSet().contains(tagsCode.intValue());
}
 
Example #6
Source File: DefaultMQPullConsumerImpl.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Set<SubscriptionData> subscriptions() {
    Set<SubscriptionData> result = new HashSet<SubscriptionData>();

    Set<String> topics = this.defaultMQPullConsumer.getRegisterTopics();
    if (topics != null) {
        synchronized (topics) {
            for (String t : topics) {
                SubscriptionData ms = null;
                try {
                    ms = FilterAPI.buildSubscriptionData(this.groupName(), t, SubscriptionData.SUB_ALL);
                } catch (Exception e) {
                    log.error("parse subscription error", e);
                }
                ms.setSubVersion(0L);
                result.add(ms);
            }
        }
    }

    return result;
}
 
Example #7
Source File: DefaultMQPushConsumerImpl.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public void subscribe(String topic, String fullClassName, String filterClassSource) throws MQClientException {
    try {
        SubscriptionData subscriptionData = FilterAPI.buildSubscriptionData(this.defaultMQPushConsumer.getConsumerGroup(), //
                topic, "*");
        subscriptionData.setSubString(fullClassName);
        subscriptionData.setClassFilterMode(true);
        subscriptionData.setFilterClassSource(filterClassSource);
        this.rebalanceImpl.getSubscriptionInner().put(topic, subscriptionData);
        if (this.mQClientFactory != null) {
            this.mQClientFactory.sendHeartbeatToAllBrokerWithLock();
        }

    } catch (Exception e) {
        throw new MQClientException("subscription exception", e);
    }
}
 
Example #8
Source File: ConsumerManager.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public boolean registerConsumer(final String group, final ClientChannelInfo clientChannelInfo,
                                ConsumeType consumeType, MessageModel messageModel, ConsumeFromWhere consumeFromWhere,
                                final Set<SubscriptionData> subList) {

    ConsumerGroupInfo consumerGroupInfo = this.consumerTable.get(group);
    if (null == consumerGroupInfo) {
        ConsumerGroupInfo tmp = new ConsumerGroupInfo(group, consumeType, messageModel, consumeFromWhere);
        ConsumerGroupInfo prev = this.consumerTable.putIfAbsent(group, tmp);
        consumerGroupInfo = prev != null ? prev : tmp;
    }

    boolean r1 =
            consumerGroupInfo.updateChannel(clientChannelInfo, consumeType, messageModel,
                    consumeFromWhere);
    boolean r2 = consumerGroupInfo.updateSubscription(subList);

    if (r1 || r2) {
        this.consumerIdsChangeListener.consumerIdsChanged(group, consumerGroupInfo.getAllChannel());
    }

    return r1 || r2;
}
 
Example #9
Source File: DefaultMQPullConsumerImpl.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
@Override
public Set<SubscriptionData> subscriptions() {
    Set<SubscriptionData> result = new HashSet<SubscriptionData>();

    Set<String> topics = this.defaultMQPullConsumer.getRegisterTopics();
    if (topics != null) {
        synchronized (topics) {
            for (String t : topics) {
                SubscriptionData ms = null;
                try {
                    ms = FilterAPI.buildSubscriptionData(this.groupName(), t, SubscriptionData.SUB_ALL);
                } catch (Exception e) {
                    log.error("parse subscription error", e);
                    return null;
                }
                ms.setSubVersion(0L);
                result.add(ms);
            }
        }
    }

    return result;
}
 
Example #10
Source File: DefaultMQPushConsumerImpl.java    From RocketMQ-Master-analyze with Apache License 2.0 6 votes vote down vote up
public void subscribe(String topic, String fullClassName, String filterClassSource)
        throws MQClientException {
    try {
        SubscriptionData subscriptionData =
                FilterAPI.buildSubscriptionData(this.defaultMQPushConsumer.getConsumerGroup(), //
                    topic, "*");
        subscriptionData.setSubString(fullClassName);
        subscriptionData.setClassFilterMode(true);
        subscriptionData.setFilterClassSource(filterClassSource);
        this.rebalanceImpl.getSubscriptionInner().put(topic, subscriptionData);
        if (this.mQClientFactory != null) {
            this.mQClientFactory.sendHeartbeatToAllBrokerWithLock();
        }

    }
    catch (Exception e) {
        throw new MQClientException("subscription exception", e);
    }
}
 
Example #11
Source File: RebalanceImpl.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public void doRebalance(final boolean isOrder) {
    Map<String, SubscriptionData> subTable = this.getSubscriptionInner();
    if (subTable != null) {
        for (final Map.Entry<String, SubscriptionData> entry : subTable.entrySet()) {
            final String topic = entry.getKey();
            try {
                this.rebalanceByTopic(topic, isOrder);
            } catch (Exception e) {
                if (!topic.startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX)) {
                    log.warn("rebalanceByTopic Exception", e);
                }
            }
        }
    }

    this.truncateMessageQueueNotMyTopic();
}
 
Example #12
Source File: RebalanceImpl.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
private void truncateMessageQueueNotMyTopic() {
    Map<String, SubscriptionData> subTable = this.getSubscriptionInner();

    for (MessageQueue mq : this.processQueueTable.keySet()) {
        if (!subTable.containsKey(mq.getTopic())) {
            ProcessQueue pq = this.processQueueTable.remove(mq);
            if (pq != null) {
                pq.setDropped(true);
                log.info("doRebalance, {}, truncateMessageQueueNotMyTopic remove unnecessary mq, {}",
                    consumerGroup, mq);
            }
        }
    }
}
 
Example #13
Source File: DefaultMQPullConsumerImpl.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
@Override
public void updateTopicSubscribeInfo(String topic, Set<MessageQueue> info) {
    Map<String, SubscriptionData> subTable = this.rebalanceImpl.getSubscriptionInner();
    if (subTable != null) {
        if (subTable.containsKey(topic)) {
            this.rebalanceImpl.getTopicSubscribeInfoTable().put(topic, info);
        }
    }
}
 
Example #14
Source File: DefaultMQPushConsumerImpl.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
@Override
public Set<SubscriptionData> subscriptions() {
    Set<SubscriptionData> subSet = new HashSet<SubscriptionData>();

    subSet.addAll(this.rebalanceImpl.getSubscriptionInner().values());

    return subSet;
}
 
Example #15
Source File: FilterAPITest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Test
public void testSubscriptionData() throws Exception {
    SubscriptionData subscriptionData =
            FilterAPI.buildSubscriptionData("ConsumerGroup1", "TestTopic", "TAG1 || Tag2 || tag3");
    subscriptionData.setFilterClassSource("java hello");
    String json = RemotingSerializable.toJson(subscriptionData, true);
    System.out.println(json);
}
 
Example #16
Source File: PullRequest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public PullRequest(RemotingCommand requestCommand, Channel clientChannel, long timeoutMillis, long suspendTimestamp,
                   long pullFromThisOffset, SubscriptionData subscriptionData) {
    this.requestCommand = requestCommand;
    this.clientChannel = clientChannel;
    this.timeoutMillis = timeoutMillis;
    this.suspendTimestamp = suspendTimestamp;
    this.pullFromThisOffset = pullFromThisOffset;
    this.subscriptionData = subscriptionData;
}
 
Example #17
Source File: DefaultMQPullConsumerImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private void copySubscription() throws MQClientException {
    try {
        Set<String> registerTopics = this.defaultMQPullConsumer.getRegisterTopics();
        if (registerTopics != null) {
            for (final String topic : registerTopics) {
                SubscriptionData subscriptionData = FilterAPI.buildSubscriptionData(this.defaultMQPullConsumer.getConsumerGroup(), //
                        topic, SubscriptionData.SUB_ALL);
                this.rebalanceImpl.getSubscriptionInner().put(topic, subscriptionData);
            }
        }
    } catch (Exception e) {
        throw new MQClientException("subscription exception", e);
    }
}
 
Example #18
Source File: RebalanceImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private void truncateMessageQueueNotMyTopic() {
    Map<String, SubscriptionData> subTable = this.getSubscriptionInner();

    for (MessageQueue mq : this.processQueueTable.keySet()) {
        if (!subTable.containsKey(mq.getTopic())) {

            ProcessQueue pq = this.processQueueTable.remove(mq);
            if (pq != null) {
                pq.setDropped(true);
                log.info("doRebalance, {}, truncateMessageQueueNotMyTopic remove unnecessary mq, {}", consumerGroup, mq);
            }
        }
    }
}
 
Example #19
Source File: DefaultMQPullConsumerImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Override
public void updateTopicSubscribeInfo(String topic, Set<MessageQueue> info) {
    Map<String, SubscriptionData> subTable = this.rebalanceImpl.getSubscriptionInner();
    if (subTable != null) {
        if (subTable.containsKey(topic)) {
            this.rebalanceImpl.getTopicSubscribeInfoTable().put(topic, info);
        }
    }
}
 
Example #20
Source File: ConsumerManager.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public SubscriptionData findSubscriptionData(final String group, final String topic) {
    ConsumerGroupInfo consumerGroupInfo = this.getConsumerGroupInfo(group);
    if (consumerGroupInfo != null) {
        return consumerGroupInfo.findSubscriptionData(topic);
    }

    return null;
}
 
Example #21
Source File: FilterAPITest.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
@Test
public void testSubscriptionData() throws Exception {
    SubscriptionData subscriptionData =
            FilterAPI.buildSubscriptionData("ConsumerGroup1", "TestTopic", "TAG1 || Tag2 || tag3");
    subscriptionData.setFilterClassSource("java hello");
    String json = RemotingSerializable.toJson(subscriptionData, true);
    System.out.println(json);
}
 
Example #22
Source File: DefaultMQPushConsumerImpl.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isSubscribeTopicNeedUpdate(String topic) {
    Map<String, SubscriptionData> subTable = this.getSubscriptionInner();
    if (subTable != null) {
        if (subTable.containsKey(topic)) {
            return !this.rebalanceImpl.topicSubscribeInfoTable.containsKey(topic);
        }
    }

    return false;
}
 
Example #23
Source File: MQClientAPIImpl.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
/**
 * 获取consumer连接信息
 * 
 * @param addr
 * @param consumerGroup
 * @param timeoutMillis
 * @return
 * @throws RemotingConnectException
 * @throws RemotingSendRequestException
 * @throws RemotingTimeoutException
 * @throws InterruptedException
 * @throws MQBrokerException
 */
public ConsumerConnection getConsumerConnectionList(final String addr, final String consumerGroup,
        final long timeoutMillis) throws RemotingConnectException, RemotingSendRequestException,
                RemotingTimeoutException, InterruptedException, MQBrokerException {
    String consumerGroupWithProjectGroup = consumerGroup;
    if (!UtilAll.isBlank(projectGroupPrefix)) {
        consumerGroupWithProjectGroup =
                VirtualEnvUtil.buildWithProjectGroup(consumerGroup, projectGroupPrefix);
    }

    GetConsumerConnectionListRequestHeader requestHeader = new GetConsumerConnectionListRequestHeader();
    requestHeader.setConsumerGroup(consumerGroupWithProjectGroup);

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

    RemotingCommand response = this.remotingClient.invokeSync(addr, request, timeoutMillis);
    switch (response.getCode()) {
    case ResponseCode.SUCCESS: {
        ConsumerConnection consumerConnection =
                ConsumerConnection.decode(response.getBody(), ConsumerConnection.class);
        if (!UtilAll.isBlank(projectGroupPrefix)) {
            ConcurrentHashMap<String, SubscriptionData> subscriptionDataConcurrentHashMap =
                    consumerConnection.getSubscriptionTable();
            for (Map.Entry<String, SubscriptionData> subscriptionDataEntry : subscriptionDataConcurrentHashMap
                .entrySet()) {
                SubscriptionData subscriptionData = subscriptionDataEntry.getValue();
                subscriptionDataEntry.getValue().setTopic(
                    VirtualEnvUtil.clearProjectGroup(subscriptionData.getTopic(), projectGroupPrefix));
            }
        }
        return consumerConnection;
    }
    default:
        break;
    }

    throw new MQBrokerException(response.getCode(), response.getRemark());
}
 
Example #24
Source File: FilterAPI.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public static SubscriptionData buildSubscriptionData(final String consumerGroup, String topic,
                                                     String subString) throws Exception {
    SubscriptionData subscriptionData = new SubscriptionData();
    subscriptionData.setTopic(topic);
    subscriptionData.setSubString(subString);

    if (null == subString || subString.equals(SubscriptionData.SUB_ALL) || subString.length() == 0) {
        subscriptionData.setSubString(SubscriptionData.SUB_ALL);
    } else {
        String[] tags = subString.split("\\|\\|");
        if (tags != null && tags.length > 0) {
            for (String tag : tags) {
                if (tag.length() > 0) {
                    String trimString = tag.trim();
                    if (trimString.length() > 0) {
                        subscriptionData.getTagsSet().add(trimString);
                        subscriptionData.getCodeSet().add(trimString.hashCode());
                    }
                }
            }
        } else {
            throw new Exception("subString split error");
        }
    }

    return subscriptionData;
}
 
Example #25
Source File: DefaultMQPushConsumerImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Override
public ConsumerRunningInfo consumerRunningInfo() {
    ConsumerRunningInfo info = new ConsumerRunningInfo();

    Properties prop = MixAll.object2Properties(this.defaultMQPushConsumer);

    prop.put(ConsumerRunningInfo.PROP_CONSUME_ORDERLY, String.valueOf(this.consumeOrderly));
    prop.put(ConsumerRunningInfo.PROP_THREADPOOL_CORE_SIZE, String.valueOf(this.consumeMessageService.getCorePoolSize()));
    prop.put(ConsumerRunningInfo.PROP_CONSUMER_START_TIMESTAMP, String.valueOf(this.consumerStartTimestamp));

    info.setProperties(prop);

    Set<SubscriptionData> subSet = this.subscriptions();
    info.getSubscriptionSet().addAll(subSet);

    Iterator<Entry<MessageQueue, ProcessQueue>> it = this.rebalanceImpl.getProcessQueueTable().entrySet().iterator();
    while (it.hasNext()) {
        Entry<MessageQueue, ProcessQueue> next = it.next();
        MessageQueue mq = next.getKey();
        ProcessQueue pq = next.getValue();

        ProcessQueueInfo pqinfo = new ProcessQueueInfo();
        pqinfo.setCommitOffset(this.offsetStore.readOffset(mq, ReadOffsetType.MEMORY_FIRST_THEN_STORE));
        pq.fillProcessQueueInfo(pqinfo);
        info.getMqTable().put(mq, pqinfo);
    }

    for (SubscriptionData sd : subSet) {
        ConsumeStatus consumeStatus = this.mQClientFactory.getConsumerStatsManager().consumeStatus(this.groupName(), sd.getTopic());
        info.getStatusTable().put(sd.getTopic(), consumeStatus);
    }

    return info;
}
 
Example #26
Source File: DefaultMQPushConsumerImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isSubscribeTopicNeedUpdate(String topic) {
    Map<String, SubscriptionData> subTable = this.getSubscriptionInner();
    if (subTable != null) {
        if (subTable.containsKey(topic)) {
            return !this.rebalanceImpl.topicSubscribeInfoTable.containsKey(topic);
        }
    }

    return false;
}
 
Example #27
Source File: DefaultMQPushConsumerImpl.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Set<SubscriptionData> subscriptions() {
    Set<SubscriptionData> subSet = new HashSet<SubscriptionData>();

    //SubscriptionData 集合
    subSet.addAll(this.rebalanceImpl.getSubscriptionInner().values());

    return subSet;
}
 
Example #28
Source File: DefaultMQPushConsumerImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public void subscribe(String topic, String subExpression) throws MQClientException {
    try {
        SubscriptionData subscriptionData = FilterAPI.buildSubscriptionData(this.defaultMQPushConsumer.getConsumerGroup(), //
                topic, subExpression);
        this.rebalanceImpl.getSubscriptionInner().put(topic, subscriptionData);
        if (this.mQClientFactory != null) {
            this.mQClientFactory.sendHeartbeatToAllBrokerWithLock();
        }
    } catch (Exception e) {
        throw new MQClientException("subscription exception", e);
    }
}
 
Example #29
Source File: FilterAPITest.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testSubscriptionData() throws Exception {
    SubscriptionData subscriptionData =
            FilterAPI.buildSubscriptionData("ConsumerGroup1", "TestTopic", "TAG1 || Tag2 || tag3");
    subscriptionData.setFilterClassSource("java hello");
    String json = RemotingSerializable.toJson(subscriptionData, true);
    System.out.println(json);
}
 
Example #30
Source File: DefaultMQPushConsumerImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Override
public Set<SubscriptionData> subscriptions() {
    Set<SubscriptionData> subSet = new HashSet<SubscriptionData>();

    subSet.addAll(this.rebalanceImpl.getSubscriptionInner().values());

    return subSet;
}