This topic describes how to subscribe to messages by using the TCP client SDK for Java provided by ApsaraMQ for RocketMQ.
Subscription modes
ApsaraMQ for RocketMQ supports the following subscription modes:
Clustering subscription
All consumers identified by the same group ID consume an equal number of messages. For example, a topic contains nine messages and a consumer group contains three consumers. In clustering consumption mode, each consumer consumes three messages. The following code shows how to configure the clustering subscription mode:
// Configure clustering subscription, which is the default mode.
properties.put(PropertyKeyConst.MessageModel, PropertyValueConst.CLUSTERING);
Broadcasting subscription
Each of the consumers identified by the same group ID consumes all messages once. For example, a topic contains nine messages and a consumer group contains three consumers. In broadcasting consumption mode, each consumer consumes nine messages. The following code shows how to configure the broadcasting subscription mode:
// Configure broadcasting subscription.
properties.put(PropertyKeyConst.MessageModel, PropertyValueConst.BROADCASTING);
Note
You must maintain consistent subscriptions for all consumer instances identified by the same group ID. For more information, see Subscription consistency.
Different limits are imposed on the preceding subscription modes. For example, in broadcasting subscription mode, you cannot send or receive ordered messages, maintain consumption progress, or reset consumer offsets. For more information, see Clustering consumption and broadcasting consumption.
Modes for obtaining messages
ApsaraMQ for RocketMQ allows you to obtain messages by using one of the following modes:
Push: Messages are pushed from ApsaraMQ for RocketMQ to consumers. In push mode, ApsaraMQ for RocketMQ supports the batch consumption feature. This feature allows you to send messages to consumers in a batch. For more information, see Batch consumption.
Pull: Messages are pulled from ApsaraMQ for RocketMQ by consumers.
Compared with the push mode, the pull mode provides more options in message receiving and allows you more freedom in message pulling. For more information, see Methods and parameters.
Important
To use pull consumers, make sure that your ApsaraMQ for RocketMQ instance is of the Enterprise Platinum Edition.
Pull consumers can access ApsaraMQ for RocketMQ instances only in virtual private clouds (VPCs).
Sample code
For information about the detailed sample code, see the ApsaraMQ for RocketMQ code repository. The following items provide the sample code on how to subscribe to messages by using the push and pull modes.