You can enable auto creation of consumer groups in testing or temporary migration scenarios. Do not leave this feature enabled in a production environment for a long time. If you do so, resources may be arbitrarily created due to improper use on the client. In production environments, we recommend that you use the Message Queue for Apache Kafkaconsole or OpenAPI to manage consumer groups.

Prerequisites

Before you begin:
  1. The major version of the Message Queue for Apache Kafka instance is 2.x, and the minor version is the latest. For information about checking the current version and upgrading your Message Queue for Apache Kafka instance, see Upgrade the instance version.
  2. A ticket has been submitted to enable auto creation of consumer groups for Message Queue for Apache Kafka instances.

Background information

After you enable auto creation of consumer groups for a Message Queue for Apache Kafka instance, the Message Queue for Apache Kafka instance creates a consumer group whenever the client requests the metadata of a consumer group that does not exist. For example, if you use a non-existent consumer group to subscribe to messages, the Message Queue for Apache Kafka instance automatically creates the specified consumer group.

Call the consumer API to automatically create consumer groups

After you enable auto creation of consumer groups for an Message Queue for Apache Kafka instance, you can call the consumer API to automatically create consumer groups.

Notice
  • The names of automatically created consumer groups must follow the naming conventions for consumer groups in Message Queue for Apache Kafka. If the name of a consumer group does not follow the naming conventions, the consumer group will not be created.
  • The number of automatically created consumer groups cannot exceed the limit of the Message Queue for Apache Kafka instance. If the number of consumer groups on the instance reaches this limit, additional consumer groups will not be created.
  • Automatically created consumer groups are not managed by the Message Queue for Apache Kafka console. For this reason, the automatically created consumer groups are not displayed on the Consumer Groups page in the Message Queue for Apache Kafka console.
Call the consumer API to automatically create a consumer group.
The following sample code calls the consumer API to automatically create a consumer group:
props.put(ConsumerConfig.GROUP_ID_CONFIG, "newConsumerGroup");
consumer.subscribe(Collections.singletonList("newTopicName"));
// If the specified consumer group does not exist, it will be automatically created.
consumer.poll(Duration.ofSeconds(1));