sequence

In this paper, we study the rocketmq ConsumerIdsChangeListener

ConsumerGroupEvent

Rocketmq – all – 4.6.0 – source – release/broker/SRC/main/Java/org/apache/rocketmq/broker/client/ConsumerGroupEvent. Java

public enum ConsumerGroupEvent {

    /**
     * Some consumers in the group are changed.
     */
    CHANGE,
    /**
     * The group of consumer is unregistered.
     */
    UNREGISTER,
    /**
     * The group of consumer is registered.
     */
    REGISTER
}
Copy the code
  • ConsumerGroupEvent defines three enumerated values: CHANGE, UNREGISTER, and REGISTER

ConsumerIdsChangeListener

Rocketmq – all – 4.6.0 – source – release/broker/SRC/main/Java/org/apache/rocketmq/broker/client/ConsumerIdsChangeListener. Java

public interface ConsumerIdsChangeListener {

    void handle(ConsumerGroupEvent event, String group, Object... args);
}
Copy the code
  • ConsumerIdsChangeListener defines the handle method, receiving and processing ConsumerGroupEvent

DefaultConsumerIdsChangeListener

Rocketmq – all – 4.6.0 – source – release/broker/SRC/main/Java/org/apache/rocketmq/broker/client/DefaultConsumerIdsChangeListene r.java

public class DefaultConsumerIdsChangeListener implements ConsumerIdsChangeListener {
    private final BrokerController brokerController;

    public DefaultConsumerIdsChangeListener(BrokerController brokerController) {
        this.brokerController = brokerController;
    }

    @Override
    public void handle(ConsumerGroupEvent event, String group, Object... args) {
        if (event == null) {
            return;
        }
        switch (event) {
            case CHANGE:
                if (args == null || args.length < 1) {
                    return;
                }
                List<Channel> channels = (List<Channel>) args[0];
                if(channels ! = null && brokerController.getBrokerConfig().isNotifyConsumerIdsChangedEnable()) {for(Channel chl : channels) { this.brokerController.getBroker2Client().notifyConsumerIdsChanged(chl, group); }}break;
            case UNREGISTER:
                this.brokerController.getConsumerFilterManager().unRegister(group);
                break;
            case REGISTER:
                if (args == null || args.length < 1) {
                    return;
                }
                Collection<SubscriptionData> subscriptionDataList = (Collection<SubscriptionData>) args[0];
                this.brokerController.getConsumerFilterManager().register(group, subscriptionDataList);
                break;
            default:
                throw new RuntimeException("Unknown event "+ event); }}}Copy the code
  • DefaultConsumerIdsChangeListener ConsumerIdsChangeListener interface is achieved, the handle method using the switch case to handle ConsumerGroupEvent; To CHANGE the type to perform brokerController. GetBroker2Client () notifyConsumerIdsChanged (chlorophyll, group); For the UNREGISTER types perform brokerController. GetConsumerFilterManager (). The UNREGISTER (group); For the REGISTER types perform brokerController. GetConsumerFilterManager (). The REGISTER (group, subscriptionDataList)

summary

ConsumerIdsChangeListener defines the handle method, receiving and processing ConsumerGroupEvent; DefaultConsumerIdsChangeListener ConsumerIdsChangeListener interface is achieved, the handle method using the switch case to handle ConsumerGroupEvent; To CHANGE the type to perform brokerController. GetBroker2Client () notifyConsumerIdsChanged (chlorophyll, group); For the UNREGISTER types perform brokerController. GetConsumerFilterManager (). The UNREGISTER (group); For the REGISTER types perform brokerController. GetConsumerFilterManager (). The REGISTER (group, subscriptionDataList)

doc

  • ConsumerIdsChangeListener