Search in sources :

Example 1 with MessageListenerConcurrently

use of org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently in project rocketmq-externals by apache.

the class RocketMQConsoleTestBase method startTestMQConsumer.

protected void startTestMQConsumer() {
    // test online consumer
    consumer = new DefaultMQPushConsumer(TEST_CONSUMER_GROUP);
    consumerConfigInfo.setBrokerNameList(Lists.newArrayList(TestConstant.TEST_BROKER_NAME));
    SubscriptionGroupConfig subscriptionGroupConfig = new SubscriptionGroupConfig();
    subscriptionGroupConfig.setGroupName(TEST_CONSUMER_GROUP);
    subscriptionGroupConfig.setRetryQueueNums(RETRY_QUEUE_NUMS);
    consumerConfigInfo.setSubscriptionGroupConfig(subscriptionGroupConfig);
    consumerService.createAndUpdateSubscriptionGroupConfig(consumerConfigInfo);
    consumer.setNamesrvAddr(TestConstant.NAME_SERVER_ADDRESS);
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    try {
        consumer.subscribe(TEST_CONSOLE_TOPIC, "*");
        consumer.registerMessageListener(new MessageListenerConcurrently() {

            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                consoleTestBaseLog.info("op=consumeMessage message={}", JsonUtil.obj2String(msgs));
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        consumer.start();
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : ConsumeConcurrentlyContext(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext) MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageListenerConcurrently(org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently) ConsumeConcurrentlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) SubscriptionGroupConfig(org.apache.rocketmq.common.subscription.SubscriptionGroupConfig)

Example 2 with MessageListenerConcurrently

use of org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently in project rocketmq-externals by apache.

the class ConsumerTest method main.

public static void main(String[] args) {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("g00003");
    consumer.setNamesrvAddr("localhost:9876");
    try {
        consumer.subscribe("flink-sink2", "*");
    } catch (MQClientException e) {
        e.printStackTrace();
    }
    consumer.registerMessageListener(new MessageListenerConcurrently() {

        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
            for (MessageExt msg : msgs) {
                System.out.println(msg.getKeys() + ":" + new String(msg.getBody()));
            }
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });
    try {
        consumer.start();
    } catch (MQClientException e) {
        e.printStackTrace();
    }
}
Also used : ConsumeConcurrentlyContext(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext) MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageListenerConcurrently(org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently) ConsumeConcurrentlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 3 with MessageListenerConcurrently

use of org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently in project spring-boot-starter-samples by vindell.

the class ScheduledMessageConsumer method main.

public static void main(String[] args) throws Exception {
    // Instantiate message consumer
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("ExampleConsumer");
    // Subscribe topics
    consumer.subscribe("TestTopic", "*");
    // Register message listener
    consumer.registerMessageListener(new MessageListenerConcurrently() {

        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> messages, ConsumeConcurrentlyContext context) {
            for (MessageExt message : messages) {
                // Print approximate delay time period
                System.out.println("Receive message[msgId=" + message.getMsgId() + "] " + (System.currentTimeMillis() - message.getStoreTimestamp()) + "ms later");
            }
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });
    // Launch consumer
    consumer.start();
}
Also used : ConsumeConcurrentlyContext(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext) MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageListenerConcurrently(org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently) ConsumeConcurrentlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer)

Example 4 with MessageListenerConcurrently

use of org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently in project rocketmq-externals by apache.

the class TestController method list.

@RequestMapping(value = "/runTask.do", method = RequestMethod.GET)
@ResponseBody
public Object list() throws MQClientException, RemotingException, InterruptedException {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(testTopic + "Group");
    consumer.setNamesrvAddr(rMQConfigure.getNamesrvAddr());
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    consumer.subscribe(testTopic, "*");
    consumer.registerMessageListener(new MessageListenerConcurrently() {

        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
            logger.info("receiveMessage msgSize={}", msgs.size());
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });
    consumer.start();
    final DefaultMQProducer producer = new DefaultMQProducer(testTopic + "Group");
    producer.setInstanceName(String.valueOf(System.currentTimeMillis()));
    producer.setNamesrvAddr(rMQConfigure.getNamesrvAddr());
    producer.start();
    new Thread(new Runnable() {

        @Override
        public void run() {
            int i = 0;
            while (true) {
                try {
                    Message msg = new Message(testTopic, "TagA" + i, "KEYS" + i, ("Hello RocketMQ " + i).getBytes());
                    Thread.sleep(1000L);
                    SendResult sendResult = producer.send(msg);
                    logger.info("sendMessage={}", JsonUtil.obj2String(sendResult));
                } catch (Exception e) {
                    e.printStackTrace();
                    try {
                        Thread.sleep(1000);
                    } catch (Exception ignore) {
                    }
                }
            }
        }
    }).start();
    return true;
}
Also used : MessageListenerConcurrently(org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently) Message(org.apache.rocketmq.common.message.Message) ConsumeConcurrentlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) MQClientException(org.apache.rocketmq.client.exception.MQClientException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) ConsumeConcurrentlyContext(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext) MessageExt(org.apache.rocketmq.common.message.MessageExt) SendResult(org.apache.rocketmq.client.producer.SendResult) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 5 with MessageListenerConcurrently

use of org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently in project rocketmq-externals by apache.

the class RocketMQReceiver method onStart.

@Override
public void onStart() {
    Validate.notEmpty(properties, "Consumer properties can not be empty");
    ordered = RocketMqUtils.getBoolean(properties, RocketMQConfig.CONSUMER_MESSAGES_ORDERLY, false);
    consumer = new DefaultMQPushConsumer();
    RocketMQConfig.buildConsumerConfigs(properties, (DefaultMQPushConsumer) consumer);
    if (ordered) {
        consumer.registerMessageListener(new MessageListenerOrderly() {

            @Override
            public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
                if (process(msgs)) {
                    return ConsumeOrderlyStatus.SUCCESS;
                } else {
                    return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT;
                }
            }
        });
    } else {
        consumer.registerMessageListener(new MessageListenerConcurrently() {

            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                if (process(msgs)) {
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                } else {
                    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                }
            }
        });
    }
    try {
        consumer.start();
    } catch (MQClientException e) {
        throw new RuntimeException(e);
    }
}
Also used : ConsumeConcurrentlyContext(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext) MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageListenerConcurrently(org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently) MessageListenerOrderly(org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly) ConsumeOrderlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus) ConsumeConcurrentlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) ConsumeOrderlyContext(org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Aggregations

DefaultMQPushConsumer (org.apache.rocketmq.client.consumer.DefaultMQPushConsumer)6 ConsumeConcurrentlyContext (org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext)6 ConsumeConcurrentlyStatus (org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus)6 MessageListenerConcurrently (org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently)6 MessageExt (org.apache.rocketmq.common.message.MessageExt)6 MQClientException (org.apache.rocketmq.client.exception.MQClientException)3 ConsumeOrderlyContext (org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext)1 ConsumeOrderlyStatus (org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus)1 MessageListenerOrderly (org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly)1 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)1 SendResult (org.apache.rocketmq.client.producer.SendResult)1 Message (org.apache.rocketmq.common.message.Message)1 SubscriptionGroupConfig (org.apache.rocketmq.common.subscription.SubscriptionGroupConfig)1 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1