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

The following examples show how to use org.apache.rocketmq.client.consumer.DefaultMQPushConsumer#start() . 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: PushConsumer.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
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");
}
 
Example 2
Source File: PushConsumer2.java    From blog with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static void main(String[] args) throws MQClientException {

		DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("ConsumerGroupName");
		consumer.setNamesrvAddr("192.168.237.128:9876");
		consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
		consumer.subscribe("TopicTest1234", "*");
		
		consumer.registerMessageListener(new MessageListenerOrderly() {
			
			@Override
			public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
				System.out.printf("Time [" + new Date().toString() + "]," +Thread.currentThread().getName() + "Receive New Messages :" + msgs + "%n");
				return ConsumeOrderlyStatus.SUCCESS;
			}
		});
		consumer.start();
	}
 
Example 3
Source File: PushConsumer.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws InterruptedException, MQClientException {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("CID_JODIE_1");
    consumer.subscribe("Jodie_topic_1023", "*");
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    //wrong time format 2017_0422_221800
    consumer.setConsumeTimestamp("20170422221800");
    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");
}
 
Example 4
Source File: Consumer.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
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");
}
 
Example 5
Source File: Consumer.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
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");
}
 
Example 6
Source File: DelayConsumer.java    From blog with MIT License 6 votes vote down vote up
public static void main(String[] args) throws MQClientException {
  DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("Delay_Consumer");
  consumer.setNamesrvAddr("127.0.0.1:9876");
  consumer.subscribe("DelayMessageTopic", "*");

  consumer.registerMessageListener(
      new MessageListenerConcurrently() {
        @Override
        public ConsumeConcurrentlyStatus consumeMessage(
            List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
          MessageExt msg = msgs.get(0);
          System.out.println(
              new String(msg.getBody())
                  + "   storeTimestamp = "
                  + (System.currentTimeMillis() - msg.getStoreTimestamp()));
          return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
      });
  consumer.start();
  System.out.println("Consumer 已启动");
}
 
Example 7
Source File: PushConsumer.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws InterruptedException, MQClientException {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("CID_JODIE_1");
    consumer.subscribe("Jodie_topic_1023", "*");
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    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");
}
 
Example 8
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 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: Consumer.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws MQClientException {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_3");

    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);

    consumer.subscribe("TopicTest", "TagA || TagC || TagD");

    consumer.setNamesrvAddr("127.0.0.1:9876"); // TODO add by yunai

    consumer.registerMessageListener(new MessageListenerOrderly() {
        AtomicLong consumeTimes = new AtomicLong(0);

        @Override
        public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
            context.setAutoCommit(false);
            System.out.printf(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n");
            this.consumeTimes.incrementAndGet();
            if ((this.consumeTimes.get() % 2) == 0) {
                return ConsumeOrderlyStatus.SUCCESS;
            } else if ((this.consumeTimes.get() % 3) == 0) {
                return ConsumeOrderlyStatus.ROLLBACK;
            } else if ((this.consumeTimes.get() % 4) == 0) {
                return ConsumeOrderlyStatus.COMMIT;
            } else if ((this.consumeTimes.get() % 5) == 0) {
                context.setSuspendCurrentQueueTimeMillis(3000);
                return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT;
            }

            return ConsumeOrderlyStatus.SUCCESS;
        }
    });

    consumer.start();
    System.out.printf("Consumer Started.%n");
}
 
Example 11
Source File: BroadcastConsumer1.java    From blog with MIT License 5 votes vote down vote up
public static void main(String[] args) throws MQClientException {
  /** TODO: 创建消息消费者对象 */
  DefaultMQPushConsumer broadcast_consumer = new DefaultMQPushConsumer("Broadcast_Consumer");
  /** TODO: 连接 namerserver */
  broadcast_consumer.setNamesrvAddr("127.0.0.1:9876");

  /**
   * TODO: 设置 Consumer 类型
   *
   * <p>默认 CLUSTERING(集群模式,一个 Consumer Group 中的 Consumer 实例平均分摊消费消息);
   *
   * <p>BROADCASTING(广播模式,一个消息被多个 Consumer 消费)
   */
  broadcast_consumer.setMessageModel(MessageModel.BROADCASTING);

  /** TODO: 订阅消息主题, 第二个参数表示 指定 订阅的 tag,* 表示全部 */
  broadcast_consumer.subscribe("MyTopic", "*");

  /** TODO: 注册消息监听 */
  broadcast_consumer.registerMessageListener(
      new MessageListenerConcurrently() {
        @Override
        public ConsumeConcurrentlyStatus consumeMessage(
            List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
          // TODO: 输出获取的消息
          msgs.forEach(o -> System.out.println(new String(o.getBody())));
          // TODO: 返回消费结果
          return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
      });

  /** TODO: 启动 namerserver 连接 */
  broadcast_consumer.start();
  System.out.println("BroadcastConsumer1 已启动===========");
}
 
Example 12
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 13
Source File: Consumer.java    From learning-code with Apache License 2.0 5 votes vote down vote up
@Bean
public DefaultMQPushConsumer getRocketMQConsumer() {

    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(groupName);
    consumer.setNamesrvAddr(namesrvAddr);
    consumer.setConsumeThreadMin(consumeThreadMin);
    consumer.setConsumeThreadMax(consumeThreadMax);
    consumer.registerMessageListener(mqMessageListenerProcessor);
    /**
     * 设置Consumer第一次启动是从队列头部开始消费还是队列尾部开始消费
     * 如果非第一次启动,那么按照上次消费的位置继续消费
     */
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);
    /**
     * 设置消费模型,集群还是广播,默认为集群
     */
    //consumer.setMessageModel(MessageModel.CLUSTERING);
    /**
     * 设置一次消费消息的条数,默认为1条
     */
    consumer.setConsumeMessageBatchMaxSize(consumeMessageBatchMaxSize);
    try {
        /**
         * 设置该消费者订阅的主题和tag,如果是订阅该主题下的所有tag,则tag使用*;如果需要指定订阅该主题下的某些tag,则使用||分割,例如tag1||tag2||tag3
         */
        String[] topicTagsArr = topics.split(";");
        for (String topicTags : topicTagsArr) {
            String[] topicTag = topicTags.split("~");
            consumer.subscribe(topicTag[0], topicTag[1]);
        }
        consumer.start();
        log.info("consumer is start !!! groupName:{},topics:{},namesrvAddr:{}", groupName, topics, namesrvAddr);
    } catch (MQClientException e) {
        log.error("consumer is start !!! groupName:{},topics:{},namesrvAddr:{}", groupName, topics, namesrvAddr, e);
    }
    return consumer;
}
 
Example 14
Source File: TransactionConsumer.java    From java-tutorial with MIT License 5 votes vote down vote up
public static void main(String[] args) throws MQClientException {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("transaction_Consumer");
    consumer.setNamesrvAddr(NAMESRVADDR);
    /**
     *  设置Consumer第一次启动是从队列头部开始消费还是队列尾部开始消费
     *  如果非第一次启动,那么按照上次消费的位置继续消费
     */
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    consumer.subscribe("TopicTransactionTest", "*");
    consumer.registerMessageListener(new MessageListenerConcurrently() {
        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list,
                                                        ConsumeConcurrentlyContext concurrentlyContext) {
            for (MessageExt msg : list) {
                System.out.println(msg + ",内容:" + new String(msg.getBody()));
            }
            try {
                TimeUnit.SECONDS.sleep(5L);
            } catch (InterruptedException e) {
                e.printStackTrace();
                //重试
                return ConsumeConcurrentlyStatus.RECONSUME_LATER;
            }
            //成功
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });
    consumer.start();
    System.out.println("consumer Started.");
}
 
Example 15
Source File: BroadcastConsumer2.java    From blog with MIT License 5 votes vote down vote up
public static void main(String[] args) throws MQClientException {
  /** TODO: 创建消息消费者对象 */
  DefaultMQPushConsumer broadcast_consumer = new DefaultMQPushConsumer("Broadcast_Consumer");
  /** TODO: 连接 namerserver */
  broadcast_consumer.setNamesrvAddr("127.0.0.1:9876");

  /**
   * TODO: 设置 Consumer 类型
   *
   * <p>默认 CLUSTERING(集群模式,一个 Consumer Group 中的 Consumer 实例平均分摊消费消息);
   *
   * <p>BROADCASTING(广播模式,一个消息被多个 Consumer 消费)
   */
  broadcast_consumer.setMessageModel(MessageModel.BROADCASTING);

  /** TODO: 订阅消息主题, 第二个参数表示 指定 订阅的 tag,* 表示全部 */
  broadcast_consumer.subscribe("MyTopic", "*");

  /** TODO: 注册消息监听 */
  broadcast_consumer.registerMessageListener(
      new MessageListenerConcurrently() {
        @Override
        public ConsumeConcurrentlyStatus consumeMessage(
            List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
          // TODO: 输出获取的消息
          msgs.forEach(o -> System.out.println(new String(o.getBody())));
          // TODO: 返回消费结果
          return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
      });

  /** TODO: 启动 namerserver 连接 */
  broadcast_consumer.start();
  System.out.println("BroadcastConsumer2 已启动===========");
}
 
Example 16
Source File: Consumer2.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 4 votes vote down vote up
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) {
                try {
                    MessageExt msg = msgs.get(0);
                    //通过把时间打印出来看看重试的时间效果 重试应该时间10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
                    String msgbody = new String(msg.getBody(), "utf-8");
                    System.out.println(simpleDateFormat.format(new Date())+msgbody + " Receive New Messages: " + msgs);
                    Thread.sleep(200000);  //不模拟异常,模拟非常耗时看看效果如何??,观察消息重试情况   观察该字段的值的变化reconsumeTimes
                } catch (Exception e) {
                    e.printStackTrace();
                    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;  
            }
        });

        /*
         *  Launch the consumer instance.
         */
        consumer.start();

        System.out.printf("Consumer Started.%n");
    }
 
Example 17
Source File: Consumer.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
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");
    }
 
Example 18
Source File: Consumer.java    From code with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws MQClientException {

        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("test_quick_ms_consumer_name");

        //consumer.setNamesrvAddr(Const.NAMESRV_ADDR_MASTER_SLAVE);

        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);

        consumer.subscribe("test_quick_topic", "*");

        consumer.registerMessageListener(new MessageListenerConcurrently() {

            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                MessageExt me = msgs.get(0);
                try {
                    String topic = me.getTopic();
                    String tags = me.getTags();
                    String keys = me.getKeys();

                    // 模拟消息消费失败异常
					//if(keys.equals("key1")) {
					//	System.err.println("消息消费失败..");
					//	int a = 1/0;
					//}

                    String msgBody = new String(me.getBody(), RemotingHelper.DEFAULT_CHARSET);
                    System.err.println("topic: " + topic + ",tags: " + tags + ", keys: " + keys + ",body: " + msgBody);
                } catch (Exception e) {
                    e.printStackTrace();
                    // 消息重发次数
					int recousumeTimes = me.getReconsumeTimes();
					System.err.println("recousumeTimes: " + recousumeTimes);
					if(recousumeTimes == 3) {
                        /*
                         * 对于broke消息重发到一定次数的时候
                         * 记录日志....
                         * 做补偿处理
                         */
                        System.out.println("消息无法消费 msid="+me.getMsgId());
						return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
					}

                    // broke消息重发
                    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });

        consumer.start();
        System.err.println("consumer start...");

    }
 
Example 19
Source File: Consumer.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
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");
        consumer.setConsumeThreadMin(1);
        consumer.setConsumeThreadMax(1);

//        consumer.setConsumeMessageBatchMaxSize(2);

        consumer.setNamesrvAddr("127.0.0.1:9876"); // TODO add by yunai

        /*
         * 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("TopicRead3", "*");
//        consumer.subscribe("TopicTest", "*");
        consumer.subscribe("TopicTest_mis", "*");

        /*
         *  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.RECONSUME_LATER;
//                return ConsumeConcurrentlyStatus.RECONSUME_LATER;
            }
        });

        /*
         *  Launch the consumer instance.
         */
        consumer.start();

        System.out.printf("Consumer Started.%n");
    }
 
Example 20
Source File: Consumer.java    From rocketmq 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");
}