Search in sources :

Example 11 with ConsumeConcurrentlyStatus

use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus in project rocketmq by apache.

the class StatsBenchmarkConsumer method main.

public static void main(String[] args) throws MQClientException, IOException {
    Options options = ServerUtil.buildCommandlineOptions(new Options());
    CommandLine commandLine = ServerUtil.parseCmdLine("benchmarkConsumer", args, buildCommandlineOptions(options), new PosixParser());
    if (null == commandLine) {
        System.exit(-1);
    }
    final String topic = commandLine.hasOption('t') ? commandLine.getOptionValue('t').trim() : "BenchmarkTest";
    final String groupPrefix = commandLine.hasOption('g') ? commandLine.getOptionValue('g').trim() : "benchmark_consumer";
    final String isPrefixEnable = commandLine.hasOption('p') ? commandLine.getOptionValue('p').trim() : "true";
    final String filterType = commandLine.hasOption('f') ? commandLine.getOptionValue('f').trim() : null;
    final String expression = commandLine.hasOption('e') ? commandLine.getOptionValue('e').trim() : null;
    String group = groupPrefix;
    if (Boolean.parseBoolean(isPrefixEnable)) {
        group = groupPrefix + "_" + Long.toString(System.currentTimeMillis() % 100);
    }
    System.out.printf("topic: %s, group: %s, prefix: %s, filterType: %s, expression: %s%n", topic, group, isPrefixEnable, filterType, expression);
    final StatsBenchmarkConsumer statsBenchmarkConsumer = new StatsBenchmarkConsumer();
    final Timer timer = new Timer("BenchmarkTimerThread", true);
    final LinkedList<Long[]> snapshotList = new LinkedList<Long[]>();
    timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            snapshotList.addLast(statsBenchmarkConsumer.createSnapshot());
            if (snapshotList.size() > 10) {
                snapshotList.removeFirst();
            }
        }
    }, 1000, 1000);
    timer.scheduleAtFixedRate(new TimerTask() {

        private void printStats() {
            if (snapshotList.size() >= 10) {
                Long[] begin = snapshotList.getFirst();
                Long[] end = snapshotList.getLast();
                final long consumeTps = (long) (((end[1] - begin[1]) / (double) (end[0] - begin[0])) * 1000L);
                final double averageB2CRT = (end[2] - begin[2]) / (double) (end[1] - begin[1]);
                final double averageS2CRT = (end[3] - begin[3]) / (double) (end[1] - begin[1]);
                System.out.printf("Consume TPS: %d Average(B2C) RT: %7.3f Average(S2C) RT: %7.3f MAX(B2C) RT: %d MAX(S2C) RT: %d%n", consumeTps, averageB2CRT, averageS2CRT, end[4], end[5]);
            }
        }

        @Override
        public void run() {
            try {
                this.printStats();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }, 10000, 10000);
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(group);
    consumer.setInstanceName(Long.toString(System.currentTimeMillis()));
    if (filterType == null || expression == null) {
        consumer.subscribe(topic, "*");
    } else {
        if (ExpressionType.TAG.equals(filterType)) {
            String expr = MixAll.file2String(expression);
            System.out.printf("Expression: %s%n", expr);
            consumer.subscribe(topic, MessageSelector.byTag(expr));
        } else if (ExpressionType.SQL92.equals(filterType)) {
            String expr = MixAll.file2String(expression);
            System.out.printf("Expression: %s%n", expr);
            consumer.subscribe(topic, MessageSelector.bySql(expr));
        } else {
            throw new IllegalArgumentException("Not support filter type! " + filterType);
        }
    }
    consumer.registerMessageListener(new MessageListenerConcurrently() {

        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
            MessageExt msg = msgs.get(0);
            long now = System.currentTimeMillis();
            statsBenchmarkConsumer.getReceiveMessageTotalCount().incrementAndGet();
            long born2ConsumerRT = now - msg.getBornTimestamp();
            statsBenchmarkConsumer.getBorn2ConsumerTotalRT().addAndGet(born2ConsumerRT);
            long store2ConsumerRT = now - msg.getStoreTimestamp();
            statsBenchmarkConsumer.getStore2ConsumerTotalRT().addAndGet(store2ConsumerRT);
            compareAndSetMax(statsBenchmarkConsumer.getBorn2ConsumerMaxRT(), born2ConsumerRT);
            compareAndSetMax(statsBenchmarkConsumer.getStore2ConsumerMaxRT(), store2ConsumerRT);
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });
    consumer.start();
    System.out.printf("Consumer Started.%n");
}
Also used : Options(org.apache.commons.cli.Options) MessageListenerConcurrently(org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently) PosixParser(org.apache.commons.cli.PosixParser) ConsumeConcurrentlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) LinkedList(java.util.LinkedList) IOException(java.io.IOException) MQClientException(org.apache.rocketmq.client.exception.MQClientException) ConsumeConcurrentlyContext(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext) MessageExt(org.apache.rocketmq.common.message.MessageExt) CommandLine(org.apache.commons.cli.CommandLine) Timer(java.util.Timer) TimerTask(java.util.TimerTask) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 12 with ConsumeConcurrentlyStatus

use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus in project rocketmq by apache.

the class PushConsumer method main.

public static void main(String[] args) throws InterruptedException, MQClientException {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_1");
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    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("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);
            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)

Example 13 with ConsumeConcurrentlyStatus

use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus in project rocketmq by apache.

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("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);
            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 14 with ConsumeConcurrentlyStatus

use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus in project rocketmq by apache.

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 15 with ConsumeConcurrentlyStatus

use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus in project rocketmq by apache.

the class Consumer method main.

public static void main(String[] args) throws InterruptedException, MQClientException {
    /*
         * Instantiate with specified consumer group name.
         */
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_4");
    /*
         * 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.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("%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)

Aggregations

ConsumeConcurrentlyStatus (org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus)40 ConsumeConcurrentlyContext (org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext)38 MessageExt (org.apache.rocketmq.common.message.MessageExt)38 MessageListenerConcurrently (org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently)36 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 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