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

The following examples show how to use com.alibaba.rocketmq.common.protocol.heartbeat.MessageModel. 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: 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 #2
Source File: RebalancePushImpl.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean removeUnnecessaryMessageQueue(MessageQueue mq, ProcessQueue pq) {
    this.defaultMQPushConsumerImpl.getOffsetStore().persist(mq); //消费位点同步到broker .
    this.defaultMQPushConsumerImpl.getOffsetStore().removeOffset(mq); //清理本地存储的队列消费位点。
    if (this.defaultMQPushConsumerImpl.isConsumeOrderly()
            && MessageModel.CLUSTERING.equals(this.defaultMQPushConsumerImpl.messageModel())) {
        try {
            if (pq.getLockConsume().tryLock(1000, TimeUnit.MILLISECONDS)) {
                try {
                    this.unlock(mq, true);
                    return true;
                }
                finally {
                    pq.getLockConsume().unlock();
                }
            }
            else {
                log.warn(
                    "[WRONG]mq is consuming, so can not unlock it, {}. maybe hanged for a while, {}",//
                    mq,//
                    pq.getTryUnlockTimes());

                pq.incTryUnlockTimes();
            }
        }
        catch (Exception e) {
            log.error("removeUnnecessaryMessageQueue Exception", e);
        }

        return false;
    }
    return true;
}
 
Example #3
Source File: MQHelper.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 5 votes vote down vote up
public static void resetOffsetByTimestamp(//
        final MessageModel messageModel,//
        final String consumerGroup, //
        final String topic, //
        final long timestamp) throws Exception {
    resetOffsetByTimestamp(messageModel, "DEFAULT", consumerGroup, topic, timestamp);
}
 
Example #4
Source File: ConsumerGroupInfo.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
public ConsumerGroupInfo(String groupName, ConsumeType consumeType, MessageModel messageModel,
        ConsumeFromWhere consumeFromWhere) {
    this.groupName = groupName;
    this.consumeType = consumeType;
    this.messageModel = messageModel;
    this.consumeFromWhere = consumeFromWhere;
}
 
Example #5
Source File: RebalanceImpl.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
public RebalanceImpl(String consumerGroup, MessageModel messageModel,
        AllocateMessageQueueStrategy allocateMessageQueueStrategy, MQClientInstance mQClientFactory) {
    this.consumerGroup = consumerGroup;
    this.messageModel = messageModel;
    this.allocateMessageQueueStrategy = allocateMessageQueueStrategy;
    this.mQClientFactory = mQClientFactory;
}
 
Example #6
Source File: MQPullConsumerScheduleService.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
@Override
public void messageQueueChanged(String topic, Set<MessageQueue> mqAll, Set<MessageQueue> mqDivided) {
    MessageModel messageModel =
            MQPullConsumerScheduleService.this.defaultMQPullConsumer.getMessageModel();
    switch (messageModel) {
    case BROADCASTING:
        MQPullConsumerScheduleService.this.putTask(topic, mqAll);
        break;
    case CLUSTERING:
        MQPullConsumerScheduleService.this.putTask(topic, mqDivided);
        break;
    default:
        break;
    }
}
 
Example #7
Source File: RebalanceImpl.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 5 votes vote down vote up
public RebalanceImpl(String consumerGroup, MessageModel messageModel,
                     AllocateMessageQueueStrategy allocateMessageQueueStrategy, MQClientInstance mQClientFactory) {
    this.consumerGroup = consumerGroup;
    this.messageModel = messageModel;
    this.allocateMessageQueueStrategy = allocateMessageQueueStrategy;
    this.mQClientFactory = mQClientFactory;
}
 
Example #8
Source File: ConsumerMeta.java    From onetwo with Apache License 2.0 5 votes vote down vote up
public ConsumerMeta(String groupName, String topic, String... tags) {
	super();
	this.groupName = groupName;
	this.topic = topic;
	this.tags = Arrays.asList(tags);
	this.messageModel = MessageModel.CLUSTERING;
	this.consumeFromWhere = ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET;
	this.ignoreOffSetThreshold = -1;
}
 
Example #9
Source File: ConsumerMeta.java    From onetwo with Apache License 2.0 5 votes vote down vote up
public ConsumerMeta(String groupName, String topic, Collection<String> tags,
		MessageModel messageModel, ConsumeFromWhere consumeFromWhere, long ignoreOffSetThreshold) {
	super();
	this.groupName = groupName;
	this.topic = topic;
	this.tags = tags;
	this.messageModel = messageModel;
	this.consumeFromWhere = consumeFromWhere;
	this.ignoreOffSetThreshold = ignoreOffSetThreshold;
}
 
Example #10
Source File: MQPullConsumerScheduleService.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Override
public void messageQueueChanged(String topic, Set<MessageQueue> mqAll, Set<MessageQueue> mqDivided) {
    MessageModel messageModel =
            MQPullConsumerScheduleService.this.defaultMQPullConsumer.getMessageModel();
    switch (messageModel) {
        case BROADCASTING:
            MQPullConsumerScheduleService.this.putTask(topic, mqAll);
            break;
        case CLUSTERING:
            MQPullConsumerScheduleService.this.putTask(topic, mqDivided);
            break;
        default:
            break;
    }
}
 
Example #11
Source File: MQPullConsumerScheduleService.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void messageQueueChanged(String topic, Set<MessageQueue> mqAll, Set<MessageQueue> mqDivided) {
    MessageModel messageModel =
            MQPullConsumerScheduleService.this.defaultMQPullConsumer.getMessageModel();
    switch (messageModel) {
        case BROADCASTING:
            MQPullConsumerScheduleService.this.putTask(topic, mqAll);
            break;
        case CLUSTERING:
            MQPullConsumerScheduleService.this.putTask(topic, mqDivided);
            break;
        default:
            break;
    }
}
 
Example #12
Source File: MQHelper.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
/**
 * Reset consumer topic offset according to time
 * 
 * @param messageModel
 *            which model
 * @param instanceName
 *            which instance
 * @param consumerGroup
 *            consumer group
 * @param topic
 *            topic
 * @param timestamp
 *            time
 * @throws Exception
 */
public static void resetOffsetByTimestamp(//
        final MessageModel messageModel, //
        final String instanceName, //
        final String consumerGroup, //
        final String topic, //
        final long timestamp) throws Exception {
    final Logger log = ClientLogger.getLog();

    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer(consumerGroup);
    consumer.setInstanceName(instanceName);
    consumer.setMessageModel(messageModel);
    consumer.start();

    Set<MessageQueue> mqs = null;
    try {
        mqs = consumer.fetchSubscribeMessageQueues(topic);
        if (mqs != null && !mqs.isEmpty()) {
            TreeSet<MessageQueue> mqsNew = new TreeSet<MessageQueue>(mqs);
            for (MessageQueue mq : mqsNew) {
                long offset = consumer.searchOffset(mq, timestamp);
                if (offset >= 0) {
                    consumer.updateConsumeOffset(mq, offset);
                    log.info("resetOffsetByTimestamp updateConsumeOffset success, {} {} {}",
                        consumerGroup, offset, mq);
                }
            }
        }
    }
    catch (Exception e) {
        log.warn("resetOffsetByTimestamp Exception", e);
        throw e;
    }
    finally {
        if (mqs != null) {
            consumer.getDefaultMQPullConsumerImpl().getOffsetStore().persistAll(mqs);
        }
        consumer.shutdown();
    }
}
 
Example #13
Source File: MQHelper.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
/**
 * Reset consumer topic offset according to time
 *
 * @param messageModel
 *         which model
 * @param instanceName
 *         which instance
 * @param consumerGroup
 *         consumer group
 * @param topic
 *         topic
 * @param timestamp
 *         time
 *
 * @throws Exception
 */
public static void resetOffsetByTimestamp(//
                                          final MessageModel messageModel,//
                                          final String instanceName,//
                                          final String consumerGroup, //
                                          final String topic, //
                                          final long timestamp) throws Exception {
    final Logger log = ClientLogger.getLog();

    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer(consumerGroup);
    consumer.setInstanceName(instanceName);
    consumer.setMessageModel(messageModel);
    consumer.start();

    Set<MessageQueue> mqs = null;
    try {
        mqs = consumer.fetchSubscribeMessageQueues(topic);
        if (mqs != null && !mqs.isEmpty()) {
            TreeSet<MessageQueue> mqsNew = new TreeSet<MessageQueue>(mqs);
            for (MessageQueue mq : mqsNew) {
                long offset = consumer.searchOffset(mq, timestamp);
                if (offset >= 0) {
                    consumer.updateConsumeOffset(mq, offset);
                    log.info("resetOffsetByTimestamp updateConsumeOffset success, {} {} {}",
                            consumerGroup, offset, mq);
                }
            }
        }
    } catch (Exception e) {
        log.warn("resetOffsetByTimestamp Exception", e);
        throw e;
    } finally {
        if (mqs != null) {
            consumer.getDefaultMQPullConsumerImpl().getOffsetStore().persistAll(mqs);
        }
        consumer.shutdown();
    }
}
 
Example #14
Source File: ConsumerGroupInfo.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
/**
 * 更新netty连接 返回值表示是否发生变更
 */
public boolean updateChannel(final ClientChannelInfo infoNew, ConsumeType consumeType,
        MessageModel messageModel, ConsumeFromWhere consumeFromWhere) {
    boolean updated = false;
    this.consumeType = consumeType;
    this.messageModel = messageModel;
    this.consumeFromWhere = consumeFromWhere;

    ClientChannelInfo infoOld = this.channelInfoTable.get(infoNew.getChannel());
    if (null == infoOld) {
        ClientChannelInfo prev = this.channelInfoTable.put(infoNew.getChannel(), infoNew);
        // 判断是否插入成功
        if (null == prev) {
            log.info("new consumer connected, group: {} {} {} channel: {}", this.groupName, consumeType,
                messageModel, infoNew.toString());
            updated = true;
        }

        infoOld = infoNew;
    }
    else {
        if (!infoOld.getClientId().equals(infoNew.getClientId())) {
            log.error(
                "[BUG] consumer channel exist in broker, but clientId not equal. GROUP: {} OLD: {} NEW: {} ",
                this.groupName, //
                infoOld.toString(), //
                infoNew.toString());
            this.channelInfoTable.put(infoNew.getChannel(), infoNew);
        }
    }

    this.lastUpdateTimestamp = System.currentTimeMillis();
    infoOld.setLastUpdateTimestamp(this.lastUpdateTimestamp);

    return updated;
}
 
Example #15
Source File: MQHelper.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Reset consumer topic offset according to time
 * 
 * @param messageModel
 *            which model
 * @param instanceName
 *            which instance
 * @param consumerGroup
 *            consumer group
 * @param topic
 *            topic
 * @param timestamp
 *            time
 * @throws Exception
 */
public static void resetOffsetByTimestamp(//
        final MessageModel messageModel,//
        final String instanceName,//
        final String consumerGroup, //
        final String topic, //
        final long timestamp) throws Exception {
    final Logger log = ClientLogger.getLog();

    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer(consumerGroup);
    consumer.setInstanceName(instanceName);
    consumer.setMessageModel(messageModel);
    consumer.start();

    Set<MessageQueue> mqs = null;
    try {
        mqs = consumer.fetchSubscribeMessageQueues(topic);
        if (mqs != null && !mqs.isEmpty()) {
            TreeSet<MessageQueue> mqsNew = new TreeSet<MessageQueue>(mqs);
            for (MessageQueue mq : mqsNew) {
                long offset = consumer.searchOffset(mq, timestamp);
                if (offset >= 0) {
                    consumer.updateConsumeOffset(mq, offset);
                    log.info("resetOffsetByTimestamp updateConsumeOffset success, {} {} {}",
                        consumerGroup, offset, mq);
                }
            }
        }
    }
    catch (Exception e) {
        log.warn("resetOffsetByTimestamp Exception", e);
        throw e;
    }
    finally {
        if (mqs != null) {
            consumer.getDefaultMQPullConsumerImpl().getOffsetStore().persistAll(mqs);
        }
        consumer.shutdown();
    }
}
 
Example #16
Source File: RebalancePushImpl.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
@Override
public boolean removeUnnecessaryMessageQueue(MessageQueue mq, ProcessQueue pq) {
    this.defaultMQPushConsumerImpl.getOffsetStore().persist(mq);
    this.defaultMQPushConsumerImpl.getOffsetStore().removeOffset(mq);
    if (this.defaultMQPushConsumerImpl.isConsumeOrderly()
            && MessageModel.CLUSTERING.equals(this.defaultMQPushConsumerImpl.messageModel())) {
        try {
            if (pq.getLockConsume().tryLock(1000, TimeUnit.MILLISECONDS)) {
                try {
                    this.unlock(mq, true);
                    return true;
                }
                finally {
                    pq.getLockConsume().unlock();
                }
            }
            else {
                log.warn("[WRONG]mq is consuming, so can not unlock it, {}. maybe hanged for a while, {}", //
                    mq, //
                    pq.getTryUnlockTimes());

                pq.incTryUnlockTimes();
            }
        }
        catch (Exception e) {
            log.error("removeUnnecessaryMessageQueue Exception", e);
        }

        return false;
    }
    return true;
}
 
Example #17
Source File: ConsumerGroupInfo.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public ConsumerGroupInfo(String groupName, ConsumeType consumeType, MessageModel messageModel,
                         ConsumeFromWhere consumeFromWhere) {
    this.groupName = groupName;
    this.consumeType = consumeType;
    this.messageModel = messageModel;
    this.consumeFromWhere = consumeFromWhere;
}
 
Example #18
Source File: ConsumeMessageOrderlyService.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
public void shutdown() {
    this.stopped = true;
    this.scheduledExecutorService.shutdown();
    this.consumeExecutor.shutdown();
    if (MessageModel.CLUSTERING.equals(this.defaultMQPushConsumerImpl.messageModel())) {
        this.unlockAllMQ();
    }
}
 
Example #19
Source File: ConsumeMessageOrderlyService.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 5 votes vote down vote up
public void shutdown() {
    this.stopped = true;
    this.scheduledExecutorService.shutdown();
    this.consumeExecutor.shutdown();
    if (MessageModel.CLUSTERING.equals(this.defaultMQPushConsumerImpl.messageModel())) {
        this.unlockAllMQ();
    }
}
 
Example #20
Source File: PushConsumer.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException, MQClientException {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_1");
    /**
     * 设置Consumer第一次启动是从队列头部开始消费还是队列尾部开始消费<br>
     * 如果非第一次启动,那么按照上次消费的位置继续消费
     */
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);

    consumer.setMessageModel(MessageModel.BROADCASTING);

    consumer.subscribe("TopicTest", "TagA || TagC || TagD");

    consumer.registerMessageListener(new MessageListenerConcurrently() {

        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
                ConsumeConcurrentlyContext context) {
            System.out.println(Thread.currentThread().getName() + " Receive New Messages: " + msgs);

            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });

    consumer.start();

    System.out.println("Broadcast Consumer Started.");
}
 
Example #21
Source File: RebalanceImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public RebalanceImpl(String consumerGroup, MessageModel messageModel, AllocateMessageQueueStrategy allocateMessageQueueStrategy,
                     MQClientInstance mQClientFactory) {
    this.consumerGroup = consumerGroup;
    this.messageModel = messageModel;
    this.allocateMessageQueueStrategy = allocateMessageQueueStrategy;
    this.mQClientFactory = mQClientFactory;
}
 
Example #22
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.println(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 #23
Source File: ConsumeMessageOrderlyService.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
public void start() {
    if (MessageModel.CLUSTERING
        .equals(ConsumeMessageOrderlyService.this.defaultMQPushConsumerImpl.messageModel())) {
        this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                ConsumeMessageOrderlyService.this.lockMQPeriodically();
            }
        }, 1000 * 1, ProcessQueue.RebalanceLockInterval, TimeUnit.MILLISECONDS);
    }
}
 
Example #24
Source File: ConsumerGroupInfo.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 5 votes vote down vote up
public ConsumerGroupInfo(String groupName, ConsumeType consumeType, MessageModel messageModel,
        ConsumeFromWhere consumeFromWhere) {
    this.groupName = groupName;
    this.consumeType = consumeType;
    this.messageModel = messageModel;
    this.consumeFromWhere = consumeFromWhere;
}
 
Example #25
Source File: ConsumerGroupInfo.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 5 votes vote down vote up
public boolean updateChannel(final ClientChannelInfo infoNew, ConsumeType consumeType,
        MessageModel messageModel, ConsumeFromWhere consumeFromWhere) {
    boolean updated = false;
    this.consumeType = consumeType;
    this.messageModel = messageModel;
    this.consumeFromWhere = consumeFromWhere;

    ClientChannelInfo infoOld = this.channelInfoTable.get(infoNew.getChannel());
    if (null == infoOld) { //原来没有注册对应的通道。
        //把新的通道注册进来 , 先进入的线程能够进入if 分支。
        ClientChannelInfo prev = this.channelInfoTable.put(infoNew.getChannel(), infoNew);
        if (null == prev) {
            log.info("new consumer connected, group: {} {} {} channel: {}", this.groupName, consumeType,
                messageModel, infoNew.toString());
            updated = true;
        }

        infoOld = infoNew;
    }
    else { //必须是相同的clientid 才能更新消费者通道信息。
        if (!infoOld.getClientId().equals(infoNew.getClientId())) {
            log.error(
                "[BUG] consumer channel exist in broker, but clientId not equal. GROUP: {} OLD: {} NEW: {} ",
                this.groupName,//
                infoOld.toString(),//
                infoNew.toString());
            this.channelInfoTable.put(infoNew.getChannel(), infoNew); //clientid不同的时候 ,也认为是加入新的消费者通道信息。
        }
    }

    this.lastUpdateTimestamp = System.currentTimeMillis();
    infoOld.setLastUpdateTimestamp(this.lastUpdateTimestamp);

    return updated;
}
 
Example #26
Source File: ConsumerManager.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 把 消费者(用clientChannelInfo 标识) 注册到指定的消费者分组(group),, 并且包含订阅元数据。
 * (消费类型, 消费模式, 位点类型,订阅的topic列表 。)
 * @param group
 * @param clientChannelInfo
 * @param consumeType
 * @param messageModel
 * @param consumeFromWhere
 * @param subList
 * @return
 */
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;
    }

    //把消费者通道(对应一个clientid ,默认clientip@processid.)注册到消费者分组下。
    boolean r1 =
            consumerGroupInfo.updateChannel(clientChannelInfo, consumeType, messageModel,
                consumeFromWhere);
    boolean r2 = consumerGroupInfo.updateSubscription(subList);

    if (r1 || r2) { //消费者被加入到消费者分组(r1) || 消费者分组增加或者删除了对topic的订阅 , 则给分组中的所有消费者发送
        //消费者id的变更通知, 启动client的rebalance (但这里有一点疑问, 如果消费者退出消费者分组,也应该启动消费者id 的变更通知的。
        // 仔细想一下,因为这里是消费者注册操作, 所以当然是消费者加入消费者分组!!!而消费者退出则应该通过其他的心跳操作来解决。

        //所谓rebalance ,以集群消费为例,其实就是一个消费者分组中的消费者要分摊消费topic下的所有消息。
        //而具体的rebalance算法是按照topic来做的 ,具体是:
        //每一个消费者client 收到reblance指令以后,看自己订阅了哪些topic ,然后按topic分别做rebalance .

        //而每一个topic内的rebalance操作如下:
        //先从client本地缓存的路由表中获取topic归属的brokername下的broker master .
        //然后问broker master要订阅者clientid列表, 然后把topic下的消费队列按均摊算法大致均匀分配给各个clientid ,
        //退出某一个队列的消费者放手(把位点更新到broker)  , 而加入队列的消费者接手(把放手的那个消费者位点拿过来以拉的方式消费)。
        // )
        this.consumerIdsChangeListener.consumerIdsChanged(group, consumerGroupInfo.getAllChannel());
    }

    return r1 || r2;
}
 
Example #27
Source File: ConsumeMessageOrderlyService.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public void shutdown() {
    this.stopped = true;
    this.scheduledExecutorService.shutdown();
    this.consumeExecutor.shutdown();
    if (MessageModel.CLUSTERING.equals(this.defaultMQPushConsumerImpl.messageModel())) {
        this.unlockAllMQ();
    }
}
 
Example #28
Source File: ConsumeMessageOrderlyService.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public void start() {
    if (MessageModel.CLUSTERING.equals(ConsumeMessageOrderlyService.this.defaultMQPushConsumerImpl.messageModel())) {
        this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                ConsumeMessageOrderlyService.this.lockMQPeriodically();
            }
        }, 1000 * 1, ProcessQueue.RebalanceLockInterval, TimeUnit.MILLISECONDS);
    }
}
 
Example #29
Source File: RebalancePushImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Override
public boolean removeUnnecessaryMessageQueue(MessageQueue mq, ProcessQueue pq) {
    this.defaultMQPushConsumerImpl.getOffsetStore().persist(mq);
    this.defaultMQPushConsumerImpl.getOffsetStore().removeOffset(mq);
    if (this.defaultMQPushConsumerImpl.isConsumeOrderly()
            && MessageModel.CLUSTERING.equals(this.defaultMQPushConsumerImpl.messageModel())) {
        try {
            if (pq.getLockConsume().tryLock(1000, TimeUnit.MILLISECONDS)) {
                try {
                    return this.unlockDelay(mq, pq);
                } finally {
                    pq.getLockConsume().unlock();
                }
            } else {
                log.warn("[WRONG]mq is consuming, so can not unlock it, {}. maybe hanged for a while, {}", //
                        mq, //
                        pq.getTryUnlockTimes());

                pq.incTryUnlockTimes();
            }
        } catch (Exception e) {
            log.error("removeUnnecessaryMessageQueue Exception", e);
        }

        return false;
    }
    return true;
}
 
Example #30
Source File: RocketMQConsumer.java    From uavstack with Apache License 2.0 5 votes vote down vote up
private DefaultMQPushConsumer initConsumer(QueueInfo queueInfo) {

        DefaultMQPushConsumer dmpc = new DefaultMQPushConsumer(consumerConfig.getComsumerGroup());
        /**
         * 设置Consumer第一次启动是从队列头部开始消费还是队列尾部开始消费<br>
         * 如果非第一次启动,那么按照上次消费的位置继续消费
         */

        dmpc.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
        dmpc.setNamesrvAddr(consumerConfig.getNamingServer());
        if (consumerConfig.getPullBatchSize() >= 0) {
            dmpc.setPullBatchSize(50);
        }

        // subscribeTopics
        subscribeTopics(dmpc, queueInfo);

        // 配置是否是单线程的consumer监听,因为在处理事务的时候,使用actor模式,需要单线程处理那些数据库写的请求
        if (consumerConfig.getConsumeThreadMax() != null && consumerConfig.getConsumeThreadMax() > 0) {
            dmpc.setConsumeThreadMax(consumerConfig.getConsumeThreadMax());
        }
        if (consumerConfig.getConsumeThreadMin() != null && consumerConfig.getConsumeThreadMin() > 0) {
            dmpc.setConsumeThreadMin(consumerConfig.getConsumeThreadMin());
        }

        // 进行空值测试,如果没有填写queue类型一律按照queue信息算
        if (MQFactory.QueueType.TOPIC.equals(queueInfo.getQueueType())) {
            dmpc.setMessageModel(MessageModel.BROADCASTING);
        }
        return dmpc;
    }