Java Code Examples for org.apache.rocketmq.client.consumer.DefaultMQPushConsumer#setInstanceName()

The following examples show how to use org.apache.rocketmq.client.consumer.DefaultMQPushConsumer#setInstanceName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RocketMQBenchmarkDriver.java    From openmessaging-benchmark with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<BenchmarkConsumer> createConsumer(final String topic, final String subscriptionName,
    final ConsumerCallback consumerCallback) {
    DefaultMQPushConsumer rmqConsumer = new DefaultMQPushConsumer(subscriptionName);
    rmqConsumer.setNamesrvAddr(this.rmqClientConfig.namesrvAddr);
    rmqConsumer.setInstanceName("ConsumerInstance" + getRandomString());
    if(null != this.rmqClientConfig.vipChannelEnabled){
        rmqConsumer.setVipChannelEnabled(this.rmqClientConfig.vipChannelEnabled);
    }
    try {
        rmqConsumer.subscribe(topic, "*");
        rmqConsumer.registerMessageListener((MessageListenerConcurrently) (msgs, context) -> {
            for (MessageExt message : msgs) {
                consumerCallback.messageReceived(message.getBody(), message.getBornTimestamp());
            }
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        });
        rmqConsumer.start();
    } catch (MQClientException e) {
        log.error("Failed to create consumer instance.", e);
    }

    return CompletableFuture.completedFuture(new RocketMQBenchmarkConsumer(rmqConsumer));
}
 
Example 2
Source File: RMQNormalConsumer.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public void create(boolean useTLS) {
    consumer = new DefaultMQPushConsumer(consumerGroup);
    consumer.setInstanceName(RandomUtil.getStringByUUID());
    consumer.setNamesrvAddr(nsAddr);
    try {
        consumer.subscribe(topic, subExpression);
    } catch (MQClientException e) {
        logger.error("consumer subscribe failed!");
        e.printStackTrace();
    }
    consumer.setMessageListener(listener);
    consumer.setUseTLS(useTLS);
}
 
Example 3
Source File: RMQNormalConsumer.java    From rocketmq_trans_message with Apache License 2.0 5 votes vote down vote up
public void create() {
    consumer = new DefaultMQPushConsumer(consumerGroup);
    consumer.setInstanceName(RandomUtil.getStringByUUID());
    consumer.setNamesrvAddr(nsAddr);
    try {
        consumer.subscribe(topic, subExpression);
    } catch (MQClientException e) {
        logger.error("consumer subscribe failed!");
        e.printStackTrace();
    }
    consumer.setMessageListener(listner);
}
 
Example 4
Source File: Consumer.java    From rocketmq_trans_message with Apache License 2.0 5 votes vote down vote up
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");
    }
}
 
Example 5
Source File: RMQNormalConsumer.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
public void create() {
    consumer = new DefaultMQPushConsumer(consumerGroup);
    consumer.setInstanceName(RandomUtil.getStringByUUID());
    consumer.setNamesrvAddr(nsAddr);
    try {
        consumer.subscribe(topic, subExpression);
    } catch (MQClientException e) {
        logger.error("consumer subscribe failed!");
        e.printStackTrace();
    }
    consumer.setMessageListener(listner);
}
 
Example 6
Source File: Consumer.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
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");
    }
}
 
Example 7
Source File: Consumer.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
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");
    }
}
 
Example 8
Source File: RockMqTestCase.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Test
public void testQueue() throws Exception {
    int size = 5;
    DefaultMQPushConsumer[] consumers = new DefaultMQPushConsumer[size];
    for (int index = 0; index < size; index++) {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("queue");
        consumer.setInstanceName("queueConsumer" + index);
        consumer.setNamesrvAddr("localhost:9876");
        consumer.setMessageModel(MessageModel.CLUSTERING);
        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
        consumer.setConsumeMessageBatchMaxSize(1000);
        consumer.subscribe("queue-message", "*");
        consumer.registerMessageListener(listener);
        consumer.start();
        consumers[index] = consumer;
    }

    DefaultMQProducer producer = new DefaultMQProducer("queue");
    producer.setNamesrvAddr("localhost:9876");
    producer.start();

    for (int index = 0; index < 10; index++) {
        Message message = new Message("queue-message", "tag", ("RocketMQ 集群模式 " + index).getBytes(StringUtility.CHARSET));
        producer.send(message);
    }

    semaphore.acquire(10);

    producer.shutdown();
    for (int index = 0; index < size; index++) {
        consumers[index].shutdown();
    }
}
 
Example 9
Source File: RocketEventChannel.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public void registerMonitor(Set<Class> types, EventMonitor monitor) {
    try {
        for (Class type : types) {
            EventManager manager = managers.get(type);
            if (manager == null) {
                manager = new EventManager();
                managers.put(type, manager);
                // TODO 需要防止路径冲突
                String address = name + StringUtility.DOT + type.getName();
                address = address.replace(StringUtility.DOT, StringUtility.DASH);
                DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(address);
                consumer.setInstanceName(name);
                consumer.setNamesrvAddr(connections);
                consumer.setConsumeMessageBatchMaxSize(1000);
                consumer.subscribe(address, "*");
                switch (mode) {
                case QUEUE: {
                    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
                    consumer.setMessageModel(MessageModel.CLUSTERING);
                    break;
                }
                case TOPIC: {
                    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);
                    consumer.setMessageModel(MessageModel.BROADCASTING);
                    break;
                }
                }
                EventHandler handler = new EventHandler(type, manager);
                consumer.registerMessageListener(handler);
                consumer.start();
                consumers.put(type, consumer);
            }
            manager.attachMonitor(monitor);
        }
    } catch (Exception exception) {
        throw new RuntimeException(exception);
    }
}
 
Example 10
Source File: RMQNormalConsumer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public void create(boolean useTLS) {
    consumer = new DefaultMQPushConsumer(consumerGroup);
    consumer.setInstanceName(RandomUtil.getStringByUUID());
    consumer.setNamesrvAddr(nsAddr);
    try {
        consumer.subscribe(topic, subExpression);
    } catch (MQClientException e) {
        logger.error("consumer subscribe failed!");
        e.printStackTrace();
    }
    consumer.setMessageListener(listener);
    consumer.setUseTLS(useTLS);
}
 
Example 11
Source File: Consumer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
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");
    }
}
 
Example 12
Source File: Consumer.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
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");
    }
}
 
Example 13
Source File: Consumer.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
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");
    }
}
 
Example 14
Source File: RMQNormalConsumer.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
public void create(boolean useTLS) {
    consumer = new DefaultMQPushConsumer(consumerGroup);
    consumer.setInstanceName(RandomUtil.getStringByUUID());
    consumer.setNamesrvAddr(nsAddr);
    try {
        consumer.subscribe(topic, subExpression);
    } catch (MQClientException e) {
        logger.error("consumer subscribe failed!");
        e.printStackTrace();
    }
    consumer.setMessageListener(listener);
    consumer.setUseTLS(useTLS);
}
 
Example 15
Source File: Consumer.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
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");
    }
}
 
Example 16
Source File: Consumer.java    From java-tutorial with MIT License 4 votes vote down vote up
public static void main(String[] args) throws MQClientException {

        //声明并初始化一个consumer
        //需要一个consumer group名字作为构造方法的参数,这里为consumer1
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("PUSH_CONSUMER_GROUP");

        //同样也要设置NameServer地址
        consumer.setNamesrvAddr(SyncProducer.NAMESRVADDR);
        consumer.setInstanceName("PUSH_CONSUMER");
        //大于3.5.3版本要设置为false 否则取不到topic
        consumer.setVipChannelEnabled(false);
        // 批量消费,每次拉取10条
        consumer.setConsumeMessageBatchMaxSize(10);

        //这里设置的是一个consumer的消费策略
        // CONSUME_FROM_LAST_OFFSET 默认策略,从该队列最尾开始消费,即跳过历史消息
        // CONSUME_FROM_FIRST_OFFSET 从队列最开始开始消费,即历史消息(还储存在broker的)全部消费一遍
        // CONSUME_FROM_TIMESTAMP 从某个时间点开始消费,和setConsumeTimestamp()配合使用,默认是半个小时以前

        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
        //设置consumer所订阅的Topic和Tag,*代表全部的Tag
        consumer.subscribe("Sync_TopicA_Test", "*");

        //设置一个Listener,主要进行消息的逻辑处理
        consumer.registerMessageListener((MessageListenerConcurrently) (msgs, context) -> {
            MessageExt msg = msgs.get(0);

            try {
                String topic = msg.getTopic();
                String msgBody = new String(msg.getBody(), "utf-8");
                String tags = msg.getTags();
                System.out.println("收到消息:topic:" + topic + ",tags:" + tags + ",msg:" + msg + "msgBody:" + msgBody);
                System.out.println("MsgBody:" + msgBody);
                System.out.println("------------------------------------------------------------------------------");
            } catch (Exception e) {
                e.printStackTrace();
                // ②如果重试了三次就返回成功
                if (msg.getReconsumeTimes() == 3) {
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                }
                return ConsumeConcurrentlyStatus.RECONSUME_LATER;
            }
            //返回消费状态
            //CONSUME_SUCCESS 消费成功
            //RECONSUME_LATER 消费失败,需要稍后重新消费
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        });


        //调用start()方法启动consumer
        consumer.start();

        System.out.println("Consumer Started.");
    }
 
Example 17
Source File: RocketmqConsumer.java    From myth with Apache License 2.0 4 votes vote down vote up
/**
 * Push consumer default mq push consumer.
 *
 * @return the default mq push consumer
 * @throws MQClientException the mq client exception
 */
@Bean
public DefaultMQPushConsumer pushConsumer() throws MQClientException {
    /**
     * 一个应用创建一个Consumer,由应用来维护此对象,可以设置为全局对象或者单例<br>
     * 注意:ConsumerGroupName需要由应用来保证唯一
     */
    DefaultMQPushConsumer consumer =
            new DefaultMQPushConsumer(env.getProperty("spring.rocketmq.consumerGroupName"));
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    consumer.setNamesrvAddr(env.getProperty("spring.rocketmq.namesrvAddr"));
    consumer.setInstanceName(env.getProperty("spring.rocketmq.instanceName"));
    //设置批量消费,以提升消费吞吐量,默认是1
    consumer.setConsumeMessageBatchMaxSize(3);


    consumer.subscribe(QUEUE, QUEUE);

    consumer.registerMessageListener((List<MessageExt> msgList,
                                      ConsumeConcurrentlyContext context) -> {

        MessageExt msg = msgList.get(0);
        try {
            // 默认msgList里只有一条消息,可以通过设置consumeMessageBatchMaxSize参数来批量接收消息
            final byte[] message = msg.getBody();
            LogUtil.debug(LOGGER,()->"springcloud inventory-serivce rocketmq 接收到myth框架发出的信息====");

            final Boolean success = mythMqReceiveService.processMessage(message);


        } catch (Exception e) {
            e.printStackTrace();
            return ConsumeConcurrentlyStatus.RECONSUME_LATER;
        }

        //如果没有return success,consumer会重复消费此信息,直到success。
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    });

    consumer.start();

    return consumer;
}
 
Example 18
Source File: RocketmqConsumer.java    From myth with Apache License 2.0 4 votes vote down vote up
/**
 * Push consumer default mq push consumer.
 *
 * @return the default mq push consumer
 * @throws MQClientException the mq client exception
 */
@Bean
public DefaultMQPushConsumer pushConsumer() throws MQClientException {
    /**
     * 一个应用创建一个Consumer,由应用来维护此对象,可以设置为全局对象或者单例<br>
     * 注意:ConsumerGroupName需要由应用来保证唯一
     */
    DefaultMQPushConsumer consumer =
            new DefaultMQPushConsumer(env.getProperty("spring.rocketmq.consumerGroupName"));
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    consumer.setNamesrvAddr(env.getProperty("spring.rocketmq.namesrvAddr"));
    consumer.setInstanceName(env.getProperty("spring.rocketmq.instanceName"));
    //设置批量消费,以提升消费吞吐量,默认是1
    consumer.setConsumeMessageBatchMaxSize(1);


    /**
     * 订阅指定topic下tags
     */
    consumer.subscribe(QUEUE, QUEUE);

    consumer.registerMessageListener((List<MessageExt> msgList,
                                      ConsumeConcurrentlyContext context) -> {

        MessageExt msg = msgList.get(0);
        try {
            // 默认msgList里只有一条消息,可以通过设置consumeMessageBatchMaxSize参数来批量接收消息
            final byte[] message = msg.getBody();
            final Boolean success = mythMqReceiveService.processMessage(message);
            if (success) {
                LogUtil.debug(LOGGER, () -> "motan 框架消费rocketmq消息成功");
            }

        } catch (Exception e) {
            e.printStackTrace();
            //重复消费3次
            return ConsumeConcurrentlyStatus.RECONSUME_LATER;
        }

        //如果没有return success,consumer会重复消费此信息,直到success。
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    });

    /**
     * Consumer对象在使用之前必须要调用start初始化,初始化一次即可<br>
     */
    consumer.start();

    return consumer;
}
 
Example 19
Source File: Consumer.java    From rocketmq_trans_message with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws MQClientException {
    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";
    String group = groupPrefix;
    if (Boolean.parseBoolean(isPrefixEnable)) {
        group = groupPrefix + "_" + Long.toString(System.currentTimeMillis() % 100);
    }

    System.out.printf("topic %s group %s prefix %s%n", topic, group, isPrefixEnable);

    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()));

    consumer.subscribe(topic, "*");

    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");
}
 
Example 20
Source File: RocketmqConsumer.java    From myth with Apache License 2.0 4 votes vote down vote up
/**
 * Push consumer default mq push consumer.
 *
 * @return the default mq push consumer
 * @throws MQClientException the mq client exception
 */
@Bean
public DefaultMQPushConsumer pushConsumer() throws MQClientException {
    /**
     * 一个应用创建一个Consumer,由应用来维护此对象,可以设置为全局对象或者单例<br>
     * 注意:ConsumerGroupName需要由应用来保证唯一
     */
    DefaultMQPushConsumer consumer =
            new DefaultMQPushConsumer(env.getProperty("spring.rocketmq.consumerGroupName"));
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    consumer.setNamesrvAddr(env.getProperty("spring.rocketmq.namesrvAddr"));
    consumer.setInstanceName(env.getProperty("spring.rocketmq.instanceName"));
    //设置批量消费,以提升消费吞吐量,默认是1
    consumer.setConsumeMessageBatchMaxSize(1);

    /**
     * 订阅指定topic下tags
     */
    consumer.subscribe(TOPIC, TOPIC);

    consumer.registerMessageListener((List<MessageExt> msgList, ConsumeConcurrentlyContext context) -> {

        MessageExt msg = msgList.get(0);
        try {
            // 默认msgList里只有一条消息,可以通过设置consumeMessageBatchMaxSize参数来批量接收消息
            final byte[] message = msg.getBody();
            final Boolean success = mythMqReceiveService.processMessage(message);
            if (success) {
                //如果没有return success,consumer会重复消费此信息,直到success。
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }

        } catch (Exception e) {
            e.printStackTrace();
            //重复消费3次
            return ConsumeConcurrentlyStatus.RECONSUME_LATER;
        }
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;

    });

    consumer.start();

    return consumer;
}