org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext Java Examples

The following examples show how to use org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext. 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: SqlFilterConsumer.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name");

        // Don't forget to set enablePropertyFilter=true in broker
        consumer.subscribe("SqlFilterTest",
            MessageSelector.bySql("(TAGS is not null and TAGS in ('TagA', 'TagB'))" +
                "and (a is not null and a between 0 and 3)"));

        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 #2
Source File: DefaultMQPushConsumerImplTest.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
@Test
public void checkConfigTest() throws MQClientException {

    //test type
    thrown.expect(MQClientException.class);

    //test message
    thrown.expectMessage("consumeThreadMin (10) is larger than consumeThreadMax (9)");

    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("test_consumer_group");

    consumer.setConsumeThreadMin(10);
    consumer.setConsumeThreadMax(9);

    consumer.registerMessageListener(new MessageListenerConcurrently() {
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
            ConsumeConcurrentlyContext context) {
            System.out.println(" Receive New Messages: " + msgs);
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });

    DefaultMQPushConsumerImpl defaultMQPushConsumerImpl = new DefaultMQPushConsumerImpl(consumer, null);
    defaultMQPushConsumerImpl.start();
}
 
Example #3
Source File: RMQNormalListner.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
    ConsumeConcurrentlyContext consumeConcurrentlyContext) {
    for (MessageExt msg : msgs) {
        msgIndex.getAndIncrement();
        if (isDebug) {
            if (listnerName != null && listnerName != "") {
                logger.info(listnerName + ":" + msgIndex.get() + ":"
                    + String.format("msgid:%s broker:%s queueId:%s offset:%s",
                    msg.getMsgId(), msg.getStoreHost(), msg.getQueueId(),
                    msg.getQueueOffset()));
            } else {
                logger.info(msg);
            }
        }

        msgBodys.addData(new String(msg.getBody()));
        originMsgs.addData(msg);
        originMsgIndex.put(new String(msg.getBody()), msg);
    }
    return consumeStatus;
}
 
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: RMQNormalListner.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
    ConsumeConcurrentlyContext consumeConcurrentlyContext) {
    for (MessageExt msg : msgs) {
        msgIndex.getAndIncrement();
        if (isDebug) {
            if (listnerName != null && listnerName != "") {
                logger.info(listnerName + ":" + msgIndex.get() + ":"
                    + String.format("msgid:%s broker:%s queueId:%s offset:%s",
                    msg.getMsgId(), msg.getStoreHost(), msg.getQueueId(),
                    msg.getQueueOffset()));
            } else {
                logger.info(msg);
            }
        }

        msgBodys.addData(new String(msg.getBody()));
        originMsgs.addData(msg);
        if (originMsgIndex != null) {
            originMsgIndex.put(new String(msg.getBody()), msg);
        }
    }
    return consumeStatus;
}
 
Example #6
Source File: Consumer.java    From rocketmq_trans_message with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws InterruptedException, MQClientException {
    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 #7
Source File: WalletPaymentMsgListenerImpl.java    From order-charge-notify with Apache License 2.0 6 votes vote down vote up
/**
 * 钱包扣款关键逻辑
 * @param msgs
 * @param context
 * @return
 */
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
    try {
        // 默认msgs只有一条消息
        for (MessageExt msg : msgs) {
            // 消费次数
            int reconsumeTimes = msg.getReconsumeTimes();
            String msgId = msg.getMsgId();
            LOGGER.info("===============msgId={},消费次数={}===============", msgId, reconsumeTimes);
            return walletCharge(msg, msgId);
        }
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    } catch (Exception e) {
        LOGGER.error("钱包扣款消费异常,e={}", e);
        return ConsumeConcurrentlyStatus.RECONSUME_LATER;
    }
}
 
Example #8
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 #9
Source File: RMQNormalListener.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
    ConsumeConcurrentlyContext consumeConcurrentlyContext) {
    for (MessageExt msg : msgs) {
        msgIndex.getAndIncrement();
        if (isDebug) {
            if (listenerName != null && !listenerName.isEmpty()) {
                logger.info(listenerName + ":" + msgIndex.get() + ":"
                    + String.format("msgid:%s broker:%s queueId:%s offset:%s",
                    msg.getMsgId(), msg.getStoreHost(), msg.getQueueId(),
                    msg.getQueueOffset()));
            } else {
                logger.info(msg);
            }
        }

        msgBodys.addData(new String(msg.getBody()));
        originMsgs.addData(msg);
        if (originMsgIndex != null) {
            originMsgIndex.put(new String(msg.getBody()), msg);
        }
    }
    return consumeStatus;
}
 
Example #10
Source File: Consumer.java    From rocketmq-read 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 #11
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 #12
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 #13
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.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 #14
Source File: PushConsumer.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 {
    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(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n");
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });

    consumer.start();
    System.out.printf("Broadcast Consumer Started.%n");
}
 
Example #15
Source File: DefaultMQPushConsumerTest.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
@Test
public void testPullMessage_Success() throws InterruptedException, RemotingException, MQBrokerException {
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    final MessageExt[] messageExts = new MessageExt[1];
    pushConsumer.getDefaultMQPushConsumerImpl().setConsumeMessageService(new ConsumeMessageConcurrentlyService(pushConsumer.getDefaultMQPushConsumerImpl(), new MessageListenerConcurrently() {
        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
            ConsumeConcurrentlyContext context) {
            messageExts[0] = msgs.get(0);
            countDownLatch.countDown();
            return null;
        }
    }));

    PullMessageService pullMessageService = mQClientFactory.getPullMessageService();
    pullMessageService.executePullRequestImmediately(createPullRequest());
    countDownLatch.await();
    assertThat(messageExts[0].getTopic()).isEqualTo(topic);
    assertThat(messageExts[0].getBody()).isEqualTo(new byte[] {'a'});
}
 
Example #16
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("CID_JODIE_1");
    consumer.setNamesrvAddr("127.0.0.1:9876");
    consumer.subscribe("face-manager-1", "*");
    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 #17
Source File: RMQNormalListener.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
    ConsumeConcurrentlyContext consumeConcurrentlyContext) {
    for (MessageExt msg : msgs) {
        msgIndex.getAndIncrement();
        if (isDebug) {
            if (listenerName != null && !listenerName.isEmpty()) {
                logger.info(listenerName + ":" + msgIndex.get() + ":"
                    + String.format("msgid:%s broker:%s queueId:%s offset:%s",
                    msg.getMsgId(), msg.getStoreHost(), msg.getQueueId(),
                    msg.getQueueOffset()));
            } else {
                logger.info(msg);
            }
        }

        msgBodys.addData(new String(msg.getBody()));
        originMsgs.addData(msg);
        if (originMsgIndex != null) {
            originMsgIndex.put(new String(msg.getBody()), msg);
        }
    }
    return consumeStatus;
}
 
Example #18
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 #19
Source File: MessageListenerConcurrentlyProxy.java    From pepper-metrics with Apache License 2.0 6 votes vote down vote up
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
    long begin = System.currentTimeMillis();
    ConsumeConcurrentlyStatus status;
    final MessageQueue messageQueue = context.getMessageQueue();
    final String[] tags = {"metric", messageQueue.getTopic() + "-Q" + messageQueue.getQueueId(),
            "namespace", namespace,
            "consumeGroup", consumeGroup,
            "topic", messageQueue.getTopic(),
            "queueId", String.valueOf(messageQueue.getQueueId())};
    stats.incConc(tags);
    try {
        status = target.consumeMessage(msgs, context);
    } catch (Throwable t) {
        stats.error(tags);
        throw t;
    } finally {
        stats.observe(System.currentTimeMillis() - begin, tags);
        stats.decConc(tags);
    }
    return status;
}
 
Example #20
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("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 #21
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 #22
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 #23
Source File: DefaultMQPushConsumerTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
@Test
public void testPullMessage_Success() throws InterruptedException, RemotingException, MQBrokerException {
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    final MessageExt[] messageExts = new MessageExt[1];
    pushConsumer.getDefaultMQPushConsumerImpl().setConsumeMessageService(new ConsumeMessageConcurrentlyService(pushConsumer.getDefaultMQPushConsumerImpl(), new MessageListenerConcurrently() {
        @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
            ConsumeConcurrentlyContext context) {
            messageExts[0] = msgs.get(0);
            countDownLatch.countDown();
            return null;
        }
    }));

    PullMessageService pullMessageService = mQClientFactory.getPullMessageService();
    pullMessageService.executePullRequestImmediately(createPullRequest());
    countDownLatch.await();
    assertThat(messageExts[0].getTopic()).isEqualTo(topic);
    assertThat(messageExts[0].getBody()).isEqualTo(new byte[] {'a'});
}
 
Example #24
Source File: PushConsumer.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 {
    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(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n");
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });
    consumer.start();
    System.out.printf("Consumer Started.%n");
}
 
Example #25
Source File: DefaultMQPushConsumerTest.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
@Test
public void testPullMessage_Success() throws InterruptedException, RemotingException, MQBrokerException {
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    final MessageExt[] messageExts = new MessageExt[1];
    pushConsumer.getDefaultMQPushConsumerImpl().setConsumeMessageService(new ConsumeMessageConcurrentlyService(pushConsumer.getDefaultMQPushConsumerImpl(), new MessageListenerConcurrently() {
        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
            ConsumeConcurrentlyContext context) {
            messageExts[0] = msgs.get(0);
            countDownLatch.countDown();
            return null;
        }
    }));

    PullMessageService pullMessageService = mQClientFactory.getPullMessageService();
    pullMessageService.executePullRequestImmediately(createPullRequest());
    countDownLatch.await();
    assertThat(messageExts[0].getTopic()).isEqualTo(topic);
    assertThat(messageExts[0].getBody()).isEqualTo(new byte[] {'a'});
}
 
Example #26
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 #27
Source File: RMQNormalListener.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
    ConsumeConcurrentlyContext consumeConcurrentlyContext) {
    for (MessageExt msg : msgs) {
        msgIndex.getAndIncrement();
        if (isDebug) {
            if (listenerName != null && !listenerName.isEmpty()) {
                logger.info(listenerName + ":" + msgIndex.get() + ":"
                    + String.format("msgid:%s broker:%s queueId:%s offset:%s",
                    msg.getMsgId(), msg.getStoreHost(), msg.getQueueId(),
                    msg.getQueueOffset()));
            } else {
                logger.info(msg);
            }
        }

        msgBodys.addData(new String(msg.getBody()));
        originMsgs.addData(msg);
        if (originMsgIndex != null) {
            originMsgIndex.put(new String(msg.getBody()), msg);
        }
    }
    return consumeStatus;
}
 
Example #28
Source File: DefaultMQPushConsumerTest.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
@Test
public void testPullMessage_Success() throws InterruptedException, RemotingException, MQBrokerException {
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    final MessageExt[] messageExts = new MessageExt[1];
    pushConsumer.getDefaultMQPushConsumerImpl().setConsumeMessageService(new ConsumeMessageConcurrentlyService(pushConsumer.getDefaultMQPushConsumerImpl(), new MessageListenerConcurrently() {
        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
            ConsumeConcurrentlyContext context) {
            messageExts[0] = msgs.get(0);
            countDownLatch.countDown();
            return null;
        }
    }));

    PullMessageService pullMessageService = mQClientFactory.getPullMessageService();
    pullMessageService.executePullRequestImmediately(createPullRequest());
    countDownLatch.await();
    assertThat(messageExts[0].getTopic()).isEqualTo(topic);
    assertThat(messageExts[0].getBody()).isEqualTo(new byte[] {'a'});
}
 
Example #29
Source File: DefaultRocketMQListenerContainer.java    From rocketmq-spring with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
    for (MessageExt messageExt : msgs) {
        log.debug("received msg: {}", messageExt);
        try {
            long now = System.currentTimeMillis();
            handleMessage(messageExt);
            long costTime = System.currentTimeMillis() - now;
            log.debug("consume {} cost: {} ms", messageExt.getMsgId(), costTime);
        } catch (Exception e) {
            log.warn("consume message failed. messageExt:{}, error:{}", messageExt, e);
            context.setDelayLevelWhenNextConsume(delayLevelWhenNextConsume);
            return ConsumeConcurrentlyStatus.RECONSUME_LATER;
        }
    }

    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
 
Example #30
Source File: RMQNormalListener.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
    ConsumeConcurrentlyContext consumeConcurrentlyContext) {
    for (MessageExt msg : msgs) {
        msgIndex.getAndIncrement();
        if (isDebug) {
            if (listenerName != null && !listenerName.isEmpty()) {
                logger.info(listenerName + ":" + msgIndex.get() + ":"
                    + String.format("msgid:%s broker:%s queueId:%s offset:%s",
                    msg.getMsgId(), msg.getStoreHost(), msg.getQueueId(),
                    msg.getQueueOffset()));
            } else {
                logger.info(msg);
            }
        }

        msgBodys.addData(new String(msg.getBody()));
        originMsgs.addData(msg);
        if (originMsgIndex != null) {
            originMsgIndex.put(new String(msg.getBody()), msg);
        }
    }
    return consumeStatus;
}