An article to understand the use of RocketMQ consumer DefaultMQPushConsumer

In an article to understand the use of RocketMQ producer DefaultMQProducer, we introduce the use of RockerMQ producer in detail. In this article, we introduce the use of RocketMQ consumer. There are two implementations of RocketMQ consumer, DefaultMQPushConsumer and DefaultMQPullConsumer, which are respectively pull Mode and push mode. Among them, the pull mode is for consumers to actively send requests and go to the message server to pull messages at regular intervals. The push mode uses a long polling mechanism. The consumer polling mode actively sends requests to the server broker. If the broker detects that there is New messages will be returned immediately, otherwise no messages will be returned temporarily, and the request will be cached locally. The Broker has a thread to detect the pending request, and when there is a new message, it will respond to the request. In this article, we mainly introduce the use of DefaultMQPushConsumer.

Compared with the multiple message sending APIs provided by the DefaultMQProducer producer, the message consumption method provided by DefaultMQPushConsumer is relatively simple. It provides the registerMessageListener method to register a listener for message consumption. The following methods are provided by DefaultMQPushConsumer:

//注册一个回调,在有消息存在时执行并发消费。
public void registerMessageListener(MessageListenerOrderly messageListener) {}
//注册一个回调,以便在消息到达时执行顺序消费。
public void registerMessageListener(MessageListenerConcurrently messageListener) {}

DefaultMQPushConsumer mainly uses the above two methods to send messages from message producers. The following is an example of DefaultMQPushConsumer. Its focus is mainly on the configuration of DefaultMQPushConsumer. Different configurations may have different effects. Let’s look at an example first, and introduce it. The core configuration, the code is as follows:

public class Consumer {
    public static void main(String[] args) throws InterruptedException, MQClientException {
        //实例化消费者,传入消费者组,表示消费同一类消息
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("OnewayProducer");
        //设置nameserver地址
        consumer.setNamesrvAddr("127.0.0.1:9876");
        //设置订阅的主图
        consumer.subscribe("BenchmarkTest", "*");
        //设置如何从何处开始消费
        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
        //注册消息监听器,用于消费生产者生产的消息
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
	        System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);
	        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
	        }
            });
        //启动消费者
        consumer.start();
    }
}

The above code is an example of a simple RocketMQ consumer, instantiating the consumer and setting the nameServer will not be explained here, the following is the core configuration of the RocketMQ consumer:

//订阅主题为topic的消息,并且可以通过subExpression过滤消息
public void subscribe(String topic, String subExpression){}
//订阅主题为topic的消息,可以订阅某Class实例的消息
public void subscribe(String topic, String fullClassName, String filterClassSource){}
//订阅主题为topic的消息,使用MessageSelector过滤消息
public void subscribe(final String topic, final MessageSelector messageSelector){}
//取消订阅主题为topic的消息
public void unsubscribe(String topic){}
//消息模型,支持集群消费和广播消费,默认为集群消费
public void setMessageModel(MessageModel messageModel){}
//Consumer 启动后,默认从什么位置开始消费,默认为CONSUME_FROM_LAST_OFFSET
public void setConsumeFromWhere(ConsumeFromWhere consumeFromWhere) {}
//集群模式下消息队列Rebalance 算法实现策略
public void setAllocateMessageQueueStrategy(AllocateMessageQueueStrategy allocateMessageQueueStrategy){}
//消费进度存储
public void setOffsetStore(OffsetStore offsetStore){}
//消费线程池数量的最大值
public void setConsumeThreadMax(int consumeThreadMax) {}
//消费线程池数量的最小值
public void setConsumeThreadMin(int consumeThreadMin){}
//单队列并行消费允许的最大跨度
public void setConsumeConcurrentlyMaxSpan(int consumeConcurrentlyMaxSpan){}
//默认值为1000,每1000次流控后打印流控日志
public void setPullThresholdForQueue(int pullThresholdForQueue) {}
//推模式下拉取任务间隔时间,默认一次拉取任务完成继续拉取
public void setPullInterval(long pullInterval) {}
//每次消息拉取拉取的消磁条数,默认为32条
public void setPullBatchSize(int pullBatchSize) {}
//消息并发消费时一次消费的消息条数
public void setConsumeMessageBatchMaxSize(int consumeMessageBatchMaxSize) {}
//是否每次拉取消息都更新订阅信息,默认为false
public void setPostSubscriptionWhenPull(boolean postSubscriptionWhenPull) {}
//最大消费重试次数
public void setMaxReconsumeTimes(final int maxReconsumeTimes){}
//延迟将队列的消息提交到消费线程的等待时长,默认为延迟1秒
public void setSuspendCurrentQueueTimeMillis(final long suspendCurrentQueueTimeMillis) {}
//信息消费超时时间,默认为15秒
public void setConsumeTimeout(final long consumeTimeout) {}

Above we introduced the core configuration of consumers. Below we select the important configuration for detailed explanation. Let's first introduce the configuration of the AllocateMessageQueueStrategy load balancing strategy. Its root interface is AllocateMessageQueueStrategy. Below we introduce its implementation, as shown in the following class diagram:

  • AllocateMessageQueueAveragely distributes equally, which is also the default strategy
  • AllocateMessageQueueAveragelyByCircle ring allocation strategy
  • AllocateMessageQueueByConfig manual configuration
  • AllocateMessageQueueConsistentHash consistent Hash allocation
  • AllocateMessageQueueByMachineRoom machine room allocation strategy

 After introducing the distribution strategy, let's look at the setMessageModel configuration, which is used to configure the message model to support cluster consumption and broadcast consumption. The default is cluster consumption. The definition of MessageModel is as follows:

public enum MessageModel {
    /**
     * 广播,消费组中的每个消费者都可以消费所有消息
     */
    BROADCASTING("BROADCASTING"),
    /**
     * 集群,消费组中的每个消费者都可以消费消息中的一部分消息
     */
    CLUSTERING("CLUSTERING");
    ......
}

setConsumeFromWhere is used to configure where to start consumption by default. The default is CONSUME_FROM_LAST_OFFSET. The following is the definition of ConsumeFromWhere:

public enum ConsumeFromWhere {
    //从上次消费的位点开始消费,相当于断电继续
    CONSUME_FROM_LAST_OFFSET,
    //从ConsumeQueue最小位点开始消费
    CONSUME_FROM_FIRST_OFFSET,
    //从指定的时间开始消费
    CONSUME_FROM_TIMESTAMP,
    @Deprecated
    CONSUME_FROM_LAST_OFFSET_AND_FROM_MIN_WHEN_BOOT_FIRST,
    @Deprecated
    CONSUME_FROM_MIN_OFFSET,
    @Deprecated
    CONSUME_FROM_MAX_OFFSET,
}

More consumer examples will not be shown here. In fact, the official has also written a lot of usage examples of RocketMQ. If you are interested, you can find the official github examples. We will introduce the high availability of RocketMQ later.

Guess you like

Origin blog.csdn.net/wk19920726/article/details/108664429