org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus Java Examples
The following examples show how to use
org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus.
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: RMQOrderListener.java From rocketmq-4.3.0 with Apache License 2.0 | 6 votes |
public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) { for (MessageExt msg : msgs) { if (isDebug) { if (listenerName != null && listenerName != "") { logger.info(listenerName + ": " + msg); } else { logger.info(msg); } } putMsg(msg); msgBodys.addData(new String(msg.getBody())); originMsgs.addData(msg); } return ConsumeOrderlyStatus.SUCCESS; }
Example #2
Source File: ConsumeMessageOrderlyServiceTest.java From rocketmq with Apache License 2.0 | 6 votes |
@Test public void testConsumeMessageDirectly_WithNoException() { Map<ConsumeOrderlyStatus, CMResult> map = new HashMap(); map.put(ConsumeOrderlyStatus.SUCCESS, CMResult.CR_SUCCESS); map.put(ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT, CMResult.CR_LATER); map.put(ConsumeOrderlyStatus.COMMIT, CMResult.CR_COMMIT); map.put(ConsumeOrderlyStatus.ROLLBACK, CMResult.CR_ROLLBACK); map.put(null, CMResult.CR_RETURN_NULL); for (ConsumeOrderlyStatus consumeOrderlyStatus : map.keySet()) { final ConsumeOrderlyStatus status = consumeOrderlyStatus; MessageListenerOrderly listenerOrderly = new MessageListenerOrderly() { @Override public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) { return status; } }; ConsumeMessageOrderlyService consumeMessageOrderlyService = new ConsumeMessageOrderlyService(pushConsumer.getDefaultMQPushConsumerImpl(), listenerOrderly); MessageExt msg = new MessageExt(); msg.setTopic(topic); assertTrue(consumeMessageOrderlyService.consumeMessageDirectly(msg, brokerName).getConsumeResult().equals(map.get(consumeOrderlyStatus))); } }
Example #3
Source File: DefaultMQPushConsumerTest.java From DDMQ with Apache License 2.0 | 6 votes |
@Test public void testPullMessage_SuccessWithOrderlyService() throws Exception { final CountDownLatch countDownLatch = new CountDownLatch(1); final MessageExt[] messageExts = new MessageExt[1]; MessageListenerOrderly listenerOrderly = new MessageListenerOrderly() { @Override public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) { messageExts[0] = msgs.get(0); countDownLatch.countDown(); return null; } }; pushConsumer.registerMessageListener(listenerOrderly); pushConsumer.getDefaultMQPushConsumerImpl().setConsumeMessageService(new ConsumeMessageOrderlyService(pushConsumer.getDefaultMQPushConsumerImpl(), listenerOrderly)); pushConsumer.getDefaultMQPushConsumerImpl().setConsumeOrderly(true); pushConsumer.getDefaultMQPushConsumerImpl().doRebalance(); PullMessageService pullMessageService = mQClientFactory.getPullMessageService(); pullMessageService.executePullRequestLater(createPullRequest(), 100); countDownLatch.await(10, TimeUnit.SECONDS); assertThat(messageExts[0].getTopic()).isEqualTo(topic); assertThat(messageExts[0].getBody()).isEqualTo(new byte[] {'a'}); }
Example #4
Source File: RMQOrderListener.java From DDMQ with Apache License 2.0 | 6 votes |
public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) { for (MessageExt msg : msgs) { if (isDebug) { if (listenerName != null && listenerName != "") { logger.info(listenerName + ": " + msg); } else { logger.info(msg); } } putMsg(msg); msgBodys.addData(new String(msg.getBody())); originMsgs.addData(msg); } return ConsumeOrderlyStatus.SUCCESS; }
Example #5
Source File: DefaultMQPushConsumerTest.java From rocketmq with Apache License 2.0 | 6 votes |
@Test public void testPullMessage_SuccessWithOrderlyService() throws Exception { final CountDownLatch countDownLatch = new CountDownLatch(1); final MessageExt[] messageExts = new MessageExt[1]; MessageListenerOrderly listenerOrderly = new MessageListenerOrderly() { @Override public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) { messageExts[0] = msgs.get(0); countDownLatch.countDown(); return null; } }; pushConsumer.registerMessageListener(listenerOrderly); pushConsumer.getDefaultMQPushConsumerImpl().setConsumeMessageService(new ConsumeMessageOrderlyService(pushConsumer.getDefaultMQPushConsumerImpl(), listenerOrderly)); pushConsumer.getDefaultMQPushConsumerImpl().setConsumeOrderly(true); pushConsumer.getDefaultMQPushConsumerImpl().doRebalance(); PullMessageService pullMessageService = mQClientFactory.getPullMessageService(); pullMessageService.executePullRequestLater(createPullRequest(), 100); countDownLatch.await(10, TimeUnit.SECONDS); assertThat(messageExts[0].getTopic()).isEqualTo(topic); assertThat(messageExts[0].getBody()).isEqualTo(new byte[] {'a'}); }
Example #6
Source File: RMQOrderListener.java From rocketmq with Apache License 2.0 | 6 votes |
@Override public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) { for (MessageExt msg : msgs) { if (isDebug) { if (listenerName != null && listenerName != "") { logger.info(listenerName + ": " + msg); } else { logger.info(msg); } } putMsg(msg); msgBodys.addData(new String(msg.getBody())); originMsgs.addData(msg); } return ConsumeOrderlyStatus.SUCCESS; }
Example #7
Source File: RocketmqMessageSubscriberFactory.java From framework with Apache License 2.0 | 6 votes |
/** * Description: <br> * * @author 王伟<br> * @taskId <br> * @param channel * @param broadcast * @param subscriber <br> */ private void consumeOrderly(final String channel, final boolean broadcast, final MessageSubscriber subscriber) { RocketmqFactory.getPushConsumer(channel, channel, broadcast, (MessageListenerOrderly) (msgs, context) -> { // 自动提交 更新消费队列的位置 context.setAutoCommit(true); if (msgs.size() == 0) { return ConsumeOrderlyStatus.SUCCESS; } // 事件监听 for (MessageExt messageExt : msgs) { subscriber.onMessage(messageExt.getTopic(), messageExt.getBody()); } return ConsumeOrderlyStatus.SUCCESS; }); }
Example #8
Source File: RMQOrderListener.java From rocketmq_trans_message with Apache License 2.0 | 6 votes |
public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) { for (MessageExt msg : msgs) { if (isDebug) { if (listnerName != null && listnerName != "") { logger.info(listnerName + ": " + msg); } else { logger.info(msg); } } putMsg(msg); msgBodys.addData(new String(msg.getBody())); originMsgs.addData(msg); } return ConsumeOrderlyStatus.SUCCESS; }
Example #9
Source File: DefaultMQPushConsumerTest.java From rocketmq-4.3.0 with Apache License 2.0 | 6 votes |
@Test public void testPullMessage_SuccessWithOrderlyService() throws Exception { final CountDownLatch countDownLatch = new CountDownLatch(1); final MessageExt[] messageExts = new MessageExt[1]; MessageListenerOrderly listenerOrderly = new MessageListenerOrderly() { @Override public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) { messageExts[0] = msgs.get(0); countDownLatch.countDown(); return null; } }; pushConsumer.registerMessageListener(listenerOrderly); pushConsumer.getDefaultMQPushConsumerImpl().setConsumeMessageService(new ConsumeMessageOrderlyService(pushConsumer.getDefaultMQPushConsumerImpl(), listenerOrderly)); pushConsumer.getDefaultMQPushConsumerImpl().setConsumeOrderly(true); pushConsumer.getDefaultMQPushConsumerImpl().doRebalance(); PullMessageService pullMessageService = mQClientFactory.getPullMessageService(); pullMessageService.executePullRequestLater(createPullRequest(), 100); countDownLatch.await(10, TimeUnit.SECONDS); assertThat(messageExts[0].getTopic()).isEqualTo(topic); assertThat(messageExts[0].getBody()).isEqualTo(new byte[] {'a'}); }
Example #10
Source File: RMQOrderListener.java From rocketmq-all-4.1.0-incubating with Apache License 2.0 | 6 votes |
public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) { for (MessageExt msg : msgs) { if (isDebug) { if (listnerName != null && listnerName != "") { logger.info(listnerName + ": " + msg); } else { logger.info(msg); } } putMsg(msg); msgBodys.addData(new String(msg.getBody())); originMsgs.addData(msg); } return ConsumeOrderlyStatus.SUCCESS; }
Example #11
Source File: DefaultMQPushConsumerTest.java From rocketmq-read with Apache License 2.0 | 6 votes |
@Test public void testPullMessage_SuccessWithOrderlyService() throws Exception { final CountDownLatch countDownLatch = new CountDownLatch(1); final MessageExt[] messageExts = new MessageExt[1]; MessageListenerOrderly listenerOrderly = new MessageListenerOrderly() { @Override public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) { messageExts[0] = msgs.get(0); countDownLatch.countDown(); return null; } }; pushConsumer.registerMessageListener(listenerOrderly); pushConsumer.getDefaultMQPushConsumerImpl().setConsumeMessageService(new ConsumeMessageOrderlyService(pushConsumer.getDefaultMQPushConsumerImpl(), listenerOrderly)); pushConsumer.getDefaultMQPushConsumerImpl().setConsumeOrderly(true); pushConsumer.getDefaultMQPushConsumerImpl().doRebalance(); PullMessageService pullMessageService = mQClientFactory.getPullMessageService(); pullMessageService.executePullRequestLater(createPullRequest(), 100); countDownLatch.await(10, TimeUnit.SECONDS); assertThat(messageExts[0].getTopic()).isEqualTo(topic); assertThat(messageExts[0].getBody()).isEqualTo(new byte[] {'a'}); }
Example #12
Source File: RMQOrderListener.java From rocketmq-read with Apache License 2.0 | 6 votes |
public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) { for (MessageExt msg : msgs) { if (isDebug) { if (listenerName != null && listenerName != "") { logger.info(listenerName + ": " + msg); } else { logger.info(msg); } } putMsg(msg); msgBodys.addData(new String(msg.getBody())); originMsgs.addData(msg); } return ConsumeOrderlyStatus.SUCCESS; }
Example #13
Source File: DefaultMQPushConsumerTest.java From rocketmq-all-4.1.0-incubating with Apache License 2.0 | 6 votes |
@Test public void testPullMessage_SuccessWithOrderlyService() throws Exception { final CountDownLatch countDownLatch = new CountDownLatch(1); final MessageExt[] messageExts = new MessageExt[1]; MessageListenerOrderly listenerOrderly = new MessageListenerOrderly() { @Override public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) { messageExts[0] = msgs.get(0); countDownLatch.countDown(); return null; } }; pushConsumer.registerMessageListener(listenerOrderly); pushConsumer.getDefaultMQPushConsumerImpl().setConsumeMessageService(new ConsumeMessageOrderlyService(pushConsumer.getDefaultMQPushConsumerImpl(), listenerOrderly)); pushConsumer.getDefaultMQPushConsumerImpl().setConsumeOrderly(true); pushConsumer.getDefaultMQPushConsumerImpl().doRebalance(); PullMessageService pullMessageService = mQClientFactory.getPullMessageService(); pullMessageService.executePullRequestLater(createPullRequest(), 100); countDownLatch.await(10, TimeUnit.SECONDS); assertThat(messageExts[0].getTopic()).isEqualTo(topic); assertThat(messageExts[0].getBody()).isEqualTo(new byte[] {'a'}); }
Example #14
Source File: RocketMQListenerBindingContainer.java From spring-cloud-alibaba with Apache License 2.0 | 6 votes |
@SuppressWarnings({ "unchecked", "Duplicates" }) @Override public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) { for (MessageExt messageExt : msgs) { log.debug("received msg: {}", messageExt); try { long now = System.currentTimeMillis(); rocketMQListener.onMessage(convertToSpringMessage(messageExt)); long costTime = System.currentTimeMillis() - now; log.info("consume {} message key:[{}] cost: {} ms", messageExt.getMsgId(), messageExt.getKeys(), costTime); } catch (Exception e) { log.warn("consume message failed. messageExt:{}", messageExt, e); context.setSuspendCurrentQueueTimeMillis( suspendCurrentQueueTimeMillis); return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT; } } return ConsumeOrderlyStatus.SUCCESS; }
Example #15
Source File: DefaultRocketMQListenerContainer.java From rocketmq-spring with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") @Override public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) { for (MessageExt messageExt : msgs) { log.debug("received msg: {}", messageExt); try { long now = System.currentTimeMillis(); handleMessage(messageExt); long costTime = System.currentTimeMillis() - now; log.info("consume {} cost: {} ms", messageExt.getMsgId(), costTime); } catch (Exception e) { log.warn("consume message failed. messageExt:{}", messageExt, e); context.setSuspendCurrentQueueTimeMillis(suspendCurrentQueueTimeMillis); return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT; } } return ConsumeOrderlyStatus.SUCCESS; }
Example #16
Source File: PushConsumer2.java From blog with BSD 2-Clause "Simplified" License | 6 votes |
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 #17
Source File: DefaultMQPushConsumerTest.java From rocketmq with Apache License 2.0 | 6 votes |
@Test public void testPullMessage_SuccessWithOrderlyService() throws Exception { final CountDownLatch countDownLatch = new CountDownLatch(1); final MessageExt[] messageExts = new MessageExt[1]; MessageListenerOrderly listenerOrderly = new MessageListenerOrderly() { @Override public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) { messageExts[0] = msgs.get(0); countDownLatch.countDown(); return null; } }; pushConsumer.registerMessageListener(listenerOrderly); pushConsumer.getDefaultMQPushConsumerImpl().setConsumeMessageService(new ConsumeMessageOrderlyService(pushConsumer.getDefaultMQPushConsumerImpl(), listenerOrderly)); pushConsumer.getDefaultMQPushConsumerImpl().setConsumeOrderly(true); pushConsumer.getDefaultMQPushConsumerImpl().doRebalance(); PullMessageService pullMessageService = mQClientFactory.getPullMessageService(); pullMessageService.executePullRequestLater(createPullRequest(), 100); countDownLatch.await(10, TimeUnit.SECONDS); assertThat(messageExts[0].getTopic()).isEqualTo(topic); assertThat(messageExts[0].getBody()).isEqualTo(new byte[] {'a'}); }
Example #18
Source File: RMQOrderListener.java From rocketmq with Apache License 2.0 | 6 votes |
public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) { for (MessageExt msg : msgs) { if (isDebug) { if (listnerName != null && listnerName != "") { logger.info(listnerName + ": " + msg); } else { logger.info(msg); } } putMsg(msg); msgBodys.addData(new String(msg.getBody())); originMsgs.addData(msg); } return ConsumeOrderlyStatus.SUCCESS; }
Example #19
Source File: RMQOrderListener.java From DDMQ with Apache License 2.0 | 6 votes |
public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) { for (MessageExt msg : msgs) { if (isDebug) { if (listenerName != null && listenerName != "") { logger.info(listenerName + ": " + msg); } else { logger.info(msg); } } putMsg(msg); msgBodys.addData(new String(msg.getBody())); originMsgs.addData(msg); } return ConsumeOrderlyStatus.SUCCESS; }
Example #20
Source File: DefaultMQPushConsumerTest.java From DDMQ with Apache License 2.0 | 6 votes |
@Test public void testPullMessage_SuccessWithOrderlyService() throws Exception { final CountDownLatch countDownLatch = new CountDownLatch(1); final MessageExt[] messageExts = new MessageExt[1]; MessageListenerOrderly listenerOrderly = new MessageListenerOrderly() { @Override public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) { messageExts[0] = msgs.get(0); countDownLatch.countDown(); return null; } }; pushConsumer.registerMessageListener(listenerOrderly); pushConsumer.getDefaultMQPushConsumerImpl().setConsumeMessageService(new ConsumeMessageOrderlyService(pushConsumer.getDefaultMQPushConsumerImpl(), listenerOrderly)); pushConsumer.getDefaultMQPushConsumerImpl().setConsumeOrderly(true); pushConsumer.getDefaultMQPushConsumerImpl().doRebalance(); PullMessageService pullMessageService = mQClientFactory.getPullMessageService(); pullMessageService.executePullRequestLater(createPullRequest(), 100); countDownLatch.await(10, TimeUnit.SECONDS); assertThat(messageExts[0].getTopic()).isEqualTo(topic); assertThat(messageExts[0].getBody()).isEqualTo(new byte[] {'a'}); }
Example #21
Source File: OrderedConsumer.java From javatech with Creative Commons Attribution Share Alike 4.0 International | 5 votes |
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 #22
Source File: Consumer.java From rocketmq_trans_message with Apache License 2.0 | 5 votes |
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.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 #23
Source File: RocketMQApplicationMessageListener.java From enode with MIT License | 5 votes |
@Override public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) { final CountDownLatch latch = new CountDownLatch(msgs.size()); msgs.forEach(messageExt -> { QueueMessage queueMessage = RocketMQTool.covertToQueueMessage(messageExt); applicationMessageListener.handle(queueMessage, message -> { latch.countDown(); }); }); Task.await(latch); return ConsumeOrderlyStatus.SUCCESS; }
Example #24
Source File: AbstractMQPushConsumer.java From rocketmq-spring-boot-starter with Apache License 2.0 | 5 votes |
/** * 原生dealMessage方法,可以重写此方法自定义序列化和返回消费成功的相关逻辑 * * @param list 消息列表 * @param consumeOrderlyContext 上下文 * @return 处理结果 */ public ConsumeOrderlyStatus dealMessage(List<MessageExt> list, ConsumeOrderlyContext consumeOrderlyContext) { for(MessageExt messageExt : list) { log.info("receive msgId: {}, tags : {}" , messageExt.getMsgId(), messageExt.getTags()); T t = parseMessage(messageExt); Map<String, Object> ext = parseExtParam(messageExt); if( null != t && !process(t, ext)) { log.warn("consume fail , ask for re-consume , msgId: {}", messageExt.getMsgId()); return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT; } } return ConsumeOrderlyStatus.SUCCESS; }
Example #25
Source File: OrderConsumer.java From blog with MIT License | 5 votes |
public static void main(String[] args) throws MQClientException { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("Order_Consumer"); consumer.setNamesrvAddr("127.0.0.1:9876"); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.subscribe("OrderTopic", "*"); /** TODO: 设置最小消费线程数 */ consumer.setConsumeThreadMin(1); /** TODO: 设置最大消费线程数 */ consumer.setConsumeThreadMax(1); consumer.registerMessageListener( // 顺序消费,一个 Queue 对应一个线程 new MessageListenerOrderly() { @Override public ConsumeOrderlyStatus consumeMessage( List<MessageExt> msgs, ConsumeOrderlyContext context) { System.out.println( Thread.currentThread().getName() + " message: " + new String(msgs.get(0).getBody()) + " QueueID: " + msgs.get(0).getQueueId()); return ConsumeOrderlyStatus.SUCCESS; } }); consumer.start(); System.out.println("Consumer 已启动"); }
Example #26
Source File: ConsumeMessageOrderlyServiceTest.java From rocketmq with Apache License 2.0 | 5 votes |
@Test public void testConsumeMessageDirectly_WithException() { MessageListenerOrderly listenerOrderly = new MessageListenerOrderly() { @Override public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) { throw new RuntimeException(); } }; ConsumeMessageOrderlyService consumeMessageOrderlyService = new ConsumeMessageOrderlyService(pushConsumer.getDefaultMQPushConsumerImpl(), listenerOrderly); MessageExt msg = new MessageExt(); msg.setTopic(topic); assertTrue(consumeMessageOrderlyService.consumeMessageDirectly(msg, brokerName).getConsumeResult().equals(CMResult.CR_THROW_EXCEPTION)); }
Example #27
Source File: CarreraNewRocketMqConsumer.java From DDMQ with Apache License 2.0 | 5 votes |
@Override protected ConsumeOrderlyStatus consumeRocketMQMessages(List<MessageExt> msgs, MessageQueue mq) { if (!isRunning) { return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT; } if (msgs.size() <= 0) { return ConsumeOrderlyStatus.SUCCESS; } assert msgs.size() == 1; String topic = msgs.get(0).getTopic(); if (!LimiterMgr.getInstance().doNonBlockLimit(getGroupBrokerCluster(), topic, msgs.size())) { return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT_FOR_BLOCK; } for (final MessageExt msg : msgs) { final CommonMessage commonMessage = new CommonMessage(msg.getTopic(), msg.getKeys(), msg.getBody()); final ConsumeContext context = new ConsumeContext(RMQ, group); context.setStartTime(System.currentTimeMillis()); context.setMessageQueue(mq); context.setOffset(msg.getQueueOffset()); context.setOriginMessage(msg); context.setQid(QidUtils.rmqMakeQid(rocketmqConfiguration.getClusterName(), mq.getBrokerName(), mq.getQueueId())); context.setProperties(CommonUtils.mapRemoveKeys(msg.getProperties(), MessageConst.STRING_HASH_SET)); try { if (commitLagLimiter != null && !commitLagLimiter.tryAcquire(tracker, topic, context)) { return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT_FOR_BLOCK; } handleMessage(commonMessage, context); } catch (Exception e) { logConsumeException(commonMessage, context, e); return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT; } } return ConsumeOrderlyStatus.SUCCESS; }
Example #28
Source File: MessageOrderlyConsumeInterceptor.java From skywalking with Apache License 2.0 | 5 votes |
@Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable { ConsumeOrderlyStatus status = (ConsumeOrderlyStatus) ret; if (status == ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT) { AbstractSpan activeSpan = ContextManager.activeSpan(); activeSpan.errorOccurred(); Tags.STATUS_CODE.set(activeSpan, status.name()); } ContextManager.stopSpan(); return ret; }
Example #29
Source File: OrderedConsumer.java From spring-boot-starter-samples with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws Exception { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("example_group_name"); 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 #30
Source File: RocketMQCanalConnector.java From canal with Apache License 2.0 | 5 votes |
public synchronized void subscribe(String filter) throws CanalClientException { if (connected) { return; } try { if (rocketMQConsumer == null) { this.connect(); } rocketMQConsumer.subscribe(this.topic, "*"); rocketMQConsumer.registerMessageListener(new MessageListenerOrderly() { @Override public ConsumeOrderlyStatus consumeMessage(List<MessageExt> messageExts, ConsumeOrderlyContext context) { context.setAutoCommit(true); boolean isSuccess = process(messageExts); if (isSuccess) { return ConsumeOrderlyStatus.SUCCESS; } else { return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT; } } }); rocketMQConsumer.start(); } catch (MQClientException ex) { connected = false; logger.error("Start RocketMQ consumer error", ex); } connected = true; }