Search in sources :

Example 1 with ConsumeConcurrentlyStatus

use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class AbstractTestCase method consumeMessages.

protected int consumeMessages(int count, final String key, int timeout) throws MQClientException, InterruptedException {
    final AtomicInteger cc = new AtomicInteger(0);
    final CountDownLatch countDownLatch = new CountDownLatch(count);
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("hello");
    consumer.setNamesrvAddr(nameServer);
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    consumer.subscribe(topic, "*");
    consumer.registerMessageListener(new MessageListenerConcurrently() {

        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
            for (MessageExt msg : msgs) {
                String body = new String(msg.getBody());
                if (key == null || body.contains(key)) {
                    countDownLatch.countDown();
                    cc.incrementAndGet();
                    continue;
                }
            }
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });
    consumer.start();
    countDownLatch.await(timeout, TimeUnit.SECONDS);
    consumer.shutdown();
    return cc.get();
}
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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConsumeConcurrentlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with ConsumeConcurrentlyStatus

use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class Consumer method main.

public static void main(String[] args) throws InterruptedException, MQClientException, IOException {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("ConsumerGroupNamecc4");
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    File classFile = new File(classLoader.getResource("MessageFilterImpl.java").getFile());
    String filterCode = MixAll.file2String(classFile);
    consumer.subscribe("TopicTest", "org.apache.rocketmq.example.filter.MessageFilterImpl", filterCode);
    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("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) File(java.io.File)

Example 3 with ConsumeConcurrentlyStatus

use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class SqlConsumer method main.

public static void main(String[] args) {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_4");
    try {
        consumer.subscribe("TopicTest", MessageSelector.bySql("(TAGS is not null and TAGS in ('TagA', 'TagB'))" + "and (a is not null and a between 0  3)"));
    } catch (MQClientException e) {
        e.printStackTrace();
        return;
    }
    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;
        }
    });
    try {
        consumer.start();
    } catch (MQClientException e) {
        e.printStackTrace();
        return;
    }
    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) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 4 with ConsumeConcurrentlyStatus

use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class Consumer method main.

public static void main(String[] args) throws InterruptedException, MQClientException {
    CommandLine commandLine = buildCommandline(args);
    if (commandLine != null) {
        String group = commandLine.getOptionValue('g');
        String topic = commandLine.getOptionValue('t');
        String subscription = commandLine.getOptionValue('s');
        final String returnFailedHalf = commandLine.getOptionValue('f');
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(group);
        consumer.setInstanceName(Long.toString(System.currentTimeMillis()));
        consumer.subscribe(topic, subscription);
        consumer.registerMessageListener(new MessageListenerConcurrently() {

            AtomicLong consumeTimes = new AtomicLong(0);

            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                long currentTimes = this.consumeTimes.incrementAndGet();
                System.out.printf("%-8d %s%n", currentTimes, msgs);
                if (Boolean.parseBoolean(returnFailedHalf)) {
                    if ((currentTimes % 2) == 0) {
                        return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                    }
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        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) CommandLine(org.apache.commons.cli.CommandLine) AtomicLong(java.util.concurrent.atomic.AtomicLong) MessageListenerConcurrently(org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently) ConsumeConcurrentlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer)

Example 5 with ConsumeConcurrentlyStatus

use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class Consumer method main.

public static void main(String[] args) throws InterruptedException, MQClientException {
    /*
         * Instantiate with specified consumer group name.
         */
    /**
     * Consumer Group,非常重要的概念,后续会慢慢补充
     */
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_4");
    // 指定NameServer地址,多个地址以 ; 隔开
    consumer.setNamesrvAddr("127.0.0.1:9876");
    /*
         * 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第一次启动是从队列头部开始消费还是队列尾部开始消费
     * 如果非第一次启动,那么按照上次消费的位置继续消费
     */
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    /*
         * 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(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n");
            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)

Aggregations

ConsumeConcurrentlyStatus (org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus)38 ConsumeConcurrentlyContext (org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext)36 MessageExt (org.apache.rocketmq.common.message.MessageExt)36 MessageListenerConcurrently (org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently)34 DefaultMQPushConsumer (org.apache.rocketmq.client.consumer.DefaultMQPushConsumer)31 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 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 Timer (java.util.Timer)2 TimerTask (java.util.TimerTask)2