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

The following examples show how to use org.apache.rocketmq.client.consumer.DefaultMQPushConsumer#setMessageListener() . 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: ConsumerTest.java    From message-queue-client-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception {

    RocketmqMessageConsumerListener messageConsumerListener = new RocketmqMessageConsumerListener();

    messageConsumerListener.setModel("MODEL_1");
    messageConsumerListener.setBatch("NON_BATCH");
    messageConsumerListener.setMessageDecoder(new RocketmqMessageDecoderDemo());
    messageConsumerListener.setConsumer(new MessageConsumer());

    DefaultMQPushConsumer defaultMQPushConsumer = new DefaultMQPushConsumer();

    defaultMQPushConsumer.setNamesrvAddr("localhost:9876");
    defaultMQPushConsumer.setConsumerGroup("contest");
    defaultMQPushConsumer.subscribe("QUEUE_TEST", "*");
    defaultMQPushConsumer.setMessageListener(messageConsumerListener.getMessageListener());

    defaultMQPushConsumer.start();
}
 
Example 2
Source File: RocketMQEventConfig.java    From enode with MIT License 5 votes vote down vote up
@Bean(initMethod = "start", destroyMethod = "shutdown")
public DefaultMQPushConsumer eventConsumer(RocketMQDomainEventListener domainEventListener) throws MQClientException {
    DefaultMQPushConsumer defaultMQPushConsumer = new DefaultMQPushConsumer();
    defaultMQPushConsumer.setConsumerGroup(QueueProperties.DEFAULT_CONSUMER_GROUP);
    defaultMQPushConsumer.setNamesrvAddr(QueueProperties.NAMESRVADDR);
    defaultMQPushConsumer.subscribe(QueueProperties.EVENT_TOPIC, "*");
    defaultMQPushConsumer.setMessageListener(domainEventListener);
    return defaultMQPushConsumer;
}
 
Example 3
Source File: RocketMQEventConfig.java    From enode with MIT License 5 votes vote down vote up
@Bean(initMethod = "start", destroyMethod = "shutdown")
public DefaultMQPushConsumer applicationConsumer(RocketMQApplicationMessageListener applicationMessageListener) throws MQClientException {
    DefaultMQPushConsumer defaultMQPushConsumer = new DefaultMQPushConsumer();
    defaultMQPushConsumer.setConsumerGroup(QueueProperties.DEFAULT_CONSUMER_GROUP1);
    defaultMQPushConsumer.setNamesrvAddr(QueueProperties.NAMESRVADDR);
    defaultMQPushConsumer.subscribe(QueueProperties.APPLICATION_TOPIC, "*");
    defaultMQPushConsumer.setMessageListener(applicationMessageListener);
    return defaultMQPushConsumer;
}
 
Example 4
Source File: RocketMQEventConfig.java    From enode with MIT License 5 votes vote down vote up
@Bean(initMethod = "start", destroyMethod = "shutdown")
public DefaultMQPushConsumer exceptionConsumer(RocketMQPublishableExceptionListener publishableExceptionListener) throws MQClientException {
    DefaultMQPushConsumer defaultMQPushConsumer = new DefaultMQPushConsumer();
    defaultMQPushConsumer.setConsumerGroup(QueueProperties.DEFAULT_CONSUMER_GROUP2);
    defaultMQPushConsumer.setNamesrvAddr(QueueProperties.NAMESRVADDR);
    defaultMQPushConsumer.subscribe(QueueProperties.EXCEPTION_TOPIC, "*");
    defaultMQPushConsumer.setMessageListener(publishableExceptionListener);
    return defaultMQPushConsumer;
}
 
Example 5
Source File: RMQNormalConsumer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public void create(boolean useTLS) {
    consumer = new DefaultMQPushConsumer(consumerGroup);
    consumer.setInstanceName(RandomUtil.getStringByUUID());
    consumer.setNamesrvAddr(nsAddr);
    try {
        consumer.subscribe(topic, subExpression);
    } catch (MQClientException e) {
        logger.error("consumer subscribe failed!");
        e.printStackTrace();
    }
    consumer.setMessageListener(listener);
    consumer.setUseTLS(useTLS);
}
 
Example 6
Source File: RMQNormalConsumer.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
public void create(boolean useTLS) {
    consumer = new DefaultMQPushConsumer(consumerGroup);
    consumer.setInstanceName(RandomUtil.getStringByUUID());
    consumer.setNamesrvAddr(nsAddr);
    try {
        consumer.subscribe(topic, subExpression);
    } catch (MQClientException e) {
        logger.error("consumer subscribe failed!");
        e.printStackTrace();
    }
    consumer.setMessageListener(listener);
    consumer.setUseTLS(useTLS);
}
 
Example 7
Source File: RMQNormalConsumer.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
public void create(boolean useTLS) {
    consumer = new DefaultMQPushConsumer(consumerGroup);
    consumer.setInstanceName(RandomUtil.getStringByUUID());
    consumer.setNamesrvAddr(nsAddr);
    try {
        consumer.subscribe(topic, subExpression);
    } catch (MQClientException e) {
        logger.error("consumer subscribe failed!");
        e.printStackTrace();
    }
    consumer.setMessageListener(listener);
    consumer.setUseTLS(useTLS);
}
 
Example 8
Source File: RMQNormalConsumer.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public void create() {
    consumer = new DefaultMQPushConsumer(consumerGroup);
    consumer.setInstanceName(RandomUtil.getStringByUUID());
    consumer.setNamesrvAddr(nsAddr);
    try {
        consumer.subscribe(topic, subExpression);
    } catch (MQClientException e) {
        logger.error("consumer subscribe failed!");
        e.printStackTrace();
    }
    consumer.setMessageListener(listner);
}
 
Example 9
Source File: RMQNormalConsumer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public void create(boolean useTLS) {
    consumer = new DefaultMQPushConsumer(consumerGroup);
    consumer.setInstanceName(RandomUtil.getStringByUUID());
    consumer.setNamesrvAddr(nsAddr);
    try {
        consumer.subscribe(topic, subExpression);
    } catch (MQClientException e) {
        logger.error("consumer subscribe failed!");
        e.printStackTrace();
    }
    consumer.setMessageListener(listener);
    consumer.setUseTLS(useTLS);
}
 
Example 10
Source File: RMQNormalConsumer.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
public void create() {
    consumer = new DefaultMQPushConsumer(consumerGroup);
    consumer.setInstanceName(RandomUtil.getStringByUUID());
    consumer.setNamesrvAddr(nsAddr);
    try {
        consumer.subscribe(topic, subExpression);
    } catch (MQClientException e) {
        logger.error("consumer subscribe failed!");
        e.printStackTrace();
    }
    consumer.setMessageListener(listner);
}
 
Example 11
Source File: RMQNormalConsumer.java    From rocketmq_trans_message with Apache License 2.0 5 votes vote down vote up
public void create() {
    consumer = new DefaultMQPushConsumer(consumerGroup);
    consumer.setInstanceName(RandomUtil.getStringByUUID());
    consumer.setNamesrvAddr(nsAddr);
    try {
        consumer.subscribe(topic, subExpression);
    } catch (MQClientException e) {
        logger.error("consumer subscribe failed!");
        e.printStackTrace();
    }
    consumer.setMessageListener(listner);
}
 
Example 12
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 13
Source File: RMQNormalConsumer.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public void create(boolean useTLS) {
    consumer = new DefaultMQPushConsumer(consumerGroup);
    consumer.setInstanceName(RandomUtil.getStringByUUID());
    consumer.setNamesrvAddr(nsAddr);
    try {
        consumer.subscribe(topic, subExpression);
    } catch (MQClientException e) {
        logger.error("consumer subscribe failed!");
        e.printStackTrace();
    }
    consumer.setMessageListener(listener);
    consumer.setUseTLS(useTLS);
}
 
Example 14
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);
        }

    }