Search in sources :

Example 6 with DefaultMQPushConsumer

use of org.apache.rocketmq.client.consumer.DefaultMQPushConsumer in project paascloud-master by paascloud.

the class AliyunMqConfiguration method defaultMQPushConsumer.

/**
 * 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;
}
Also used : DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) MQClientException(org.apache.rocketmq.client.exception.MQClientException) Bean(org.springframework.context.annotation.Bean)

Example 7 with DefaultMQPushConsumer

use of org.apache.rocketmq.client.consumer.DefaultMQPushConsumer 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 8 with DefaultMQPushConsumer

use of org.apache.rocketmq.client.consumer.DefaultMQPushConsumer 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 9 with DefaultMQPushConsumer

use of org.apache.rocketmq.client.consumer.DefaultMQPushConsumer 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)

Example 10 with DefaultMQPushConsumer

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

the class BroadcastConsumer method main.

public static void main(String[] args) throws Exception {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("example_group_name");
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    // set to broadcast mode
    consumer.setMessageModel(MessageModel.BROADCASTING);
    consumer.subscribe("TopicTest", "TagA || TagC || TagD");
    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.CONSUME_SUCCESS;
        }
    });
    consumer.start();
    System.out.printf("Broadcast Consumer Started.%n");
}
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)

Aggregations

DefaultMQPushConsumer (org.apache.rocketmq.client.consumer.DefaultMQPushConsumer)11 MessageExt (org.apache.rocketmq.common.message.MessageExt)7 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 MQClientException (org.apache.rocketmq.client.exception.MQClientException)5 ConsumeOrderlyContext (org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext)2 ConsumeOrderlyStatus (org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus)2 MessageListenerOrderly (org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly)2 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)2 URI (java.net.URI)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Connection (javax.jms.Connection)1 Destination (javax.jms.Destination)1 Session (javax.jms.Session)1 IgniteException (org.apache.ignite.IgniteException)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 JmsBaseConnectionFactory (org.apache.rocketmq.jms.domain.JmsBaseConnectionFactory)1