Java Code Examples for org.apache.rocketmq.client.consumer.DefaultMQPushConsumer#setConsumeThreadMax()

The following examples show how to use org.apache.rocketmq.client.consumer.DefaultMQPushConsumer#setConsumeThreadMax() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultMQPushConsumerImplTest.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
@Test
public void checkConfigTest() throws MQClientException {

    //test type
    thrown.expect(MQClientException.class);

    //test message
    thrown.expectMessage("consumeThreadMin (10) is larger than consumeThreadMax (9)");

    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("test_consumer_group");

    consumer.setConsumeThreadMin(10);
    consumer.setConsumeThreadMax(9);

    consumer.registerMessageListener(new MessageListenerConcurrently() {
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
            ConsumeConcurrentlyContext context) {
            System.out.println(" Receive New Messages: " + msgs);
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });

    DefaultMQPushConsumerImpl defaultMQPushConsumerImpl = new DefaultMQPushConsumerImpl(consumer, null);
    defaultMQPushConsumerImpl.start();
}
 
Example 2
Source File: DefaultMQPushConsumerImplTest.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
@Test
public void checkConfigTest() throws MQClientException {

    //test type
    thrown.expect(MQClientException.class);

    //test message
    thrown.expectMessage("consumeThreadMin (10) is larger than consumeThreadMax (9)");

    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("test_consumer_group");

    consumer.setConsumeThreadMin(10);
    consumer.setConsumeThreadMax(9);

    consumer.registerMessageListener(new MessageListenerConcurrently() {
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
            ConsumeConcurrentlyContext context) {
            System.out.println(" Receive New Messages: " + msgs);
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });

    DefaultMQPushConsumerImpl defaultMQPushConsumerImpl = new DefaultMQPushConsumerImpl(consumer, null);
    defaultMQPushConsumerImpl.start();
}
 
Example 3
Source File: DefaultMQPushConsumerImplTest.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
@Test
public void checkConfigTest() throws MQClientException {

    //test type
    thrown.expect(MQClientException.class);

    //test message
    thrown.expectMessage("consumeThreadMin (10) is larger than consumeThreadMax (9)");

    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("test_consumer_group");

    consumer.setConsumeThreadMin(10);
    consumer.setConsumeThreadMax(9);

    consumer.registerMessageListener(new MessageListenerConcurrently() {
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
            ConsumeConcurrentlyContext context) {
            System.out.println(" Receive New Messages: " + msgs);
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });

    DefaultMQPushConsumerImpl defaultMQPushConsumerImpl = new DefaultMQPushConsumerImpl(consumer, null);
    defaultMQPushConsumerImpl.start();
}
 
Example 4
Source File: DefaultMQPushConsumerImplTest.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
@Test
public void checkConfigTest() throws MQClientException {

    //test type
    thrown.expect(MQClientException.class);

    //test message
    thrown.expectMessage("consumeThreadMin (10) is larger than consumeThreadMax (9)");

    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("test_consumer_group");

    consumer.setConsumeThreadMin(10);
    consumer.setConsumeThreadMax(9);

    consumer.registerMessageListener(new MessageListenerConcurrently() {
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
            ConsumeConcurrentlyContext context) {
            System.out.println(" Receive New Messages: " + msgs);
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });

    DefaultMQPushConsumerImpl defaultMQPushConsumerImpl = new DefaultMQPushConsumerImpl(consumer, null);
    defaultMQPushConsumerImpl.start();
}
 
Example 5
Source File: PushConsumer.java    From blog with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static void main(String[] args) throws MQClientException {

		DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("ConsumerGroupName");
		consumer.setNamesrvAddr("192.168.237.128:9876");
		consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
		consumer.subscribe("TopicTest7", "*");
		consumer.setConsumeThreadMin(1);
		consumer.setConsumeThreadMax(1);
		

		consumer.registerMessageListener(new MessageListenerConcurrently() {
			public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
				System.out.printf("Time [" + new Date().toString() + "]," +Thread.currentThread().getName() + "Receive New Messages :" + msgs + "%n");
				return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
			}
		});
		consumer.start();
	}
 
Example 6
Source File: DefaultMQPushConsumerImplTest.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
@Test
public void checkConfigTest() throws MQClientException {

    //test type
    thrown.expect(MQClientException.class);

    //test message
    thrown.expectMessage("consumeThreadMin (10) is larger than consumeThreadMax (9)");

    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("test_consumer_group");

    consumer.setConsumeThreadMin(10);
    consumer.setConsumeThreadMax(9);

    consumer.registerMessageListener(new MessageListenerConcurrently() {
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
            ConsumeConcurrentlyContext context) {
            System.out.println(" Receive New Messages: " + msgs);
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });

    DefaultMQPushConsumerImpl defaultMQPushConsumerImpl = new DefaultMQPushConsumerImpl(consumer, null);
    defaultMQPushConsumerImpl.start();
}
 
Example 7
Source File: Consumer.java    From netty-chat with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer");
        consumer.setNamesrvAddr("localhost:9876");
        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
//        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);
        consumer.subscribe("a", "*");
        consumer.subscribe("p2p_1", "*");
        consumer.subscribe("p2p_2", "*");
        consumer.subscribe("p2p_3", "*");
//        consumer.subscribe("p2p_5", "*");
        consumer.subscribe("group_1", "*");
        consumer.subscribe("group_2", "*");
        consumer.subscribe("group_3", "*");
//        consumer.subscribe("group_4", "*");
//        consumer.subscribe("group_5", "*");
//        consumer.subscribe("group_6", "*");
        consumer.setConsumeThreadMin(1);
        consumer.setConsumeThreadMax(1);
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                System.out.printf("aaaaaaaaaaaaaaa%s %s Receive New Messages: %s %n", new String(msgs.get(0).getBody()), Thread.currentThread().getName(), msgs);
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
//                return null;
            }
        });
        consumer.start();

        System.out.printf("Consumer Started.%n");
    }
 
Example 8
Source File: RocketMQPushConsumerFactory.java    From rocketmq-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
private DefaultMQPushConsumer createDefaultMQPushConsumer(RocketMQConsumerListener rocketMQConsumerListener) {
    RocketMQConsumerConfig config = rocketMQConsumerListener.getConsumerConfig();
    DefaultMQPushConsumer defaultMQPushConsumer = new DefaultMQPushConsumer();
    defaultMQPushConsumer.setNamesrvAddr(nameSrvAddr);
    defaultMQPushConsumer.setConsumerGroup(config.getConsumerGroup());
    Map<String, Class<?>> tags = config.getTags();
    StringBuilder tagBuilder = new StringBuilder();
    List<String> tmpTags = new ArrayList<>(tags.keySet());
    for (int i = 0; i < tmpTags.size(); i++) {
        if (tmpTags.contains("*") && tmpTags.size() > 1) {
            throw new IllegalArgumentException("订阅的tag不合法");
        }
        tagBuilder.append(tmpTags.get(i));
        if (tmpTags.size() > i + 1) {
            tagBuilder.append("||");
        }
    }
    try {
        defaultMQPushConsumer.subscribe(config.getTopic(), tagBuilder.toString());
        defaultMQPushConsumer.subscribe(config.getTopic(), "*");
    } catch (MQClientException e) {
        throw new IllegalArgumentException("订阅语法错误", e);
    }
    defaultMQPushConsumer.setMessageModel(config.getMessageModel());
    if (config.getConsumeThreadMax() == 0) {
        defaultMQPushConsumer.setConsumeThreadMax(this.consumeThreadMax);
    } else {
        defaultMQPushConsumer.setConsumeThreadMax(config.getConsumeThreadMax());
    }
    if (config.getConsumeThreadMin() == 0) {
        defaultMQPushConsumer.setConsumeThreadMin(this.consumeThreadMin);
    } else {
        defaultMQPushConsumer.setConsumeThreadMin(config.getConsumeThreadMin());
    }
    return defaultMQPushConsumer;
}
 
Example 9
Source File: AliyunMqConfiguration.java    From paascloud-master with Apache License 2.0 5 votes vote down vote up
/**
 * Default mq push consumer default mq push consumer.
 *
 * @return the default mq push consumer
 *
 * @throws MQClientException the mq client exception
 */
@Bean
public DefaultMQPushConsumer defaultMQPushConsumer() throws MQClientException {
	DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(paascloudProperties.getAliyun().getRocketMq().getConsumerGroup());
	consumer.setNamesrvAddr(paascloudProperties.getAliyun().getRocketMq().getNamesrvAddr());
	consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);

	String[] strArray = AliyunMqTopicConstants.ConsumerTopics.OPT.split(GlobalConstant.Symbol.COMMA);
	for (String aStrArray : strArray) {
		String[] topicArray = aStrArray.split(GlobalConstant.Symbol.AT);
		String topic = topicArray[0];
		String tags = topicArray[1];
		if (PublicUtil.isEmpty(tags)) {
			tags = "*";
		}
		consumer.subscribe(topic, tags);
		log.info("RocketMq OpcPushConsumer topic = {}, tags={}", topic, tags);
	}

	consumer.registerMessageListener(optPushConsumer);
	consumer.setConsumeThreadMax(2);
	consumer.setConsumeThreadMin(2);

	taskExecutor.execute(() -> {
		try {
			Thread.sleep(5000);
			consumer.start();
			log.info("RocketMq OpcPushConsumer OK.");
		} catch (InterruptedException | MQClientException e) {
			log.error("RocketMq OpcPushConsumer, 出现异常={}", e.getMessage(), e);
		}
	});
	return consumer;
}
 
Example 10
Source File: AliyunMqConfiguration.java    From paascloud-master with Apache License 2.0 5 votes vote down vote up
/**
 * Default mq push consumer default mq push consumer.
 *
 * @return the default mq push consumer
 *
 * @throws MQClientException the mq client exception
 */
@Bean
public DefaultMQPushConsumer defaultMQPushConsumer() throws MQClientException {
	DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(paascloudProperties.getAliyun().getRocketMq().getConsumerGroup());
	consumer.setNamesrvAddr(paascloudProperties.getAliyun().getRocketMq().getNamesrvAddr());
	consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);

	String[] strArray = AliyunMqTopicConstants.ConsumerTopics.UAC.split(GlobalConstant.Symbol.COMMA);
	for (String aStrArray : strArray) {
		String[] topicArray = aStrArray.split(GlobalConstant.Symbol.AT);
		String topic = topicArray[0];
		String tags = topicArray[1];
		if (PublicUtil.isEmpty(tags)) {
			tags = "*";
		}
		consumer.subscribe(topic, tags);
		log.info("RocketMq UacPushConsumer topic = {}, tags={}", topic, tags);
	}

	consumer.registerMessageListener(uacPushMessageListener);
	consumer.setConsumeThreadMax(2);
	consumer.setConsumeThreadMin(2);

	taskExecutor.execute(() -> {
		try {
			Thread.sleep(5000);
			consumer.start();
			log.info("RocketMq UacPushConsumer OK.");
		} catch (InterruptedException | MQClientException e) {
			log.error("RocketMq OpcPushConsumer, 出现异常={}", e.getMessage(), e);
		}
	});
	return consumer;
}
 
Example 11
Source File: Consumer.java    From learning-code with Apache License 2.0 5 votes vote down vote up
@Bean
public DefaultMQPushConsumer getRocketMQConsumer() {

    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(groupName);
    consumer.setNamesrvAddr(namesrvAddr);
    consumer.setConsumeThreadMin(consumeThreadMin);
    consumer.setConsumeThreadMax(consumeThreadMax);
    consumer.registerMessageListener(mqMessageListenerProcessor);
    /**
     * 设置Consumer第一次启动是从队列头部开始消费还是队列尾部开始消费
     * 如果非第一次启动,那么按照上次消费的位置继续消费
     */
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);
    /**
     * 设置消费模型,集群还是广播,默认为集群
     */
    //consumer.setMessageModel(MessageModel.CLUSTERING);
    /**
     * 设置一次消费消息的条数,默认为1条
     */
    consumer.setConsumeMessageBatchMaxSize(consumeMessageBatchMaxSize);
    try {
        /**
         * 设置该消费者订阅的主题和tag,如果是订阅该主题下的所有tag,则tag使用*;如果需要指定订阅该主题下的某些tag,则使用||分割,例如tag1||tag2||tag3
         */
        String[] topicTagsArr = topics.split(";");
        for (String topicTags : topicTagsArr) {
            String[] topicTag = topicTags.split("~");
            consumer.subscribe(topicTag[0], topicTag[1]);
        }
        consumer.start();
        log.info("consumer is start !!! groupName:{},topics:{},namesrvAddr:{}", groupName, topics, namesrvAddr);
    } catch (MQClientException e) {
        log.error("consumer is start !!! groupName:{},topics:{},namesrvAddr:{}", groupName, topics, namesrvAddr, e);
    }
    return consumer;
}
 
Example 12
Source File: OrderConsumer.java    From blog with MIT License 5 votes vote down vote up
public static void main(String[] args) throws MQClientException {

    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("Order_Consumer");
    consumer.setNamesrvAddr("127.0.0.1:9876");
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    consumer.subscribe("OrderTopic", "*");

    /** TODO: 设置最小消费线程数 */
    consumer.setConsumeThreadMin(1);
    /** TODO: 设置最大消费线程数 */
    consumer.setConsumeThreadMax(1);

    consumer.registerMessageListener(
        // 顺序消费,一个 Queue 对应一个线程
        new MessageListenerOrderly() {
          @Override
          public ConsumeOrderlyStatus consumeMessage(
              List<MessageExt> msgs, ConsumeOrderlyContext context) {
            System.out.println(
                Thread.currentThread().getName()
                    + "  message: "
                    + new String(msgs.get(0).getBody())
                    + "  QueueID: "
                    + msgs.get(0).getQueueId());
            return ConsumeOrderlyStatus.SUCCESS;
          }
        });

    consumer.start();
    System.out.println("Consumer 已启动");
  }
 
Example 13
Source File: AbstractRocketMqConsumer.java    From spring-boot-rocketmq-starter with Apache License 2.0 5 votes vote down vote up
private void initRocketMQPushConsumer() throws MQClientException {

        Assert.notNull(getConsumerGroup(), "Property 'consumerGroup' is required");
        Assert.notEmpty(subscribeTopicTags(), "subscribeTopicTags method can't be empty");

        consumer = new DefaultMQPushConsumer(getConsumerGroup());
        if (consumeThreadMax != null) {
            consumer.setConsumeThreadMax(consumeThreadMax);
        }
        if (consumeThreadMax != null && consumeThreadMax < consumer.getConsumeThreadMin()) {
            consumer.setConsumeThreadMin(consumeThreadMax);
        }

        consumer.setConsumeFromWhere(consumeFromWhere);
        consumer.setMessageModel(messageModel);

        switch (consumeMode) {
            case Orderly:
                consumer.setMessageListener(new DefaultMessageListenerOrderly());
                break;
            case CONCURRENTLY:
                consumer.setMessageListener(new DefaultMessageListenerConcurrently());
                break;
            default:
                throw new IllegalArgumentException("Property 'consumeMode' was wrong.");
        }

    }
 
Example 14
Source File: Consumer.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws InterruptedException, MQClientException {

        /*
         * Instantiate with specified consumer group name.
         */
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_4");
        consumer.setConsumeThreadMin(1);
        consumer.setConsumeThreadMax(1);

//        consumer.setConsumeMessageBatchMaxSize(2);

        consumer.setNamesrvAddr("127.0.0.1:9876"); // TODO add by yunai

        /*
         * Specify name server addresses.
         * <p/>
         *
         * Alternatively, you may specify name server addresses via exporting environmental variable: NAMESRV_ADDR
         * <pre>
         * {@code
         * consumer.setNamesrvAddr("name-server1-ip:9876;name-server2-ip:9876");
         * }
         * </pre>
         */

        /*
         * Specify where to start in case the specified consumer group is a brand new one.
         */
//        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);

        /*
         * Subscribe one more more topics to consume.
         */
//        consumer.subscribe("TopicRead3", "*");
//        consumer.subscribe("TopicTest", "*");
        consumer.subscribe("TopicTest_mis", "*");

        /*
         *  Register callback to execute on arrival of messages fetched from brokers.
         */
        consumer.registerMessageListener(new MessageListenerConcurrently() {

            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
                ConsumeConcurrentlyContext context) {
                System.out.printf(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n");
                return ConsumeConcurrentlyStatus.RECONSUME_LATER;
//                return ConsumeConcurrentlyStatus.RECONSUME_LATER;
            }
        });

        /*
         *  Launch the consumer instance.
         */
        consumer.start();

        System.out.printf("Consumer Started.%n");
    }
 
Example 15
Source File: DefaultRocketMQListenerContainer.java    From spring-boot-starter-rocketmq with Apache License 2.0 4 votes vote down vote up
private void initRocketMQPushConsumer() throws MQClientException {

        Assert.notNull(rocketMQListener, "Property 'rocketMQListener' is required");
        Assert.notNull(consumerGroup, "Property 'consumerGroup' is required");
        Assert.notNull(nameServer, "Property 'nameServer' is required");
        Assert.notNull(topic, "Property 'topic' is required");

        consumer = new DefaultMQPushConsumer(consumerGroup);
        consumer.setNamesrvAddr(nameServer);
        consumer.setConsumeThreadMax(consumeThreadMax);
        if (consumeThreadMax < consumer.getConsumeThreadMin()) {
            consumer.setConsumeThreadMin(consumeThreadMax);
        }
        consumer.setPullThresholdForTopic(pullThresholdForTopic);
        consumer.setPullThresholdSizeForTopic(pullThresholdSizeForTopic);

        consumer.setMessageModel(messageModel);

        switch (selectorType) {
            case TAG:
                consumer.subscribe(topic, selectorExpress);
                break;
            case SQL92:
                consumer.subscribe(topic, MessageSelector.bySql(selectorExpress));
                break;
            default:
                throw new IllegalArgumentException("Property 'selectorType' was wrong.");
        }

        switch (consumeMode) {
            case ORDERLY:
                consumer.setMessageListener(new DefaultMessageListenerOrderly());
                break;
            case CONCURRENTLY:
                consumer.setMessageListener(new DefaultMessageListenerConcurrently());
                break;
            default:
                throw new IllegalArgumentException("Property 'consumeMode' was wrong.");
        }

        // provide an entryway to custom setting RocketMQ consumer
        if (rocketMQListener instanceof RocketMQPushConsumerLifecycleListener) {
            ((RocketMQPushConsumerLifecycleListener) rocketMQListener).prepareStart(consumer);
        }

    }