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

The following examples show how to use org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly. 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: DefaultMQPushConsumerTest.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
@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 #2
Source File: ConsumeMessageOrderlyService.java    From rocketmq_trans_message with Apache License 2.0 6 votes vote down vote up
public ConsumeMessageOrderlyService(DefaultMQPushConsumerImpl defaultMQPushConsumerImpl, MessageListenerOrderly messageListener) {
    this.defaultMQPushConsumerImpl = defaultMQPushConsumerImpl;
    this.messageListener = messageListener;

    this.defaultMQPushConsumer = this.defaultMQPushConsumerImpl.getDefaultMQPushConsumer();
    this.consumerGroup = this.defaultMQPushConsumer.getConsumerGroup();
    this.consumeRequestQueue = new LinkedBlockingQueue<Runnable>();

    this.consumeExecutor = new ThreadPoolExecutor(//
        this.defaultMQPushConsumer.getConsumeThreadMin(), //
        this.defaultMQPushConsumer.getConsumeThreadMax(), //
        1000 * 60, //
        TimeUnit.MILLISECONDS, //
        this.consumeRequestQueue, //
        new ThreadFactoryImpl("ConsumeMessageThread_"));

    this.scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryImpl("ConsumeMessageScheduledThread_"));
}
 
Example #3
Source File: ConsumeMessageOrderlyService.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public ConsumeMessageOrderlyService(DefaultMQPushConsumerImpl defaultMQPushConsumerImpl, MessageListenerOrderly messageListener) {
    this.defaultMQPushConsumerImpl = defaultMQPushConsumerImpl;
    this.messageListener = messageListener;

    this.defaultMQPushConsumer = this.defaultMQPushConsumerImpl.getDefaultMQPushConsumer();
    this.consumerGroup = this.defaultMQPushConsumer.getConsumerGroup();
    this.consumeRequestQueue = new LinkedBlockingQueue<Runnable>();

    this.consumeExecutor = new ThreadPoolExecutor(//
        this.defaultMQPushConsumer.getConsumeThreadMin(), //
        this.defaultMQPushConsumer.getConsumeThreadMax(), //
        1000 * 60, //
        TimeUnit.MILLISECONDS, //
        this.consumeRequestQueue, //
        new ThreadFactoryImpl("ConsumeMessageThread_"));

    this.scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryImpl("ConsumeMessageScheduledThread_"));
}
 
Example #4
Source File: DefaultMQPushConsumerTest.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
@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 #5
Source File: DefaultMQPushConsumerTest.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
@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: ConsumeMessageOrderlyService.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
/**
 * 构造顺序消费的Service
 * @param defaultMQPushConsumerImpl ;
 * @param messageListener ;
 */
public ConsumeMessageOrderlyService(DefaultMQPushConsumerImpl defaultMQPushConsumerImpl,
    MessageListenerOrderly messageListener) {

    this.defaultMQPushConsumerImpl = defaultMQPushConsumerImpl;
    this.messageListener = messageListener;

    this.defaultMQPushConsumer = this.defaultMQPushConsumerImpl.getDefaultMQPushConsumer();
    this.consumerGroup = this.defaultMQPushConsumer.getConsumerGroup();
    this.consumeRequestQueue = new LinkedBlockingQueue<Runnable>();

    //消费线程池
    this.consumeExecutor = new ThreadPoolExecutor(
        this.defaultMQPushConsumer.getConsumeThreadMin(),
        this.defaultMQPushConsumer.getConsumeThreadMax(),
        1000 * 60,
        TimeUnit.MILLISECONDS,
        this.consumeRequestQueue,
        new ThreadFactoryImpl("ConsumeMessageThread_"));
    //调度线程池
    this.scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryImpl("ConsumeMessageScheduledThread_"));
}
 
Example #7
Source File: DefaultMQPushConsumerTest.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
@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 #8
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 #9
Source File: ConsumeMessageOrderlyService.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
public ConsumeMessageOrderlyService(DefaultMQPushConsumerImpl defaultMQPushConsumerImpl,
        MessageListenerOrderly messageListener) {
        this.defaultMQPushConsumerImpl = defaultMQPushConsumerImpl;
        this.messageListener = messageListener;

        this.defaultMQPushConsumer = this.defaultMQPushConsumerImpl.getDefaultMQPushConsumer();
        this.consumerGroup = this.defaultMQPushConsumer.getConsumerGroup();
//        阻塞队列保证异步监听是有序的
        this.consumeRequestQueue = new LinkedBlockingQueue<Runnable>();

        this.consumeExecutor = new ThreadPoolExecutor(
            this.defaultMQPushConsumer.getConsumeThreadMin(),
            this.defaultMQPushConsumer.getConsumeThreadMax(),
            1000 * 60,
            TimeUnit.MILLISECONDS,
            this.consumeRequestQueue,
            new ThreadFactoryImpl("ConsumeMessageThread_"));

        this.scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryImpl("ConsumeMessageScheduledThread_"));
    }
 
Example #10
Source File: ConsumeMessageOrderlyService.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public ConsumeMessageOrderlyService(DefaultMQPushConsumerImpl defaultMQPushConsumerImpl,
    MessageListenerOrderly messageListener) {
    this.defaultMQPushConsumerImpl = defaultMQPushConsumerImpl;
    this.messageListener = messageListener;

    this.defaultMQPushConsumer = this.defaultMQPushConsumerImpl.getDefaultMQPushConsumer();
    this.consumerGroup = this.defaultMQPushConsumer.getConsumerGroup();
    this.consumeRequestQueue = new LinkedBlockingQueue<Runnable>();

    this.consumeExecutor = new ThreadPoolExecutor(
        this.defaultMQPushConsumer.getConsumeThreadMin(),
        this.defaultMQPushConsumer.getConsumeThreadMax(),
        1000 * 60,
        TimeUnit.MILLISECONDS,
        this.consumeRequestQueue,
        new ThreadFactoryImpl("ConsumeMessageThread_"));

    this.scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryImpl("ConsumeMessageScheduledThread_"));
}
 
Example #11
Source File: DefaultMQPushConsumerTest.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
@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: ConsumeMessageOrderlyServiceTest.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
@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 #13
Source File: DefaultMQPushConsumerTest.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
@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: DefaultMQPushConsumerTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
@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 #15
Source File: RocketmqMessageConsumerListenerTest.java    From message-queue-client-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void getMessageListener() throws Exception {

    RocketmqMessageConsumerListener<String> listener = new RocketmqMessageConsumerListener<String>();
    Assert.assertNotNull(listener.getBatch());
    Assert.assertNotNull(listener.getModel());
    Assert.assertNull(listener.getConsumer());
    Assert.assertNull(listener.getMessageDecoder());

    Assert.assertTrue(listener.getMessageListener() instanceof MessageListenerOrderly);

    listener.setBatch(MQ_BATCH.NON_BATCH.name());
    listener.setModel(MQ_MODEL.MODEL_2.name());
    listener.setConsumer(new MessageConsumer<String>());
    listener.setMessageDecoder(new RocketmqMessageDecoderDemo());

    Assert.assertTrue(listener.getMessageListener() instanceof MessageListenerConcurrently);

}
 
Example #16
Source File: RocketmqMessageSubscriberFactory.java    From framework with Apache License 2.0 6 votes vote down vote up
/**
 * 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 #17
Source File: ConsumeMessageOrderlyService.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
public ConsumeMessageOrderlyService(DefaultMQPushConsumerImpl defaultMQPushConsumerImpl, MessageListenerOrderly messageListener) {
    this.defaultMQPushConsumerImpl = defaultMQPushConsumerImpl;
    this.messageListener = messageListener;

    this.defaultMQPushConsumer = this.defaultMQPushConsumerImpl.getDefaultMQPushConsumer();
    this.consumerGroup = this.defaultMQPushConsumer.getConsumerGroup();
    this.consumeRequestQueue = new LinkedBlockingQueue<Runnable>();

    this.consumeExecutor = new ThreadPoolExecutor(//
        this.defaultMQPushConsumer.getConsumeThreadMin(), //
        this.defaultMQPushConsumer.getConsumeThreadMax(), //
        1000 * 60, //
        TimeUnit.MILLISECONDS, //
        this.consumeRequestQueue, //
        new ThreadFactoryImpl("ConsumeMessageThread_"));

    this.scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryImpl("ConsumeMessageScheduledThread_"));
}
 
Example #18
Source File: RocketMQCanalConnector.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #19
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 #20
Source File: Consumer.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws 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 #21
Source File: Consumer.java    From rocketmq_trans_message with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws 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 #22
Source File: ConsumeMessageOrderlyService.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public ConsumeMessageOrderlyService(DefaultMQPushConsumerImpl defaultMQPushConsumerImpl,
    MessageListenerOrderly messageListener) {
    this.defaultMQPushConsumerImpl = defaultMQPushConsumerImpl;
    this.messageListener = messageListener;

    this.defaultMQPushConsumer = this.defaultMQPushConsumerImpl.getDefaultMQPushConsumer();
    this.consumerGroup = this.defaultMQPushConsumer.getConsumerGroup();
    this.consumeRequestQueue = new LinkedBlockingQueue<Runnable>();

    if (this.defaultMQPushConsumer.getSharedThreadPoolExecutor() == null) {
        this.consumeExecutor = new ThreadPoolExecutor(
                this.defaultMQPushConsumer.getConsumeThreadMin(),
                this.defaultMQPushConsumer.getConsumeThreadMax(),
                1000 * 60,
                TimeUnit.MILLISECONDS,
                this.consumeRequestQueue,
                new ThreadFactoryImpl("ConsumeMessageThread_" + consumerGroup + "_"));
    } else {
        this.consumeExecutor = this.defaultMQPushConsumer.getSharedThreadPoolExecutor();
    }

    if (this.defaultMQPushConsumer.getSharedScheduledExecutorService() == null) {
        this.scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(
                new ThreadFactoryImpl("ConsumeMessageScheduledThread_" + consumerGroup + "_"));
    } else {
        this.scheduledExecutorService = this.defaultMQPushConsumer.getSharedScheduledExecutorService();
    }
}
 
Example #23
Source File: Consumer.java    From DDMQ 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.registerMessageListener(new MessageListenerOrderly() {
        AtomicLong consumeTimes = new AtomicLong(0);

        @Override
        public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
            context.setAutoCommit(false);
            System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);
            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 #24
Source File: Consumer.java    From DDMQ 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.registerMessageListener(new MessageListenerOrderly() {
        AtomicLong consumeTimes = new AtomicLong(0);

        @Override
        public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
            context.setAutoCommit(false);
            System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);
            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 #25
Source File: OrderedConsumer.java    From spring-boot-starter-samples with Apache License 2.0 5 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);

	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 #26
Source File: OrderConsumer.java    From blog with MIT License 5 votes vote down vote up
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 #27
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 #28
Source File: RocketMQConsumer.java    From incubator-iotdb with Apache License 2.0 5 votes vote down vote up
/**
 * Subscribe topic and add register Listener
 *
 * @throws MQClientException
 */
public void prepareConsume() throws MQClientException {
  /**
   * Subscribe one more more topics to consume.
   */
  consumer.subscribe(Constant.TOPIC, "*");
  /**
   * Setting Consumer to start first from the head of the queue or from the tail of the queue
   * If not for the first time, then continue to consume according to the position of last consumption.
   */
  consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
  /**
   * Register callback to execute on arrival of messages fetched from brokers.
   */
  consumer.registerMessageListener((MessageListenerOrderly) (msgs, context) -> {
    for (MessageExt msg : msgs) {
      logger
          .info(String.format("%s Receive New Messages: %s %n", Thread.currentThread().getName(),
              new String(msg.getBody())));
      try {
        insert(new String(msg.getBody()));
      } catch (Exception e) {
        logger.error(e.getMessage());
      }
    }
    return ConsumeOrderlyStatus.SUCCESS;
  });
}
 
Example #29
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.registerMessageListener(new MessageListenerOrderly() {
        AtomicLong consumeTimes = new AtomicLong(0);

        @Override
        public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
            context.setAutoCommit(true);
            System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);
            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: Consumer.java    From rocketmq-read 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.registerMessageListener(new MessageListenerOrderly() {
        AtomicLong consumeTimes = new AtomicLong(0);

        @Override
        public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
            context.setAutoCommit(false);
            System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);
            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");
}