Search in sources :

Example 21 with ConsumeConcurrentlyContext

use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext in project pinpoint by naver.

the class RocketMQOriginalConsumer method init.

@PostConstruct
public void init() throws MQClientException {
    // Instantiate with specified consumer group name.
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("your_consumer_group_name");
    // Specify name server addresses.
    consumer.setNamesrvAddr(namesrvAddr);
    // Subscribe one more more topics to consume.
    consumer.subscribe("TopicTest", "*");
    // 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("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });
    // Launch the consumer instance.
    consumer.start();
    System.out.printf("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) PostConstruct(javax.annotation.PostConstruct)

Example 22 with ConsumeConcurrentlyContext

use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext 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 23 with ConsumeConcurrentlyContext

use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext 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 24 with ConsumeConcurrentlyContext

use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext in project java-example by 1479005017.

the class AliyunReceiver method main.

public static void main(String[] argv) throws Exception {
    /**
     * Alions
     */
    AlionsRPCHook rpcHook = new AlionsRPCHook();
    rpcHook.setAccessKeyId(accessKeyId);
    rpcHook.setAccessKeySecret(accessKeySecret);
    rpcHook.setOnsChannel(onsChannel);
    /**
     * 初始化
     */
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(rpcHook);
    consumer.setNamesrvAddr(HttpTinyClient.fetchNamesrvAddress(nameServer));
    consumer.setConsumerGroup(consumerGroup);
    consumer.setVipChannelEnabled(vipChannelEnabled);
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    consumer.subscribe("conanli-test", "*");
    /**
     * 监听消息
     */
    consumer.registerMessageListener(new MessageListenerConcurrently() {

        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
            System.out.printf("%s Receive New Message: %s %n", Thread.currentThread().getName(), msgs);
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });
    /**
     * 启动
     */
    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 25 with ConsumeConcurrentlyContext

use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext in project java-example by 1479005017.

the class HelloReceiver method main.

public static void main(String[] argv) throws Exception {
    /**
     * 初始化
     */
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer();
    consumer.setNamesrvAddr(namesrvAddr);
    consumer.setConsumerGroup(consumerGroup);
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    consumer.subscribe("hello", "*");
    /**
     * 监听消息
     */
    consumer.registerMessageListener(new MessageListenerConcurrently() {

        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
            System.out.printf("%s Receive New Message: %s %n", Thread.currentThread().getName(), msgs);
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });
    /**
     * 启动
     */
    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)

Aggregations

ConsumeConcurrentlyContext (org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext)40 MessageExt (org.apache.rocketmq.common.message.MessageExt)40 ConsumeConcurrentlyStatus (org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus)38 MessageListenerConcurrently (org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently)38 DefaultMQPushConsumer (org.apache.rocketmq.client.consumer.DefaultMQPushConsumer)33 MQClientException (org.apache.rocketmq.client.exception.MQClientException)11 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 CommandLine (org.apache.commons.cli.CommandLine)4 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 File (java.io.File)2 IOException (java.io.IOException)2 Field (java.lang.reflect.Field)2 InetSocketAddress (java.net.InetSocketAddress)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2