TCP SDK for Java provided by Message Queue for Apache RocketMQ supports multi-thread consumption and is applicable to all message types. This topic describes how to set the number of consumption threads.

To set the number of consumption threads on the Message Queue for Apache RocketMQ client, add the ConsumeThreadNums property when you start the consumer. The following code shows examples of this property:

public static void main(String[] args) {
    Properties properties = new Properties();
    properties.put(PropertyKeyConst.GROUP_ID, "GID_001");
    properties.put(PropertyKeyConst.AccessKey, "xxxxxxxxxxxx");
    properties.put(PropertyKeyConst.SecretKey, "xxxxxxxxxxxx");
    /**
     * Set the number of consumption threads to 20.
     */
    properties.setProperty(PropertyKeyConst.ConsumeThreadNums,"20");
    Consumer consumer =ONSFactory.createConsumer(properties);
    consumer.subscribe("TestTopic", "*", new MessageListener() {
        public Action consume(Message message, ConsumeContext context) {
            System.out.println("Receive: " + message);
            return Action.CommitMessage;
        }
    });
    consumer.start();
    System.out.println("Consumer Started");
}