RocketMQ message system - pull mode

RocketMQ message system - pull mode

1. Consumption pattern

The common consumption modes of the message system are divided into push mode and pull mode. The push mode is that the server actively pushes data to the client, which is also a relatively common mode. The pull mode is that the client actively pulls data from the server.

 

1.1 push mode

① The server saves the push state and retransmits it if it fails;

② The client is stateless;

③ push is real-time;

④ Load balancing, unified processing and control on the server side, flow control needs to be done according to the consumption capacity of consumers;

 

1.2 pull mode

① The server is stateless;

② The client saves the current pull state for recovery in case of failure or restart;

③ pull is divided into long polling (real-time) and short polling (related to the time interval of pull);

④ Load balancing, consumer self-control;

 

 

Second, the principle of pull mode


  

PullMessageTask:

① Pull every 3s;

② Update the offset of the pulled queue to the broker;

③ Each queue starts a PullMessageTask, which is responsible for pulling messages from this queue; 

④ Consumption failure, or the message of an error when the listener is executed, is saved in the H2 database;

 

ReconsumerMessgaeTask:

For messages that fail to consume, get them from the database and re-consume them;

 

3. Use of pull mode

3.1 Consumers

Message topic: mengka-cc2

groupId:  consumerG2

Message Mode: PULL

Pull interval: 3000ms

 

<bean id="consumerNotifyManager" class="com.mengka.mq.client.NotifyManagerBean" init-method="init">
        <property name="groupId" value="consumerG2" />
        <property name="name" value="taaNotifyManager" />
        <property name="topic" value="mengka-cc2"/>
        <property name="ctype" value="PULL"/>
        <property name="namesrvAddr" value="192.168.1.42:9876"/>
</bean>

<bean id="pullMessageManager" class="com.mengka.mq.listener.PullMessageManager">
        <property name="consumerNotifyManager" ref="consumerNotifyManager"/>
        <property name="messageListener" ref="taaPullMessageListener" />
        <property name="spaceTime" value="3000"/>
 </bean>

 

/**
 * User: mengka
 * Date: 15-8-8
 */
@Component
public class TaaPullMessageListener implements MessageListenerPull {

    private static final Logger log = LoggerFactory.getLogger(TaaPullMessageListener.class);

    @Override
    public ConsumeConcurrentlyStatus consumeMessage(MessageExt msg) {
        log.info("---------------, receive message id = "+msg.getMsgId()+" , content = "+new String(msg.getBody())+" , tags = "+msg.getTags());
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    }
}

 

 

3.2 Producers

Message topic: mengka-cc2

groupId:  consumerG2

 

<bean id="producterNotifyManager" class="com.mengka.mq.client.NotifyManagerBean" init-method="initProducter">
        <property name="groupId" value="consumerG2" />
        <property name="name" value="taaNotifyManager" />
        <property name="topic" value="mengka-cc2"/>
        <property name="namesrvAddr" value="192.168.1.42:9876"/>
</bean>

 

String serviceConfigXMLs[] = new String[]{"rocketmq_pull_06/rocketmq-pull-producer.xml"};
ApplicationContext context = new ClassPathXmlApplicationContext(serviceConfigXMLs);
NotifyManager producterNotifyManager = (NotifyManager) context.getBean("producterNotifyManager");

String content = "Just for test[" + TimeUtil.toDate(new Date(), TimeUtil.format_1);
Message message = new BytesMessage(content.getBytes());
SendResult result = producterNotifyManager.sendMessage(message);

 

 

3.3 Pull message consumption

After the producer sends the message, in the next poll, the consumer pulls the unconsumed message data, and updates the offset to the server after the message is successfully consumed;

 
 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327058645&siteId=291194637