Search in sources :

Example 1 with MessageListenerOrderly

use of org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly in project rocketmq-externals by apache.

the class RocketMQReceiver method onStart.

@Override
public void onStart() {
    Validate.notEmpty(properties, "Consumer properties can not be empty");
    ordered = RocketMqUtils.getBoolean(properties, RocketMQConfig.CONSUMER_MESSAGES_ORDERLY, false);
    consumer = new DefaultMQPushConsumer();
    RocketMQConfig.buildConsumerConfigs(properties, (DefaultMQPushConsumer) consumer);
    if (ordered) {
        consumer.registerMessageListener(new MessageListenerOrderly() {

            @Override
            public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
                if (process(msgs)) {
                    return ConsumeOrderlyStatus.SUCCESS;
                } else {
                    return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT;
                }
            }
        });
    } else {
        consumer.registerMessageListener(new MessageListenerConcurrently() {

            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                if (process(msgs)) {
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                } else {
                    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                }
            }
        });
    }
    try {
        consumer.start();
    } catch (MQClientException e) {
        throw new RuntimeException(e);
    }
}
Also used : ConsumeConcurrentlyContext(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext) MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageListenerConcurrently(org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently) MessageListenerOrderly(org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly) ConsumeOrderlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus) ConsumeConcurrentlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) ConsumeOrderlyContext(org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 2 with MessageListenerOrderly

use of org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly in project spring-boot-starter-samples by vindell.

the class OrderedConsumer method main.

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");
}
Also used : MessageExt(org.apache.rocketmq.common.message.MessageExt) AtomicLong(java.util.concurrent.atomic.AtomicLong) MessageListenerOrderly(org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly) ConsumeOrderlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) ConsumeOrderlyContext(org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext)

Aggregations

DefaultMQPushConsumer (org.apache.rocketmq.client.consumer.DefaultMQPushConsumer)2 ConsumeOrderlyContext (org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext)2 ConsumeOrderlyStatus (org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus)2 MessageListenerOrderly (org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly)2 MessageExt (org.apache.rocketmq.common.message.MessageExt)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 ConsumeConcurrentlyContext (org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext)1 ConsumeConcurrentlyStatus (org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus)1 MessageListenerConcurrently (org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently)1 MQClientException (org.apache.rocketmq.client.exception.MQClientException)1