edit-icon download-icon

How to set the number of consumption threads from the MQ client?

Last Updated: Feb 24, 2018

You can do this by setting a ConsumeThreadNums attribute when starting the Consumer. Here’s an example:

  1. public static void main(String[] args) {
  2. Properties properties = new Properties();
  3. properties.put(PropertyKeyConst.ConsumerId, "CID_001");
  4. properties.put(PropertyKeyConst.AccessKey, "xxxxxxxxxxxx");
  5. properties.put(PropertyKeyConst.SecretKey, "xxxxxxxxxxxx");
  6. /**
  7. * Set the fixed number of consumer threads to 20.
  8. */
  9. properties.put(PropertyKeyConst.ConsumeThreadNums,20);
  10. Consumer consumer =ONSFactory.createConsumer(properties);
  11. consumer.subscribe("TestTopic", "*", new MessageListener() {
  12. public Action consume(Message message, ConsumeContext context) {
  13. System.out.println("Receive: " + message);
  14. return Action.CommitMessage;
  15. }
  16. });
  17. consumer.start();
  18. System.out.println("Consumer Started");
  19. }
Thank you! We've received your feedback.