RoketMq源码--Consumer的构造

DefaultMQPushConsumer的构造

实例代码

        // 设置group
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("test_consumer_group");
        // 注册消费监听
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            public ConsumeConcurrentlyStatus consumeMessage(List msgs,
                ConsumeConcurrentlyContext context) {
                System.out.println(" Receive New Messages: " + msgs);
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
    // 启动
    consumer.start();

consumer构造

public DefaultMQPushConsumer(final String namespace, final String consumerGroup, RPCHook rpcHook,
    AllocateMessageQueueStrategy allocateMessageQueueStrategy) {
    this.consumerGroup = consumerGroup;
    this.namespace = namespace;
    // 设置分配策略
    this.allocateMessageQueueStrategy = allocateMessageQueueStrategy;
    defaultMQPushConsumerImpl = new DefaultMQPushConsumerImpl(this, rpcHook);
}

defaultMQPushConsumerImpl的构造

public DefaultMQPushConsumerImpl(DefaultMQPushConsumer defaultMQPushConsumer, RPCHook rpcHook) {
    this.defaultMQPushConsumer = defaultMQPushConsumer;
    this.rpcHook = rpcHook;
    // 有一个pull的发生异常后延迟多久再接着处理的一个时间
    this.pullTimeDelayMillsWhenException = defaultMQPushConsumer.getPullTimeDelayMillsWhenException();
}

总结

consumer构造主要是构造内部的defaultMQPushConsumerImpl,然后需要了解一下分配的策略,有一个细节,就是这个pull过程中发生异常了的一个延迟处理的时间

你可能感兴趣的:(rocketmq)