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

The following examples show how to use org.apache.rocketmq.client.consumer.DefaultMQPushConsumer#setConsumeFromWhere() . 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: BroadcastConsumer.java    From netty-chat with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("example_group_name");

    consumer.setNamesrvAddr("127.0.0.1:9876");
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);

    //set to broadcast mode
    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(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n");
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });

    consumer.start();
    System.out.printf("Broadcast Consumer Started.%n");
}
 
Example 2
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("please_rename_unique_group_name_1");

        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);

        consumer.setMessageModel(MessageModel.BROADCASTING);

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

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

        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("Broadcast Consumer Started.%n");
    }
 
Example 3
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("TopicTest", "*");
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    //wrong time format 2017_0422_221800
    consumer.setConsumeTimestamp("20181109221800");
    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: 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 5
Source File: BroadcastConsumer.java    From spring-boot-starter-samples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
	
	DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("example_group_name");

	consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);

	// set to broadcast mode
	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(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n");
			return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
		}
	});

	consumer.start();
	System.out.printf("Broadcast Consumer Started.%n");
}
 
Example 6
Source File: PushConsumer.java    From rocketmq-4.3.0 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");
    consumer.shutdown();
}
 
Example 7
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 8
Source File: OrderedConsumer.java    From javatech with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
public static void main(String[] args) throws Exception {
	DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("example_group_name");
	consumer.setNamesrvAddr(RocketConfig.HOST);

	consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);

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

	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 9
Source File: AliyunMqConfiguration.java    From paascloud-master with Apache License 2.0 5 votes vote down vote up
/**
 * Default mq push consumer default mq push consumer.
 *
 * @return the default mq push consumer
 *
 * @throws MQClientException the mq client exception
 */
@Bean
public DefaultMQPushConsumer defaultMQPushConsumer() throws MQClientException {
	DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(paascloudProperties.getAliyun().getRocketMq().getConsumerGroup());
	consumer.setNamesrvAddr(paascloudProperties.getAliyun().getRocketMq().getNamesrvAddr());
	consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);

	String[] strArray = AliyunMqTopicConstants.ConsumerTopics.OPT.split(GlobalConstant.Symbol.COMMA);
	for (String aStrArray : strArray) {
		String[] topicArray = aStrArray.split(GlobalConstant.Symbol.AT);
		String topic = topicArray[0];
		String tags = topicArray[1];
		if (PublicUtil.isEmpty(tags)) {
			tags = "*";
		}
		consumer.subscribe(topic, tags);
		log.info("RocketMq OpcPushConsumer topic = {}, tags={}", topic, tags);
	}

	consumer.registerMessageListener(optPushConsumer);
	consumer.setConsumeThreadMax(2);
	consumer.setConsumeThreadMin(2);

	taskExecutor.execute(() -> {
		try {
			Thread.sleep(5000);
			consumer.start();
			log.info("RocketMq OpcPushConsumer OK.");
		} catch (InterruptedException | MQClientException e) {
			log.error("RocketMq OpcPushConsumer, 出现异常={}", e.getMessage(), e);
		}
	});
	return consumer;
}
 
Example 10
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 11
Source File: AbstractRocketMqConsumer.java    From spring-boot-rocketmq-starter with Apache License 2.0 5 votes vote down vote up
private void initRocketMQPushConsumer() throws MQClientException {

        Assert.notNull(getConsumerGroup(), "Property 'consumerGroup' is required");
        Assert.notEmpty(subscribeTopicTags(), "subscribeTopicTags method can't be empty");

        consumer = new DefaultMQPushConsumer(getConsumerGroup());
        if (consumeThreadMax != null) {
            consumer.setConsumeThreadMax(consumeThreadMax);
        }
        if (consumeThreadMax != null && consumeThreadMax < consumer.getConsumeThreadMin()) {
            consumer.setConsumeThreadMin(consumeThreadMax);
        }

        consumer.setConsumeFromWhere(consumeFromWhere);
        consumer.setMessageModel(messageModel);

        switch (consumeMode) {
            case Orderly:
                consumer.setMessageListener(new DefaultMessageListenerOrderly());
                break;
            case CONCURRENTLY:
                consumer.setMessageListener(new DefaultMessageListenerConcurrently());
                break;
            default:
                throw new IllegalArgumentException("Property 'consumeMode' was wrong.");
        }

    }
 
Example 12
Source File: Consumer.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws InterruptedException, MQClientException {
    String group = GROUP_ENV == null ? "please_rename_unique_group_name_4" : GROUP_ENV;
    String topic = TOPIC_ENV == null ? "TopicTest" : TOPIC_ENV;
    /*
     * Instantiate with specified consumer group name.
     */
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(group);

    /*
     * 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(topic, "*");

    /*
     *  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);
            for (MessageExt msg : msgs) {
                System.out.printf("get msg:%s%n", new String(msg.getBody()));
            }
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });

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

    System.out.printf("Consumer Started.%n");
}
 
Example 13
Source File: ResponseConsumer.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws InterruptedException, MQClientException {
    String producerGroup = "please_rename_unique_group_name";
    String consumerGroup = "please_rename_unique_group_name";
    String topic = "RequestTopic";

    // create a producer to send reply message
    DefaultMQProducer replyProducer = new DefaultMQProducer(producerGroup);
    replyProducer.start();

    // create consumer
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(consumerGroup);
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);

    // recommend client configs
    consumer.setPullTimeDelayMillsWhenException(0L);

    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);
            for (MessageExt msg : msgs) {
                try {
                    System.out.printf("handle message: %s", msg.toString());
                    String replyTo = MessageUtil.getReplyToClient(msg);
                    byte[] replyContent = "reply message contents.".getBytes();
                    // create reply message with given util, do not create reply message by yourself
                    Message replyMessage = MessageUtil.createReplyMessage(msg, replyContent);

                    // send reply message with producer
                    SendResult replyResult = replyProducer.send(replyMessage, 3000);
                    System.out.printf("reply to %s , %s %n", replyTo, replyResult.toString());
                } catch (MQClientException | RemotingException | MQBrokerException | InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });

    consumer.subscribe(topic, "*");
    consumer.start();
    System.out.printf("Consumer Started.%n");
}
 
Example 14
Source File: MessageExtConsumer.java    From rocketmq-demo with Apache License 2.0 4 votes vote down vote up
@Override
public void prepareStart(DefaultMQPushConsumer consumer) {
    // set consumer consume message from now
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_TIMESTAMP);
    consumer.setConsumeTimestamp(UtilAll.timeMillisToHumanString3(System.currentTimeMillis()));
}
 
Example 15
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;
}
 
Example 16
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 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(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n");
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });

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

        System.out.printf("Consumer Started.%n");
    }
 
Example 17
Source File: Consumer.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws InterruptedException, MQClientException {
    String group = GROUP_ENV == null ? "please_rename_unique_group_name_4" : GROUP_ENV;
    String topic = TOPIC_ENV == null ? "TopicTest" : TOPIC_ENV;
    /*
     * Instantiate with specified consumer group name.
     */
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(group);

    /*
     * 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(topic, "*");

    /*
     *  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);
            for (MessageExt msg : msgs) {
                System.out.printf("get msg:%s%n", new String(msg.getBody()));
            }
            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_async_producer_name");

        consumer.setNamesrvAddr(Const.NAMESRV_ADDR_SINGLE);

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

                    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: 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 20
Source File: Consumer1.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);
                   // int i=1/0;  //模拟异常情况,观察消息重试情况   观察该字段的值的变化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");
    }