com.alibaba.rocketmq.client.consumer.listener.ConsumeOrderlyStatus Java Examples

The following examples show how to use com.alibaba.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: Consumer.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.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.println(Thread.currentThread().getName() + " Receive New Messages: " + 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.println("Consumer Started.");
}
 
Example #2
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(false);
            System.out.println(Thread.currentThread().getName() + " Receive New Messages: " + 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.println("Consumer Started.");
}
 
Example #3
Source File: MessageOrderlyConsumeInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@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 #4
Source File: JvmCacheMessageListenerOrderly.java    From easyooo-framework with Apache License 2.0 5 votes vote down vote up
@Override
public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs,
		ConsumeOrderlyContext context) {
	MessageExt msg = msgs.get(0);
	String jsonString = new String(msg.getBody());
	try {
		UpdateBody body = seriaziler.deserializeAsObject(
				jsonString, UpdateBody.class);
		if(body == null){
			return ConsumeOrderlyStatus.SUCCESS;
		}
		
		logger.info("receive a synchronous JVM caching from "
				+ body.getClientId());
		
		if(ClientIdGenerator.getClientId().equals(body.getClientId())){
			logger.info("Ignore it, because he was himself a sender");
		}
		updateManager.update(body.getCommands());
	} catch (SerializationException e) {
		logger.error("serialized message error", e);
		if(!logger.isDebugEnabled()){
			logger.debug("message data: " + jsonString);
		}
	}
	
	return ConsumeOrderlyStatus.SUCCESS;
}
 
Example #5
Source File: ConsumeMessageOrderlyService.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 4 votes vote down vote up
public boolean processConsumeResult(//
        final List<MessageExt> msgs, //
        final ConsumeOrderlyStatus status, //
        final ConsumeOrderlyContext context, //
        final ConsumeRequest consumeRequest//
) {
    boolean continueConsume = true;
    long commitOffset = -1L;
    if (context.isAutoCommit()) {
        switch (status) {
        case COMMIT:
        case ROLLBACK:
            log.warn(
                "the message queue consume result is illegal, we think you want to ack these message {}",
                consumeRequest.getMessageQueue());
        case SUCCESS:
            commitOffset = consumeRequest.getProcessQueue().commit();
            this.getConsumerStatsManager().incConsumeOKTPS(consumerGroup,
                consumeRequest.getMessageQueue().getTopic(), msgs.size());
            break;
        case SUSPEND_CURRENT_QUEUE_A_MOMENT:
            consumeRequest.getProcessQueue().makeMessageToCosumeAgain(msgs);
            this.submitConsumeRequestLater(//
                consumeRequest.getProcessQueue(), //
                consumeRequest.getMessageQueue(), //
                context.getSuspendCurrentQueueTimeMillis());
            continueConsume = false;

            this.getConsumerStatsManager().incConsumeFailedTPS(consumerGroup,
                consumeRequest.getMessageQueue().getTopic(), msgs.size());
            break;
        default:
            break;
        }
    }
    else {
        switch (status) {
        case SUCCESS:
            this.getConsumerStatsManager().incConsumeOKTPS(consumerGroup,
                consumeRequest.getMessageQueue().getTopic(), msgs.size());
            break;
        case COMMIT:
            commitOffset = consumeRequest.getProcessQueue().commit();
            break;
        case ROLLBACK:
            consumeRequest.getProcessQueue().rollback();
            this.submitConsumeRequestLater(//
                consumeRequest.getProcessQueue(), //
                consumeRequest.getMessageQueue(), //
                context.getSuspendCurrentQueueTimeMillis());
            continueConsume = false;
            break;
        case SUSPEND_CURRENT_QUEUE_A_MOMENT:
            consumeRequest.getProcessQueue().makeMessageToCosumeAgain(msgs);
            this.submitConsumeRequestLater(//
                consumeRequest.getProcessQueue(), //
                consumeRequest.getMessageQueue(), //
                context.getSuspendCurrentQueueTimeMillis());
            continueConsume = false;
            this.getConsumerStatsManager().incConsumeFailedTPS(consumerGroup,
                consumeRequest.getMessageQueue().getTopic(), msgs.size());
            break;
        default:
            break;
        }
    }

    if (commitOffset >= 0) {
        this.defaultMQPushConsumerImpl.getOffsetStore().updateOffset(consumeRequest.getMessageQueue(),
            commitOffset, false);
    }

    return continueConsume;
}
 
Example #6
Source File: ConsumeMessageOrderlyService.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 4 votes vote down vote up
@Override
public ConsumeMessageDirectlyResult consumeMessageDirectly(MessageExt msg, String brokerName) {
    ConsumeMessageDirectlyResult result = new ConsumeMessageDirectlyResult();
    result.setOrder(true);

    List<MessageExt> msgs = new ArrayList<MessageExt>();
    msgs.add(msg);
    MessageQueue mq = new MessageQueue();
    mq.setBrokerName(brokerName);
    mq.setTopic(msg.getTopic());
    mq.setQueueId(msg.getQueueId());

    ConsumeOrderlyContext context = new ConsumeOrderlyContext(mq);

    final long beginTime = System.currentTimeMillis();

    log.info("consumeMessageDirectly receive new messge: {}", msg);

    try {
        ConsumeOrderlyStatus status = this.messageListener.consumeMessage(msgs, context);
        if (status != null) {
            switch (status) {
            case COMMIT:
                result.setConsumeResult(CMResult.CR_COMMIT);
                break;
            case ROLLBACK:
                result.setConsumeResult(CMResult.CR_ROLLBACK);
                break;
            case SUCCESS:
                result.setConsumeResult(CMResult.CR_SUCCESS);
                break;
            case SUSPEND_CURRENT_QUEUE_A_MOMENT:
                result.setConsumeResult(CMResult.CR_LATER);
                break;
            default:
                break;
            }
        }
        else {
            result.setConsumeResult(CMResult.CR_RETURN_NULL);
        }
    }
    catch (Throwable e) {
        result.setConsumeResult(CMResult.CR_THROW_EXCEPTION);
        result.setRemark(RemotingHelper.exceptionSimpleDesc(e));

        log.warn(String.format("consumeMessageDirectly exception: %s Group: %s Msgs: %s MQ: %s",//
            RemotingHelper.exceptionSimpleDesc(e),//
            ConsumeMessageOrderlyService.this.consumerGroup,//
            msgs,//
            mq), e);
    }

    result.setAutoCommit(context.isAutoCommit());
    result.setSpentTimeMills(System.currentTimeMillis() - beginTime);

    log.info("consumeMessageDirectly Result: {}", result);

    return result;
}
 
Example #7
Source File: ConsumeMessageOrderlyService.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
@Override
public ConsumeMessageDirectlyResult consumeMessageDirectly(MessageExt msg, String brokerName) {
    ConsumeMessageDirectlyResult result = new ConsumeMessageDirectlyResult();
    result.setOrder(true);

    List<MessageExt> msgs = new ArrayList<MessageExt>();
    msgs.add(msg);
    MessageQueue mq = new MessageQueue();
    mq.setBrokerName(brokerName);
    mq.setTopic(msg.getTopic());
    mq.setQueueId(msg.getQueueId());

    ConsumeOrderlyContext context = new ConsumeOrderlyContext(mq);

    final long beginTime = System.currentTimeMillis();

    log.info("consumeMessageDirectly receive new messge: {}", msg);

    try {
        ConsumeOrderlyStatus status = this.messageListener.consumeMessage(msgs, context);
        if (status != null) {
            switch (status) {
                case COMMIT:
                    result.setConsumeResult(CMResult.CR_COMMIT);
                    break;
                case ROLLBACK:
                    result.setConsumeResult(CMResult.CR_ROLLBACK);
                    break;
                case SUCCESS:
                    result.setConsumeResult(CMResult.CR_SUCCESS);
                    break;
                case SUSPEND_CURRENT_QUEUE_A_MOMENT:
                    result.setConsumeResult(CMResult.CR_LATER);
                    break;
                default:
                    break;
            }
        } else {
            result.setConsumeResult(CMResult.CR_RETURN_NULL);
        }
    } catch (Throwable e) {
        result.setConsumeResult(CMResult.CR_THROW_EXCEPTION);
        result.setRemark(RemotingHelper.exceptionSimpleDesc(e));

        log.warn(String.format("consumeMessageDirectly exception: %s Group: %s Msgs: %s MQ: %s", //
                RemotingHelper.exceptionSimpleDesc(e), //
                ConsumeMessageOrderlyService.this.consumerGroup, //
                msgs, //
                mq), e);
    }

    result.setAutoCommit(context.isAutoCommit());
    result.setSpentTimeMills(System.currentTimeMillis() - beginTime);

    log.info("consumeMessageDirectly Result: {}", result);

    return result;
}
 
Example #8
Source File: ConsumeMessageOrderlyService.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
public boolean processConsumeResult(//
                                    final List<MessageExt> msgs, //
                                    final ConsumeOrderlyStatus status, //
                                    final ConsumeOrderlyContext context, //
                                    final ConsumeRequest consumeRequest//
) {
    boolean continueConsume = true;
    long commitOffset = -1L;
    if (context.isAutoCommit()) {
        switch (status) {
            case COMMIT:
            case ROLLBACK:
                log.warn("the message queue consume result is illegal, we think you want to ack these message {}",
                        consumeRequest.getMessageQueue());
            case SUCCESS:
                commitOffset = consumeRequest.getProcessQueue().commit();
                this.getConsumerStatsManager().incConsumeOKTPS(consumerGroup, consumeRequest.getMessageQueue().getTopic(), msgs.size());
                break;
            case SUSPEND_CURRENT_QUEUE_A_MOMENT:
                this.getConsumerStatsManager().incConsumeFailedTPS(consumerGroup, consumeRequest.getMessageQueue().getTopic(), msgs.size());
                if (checkReconsumeTimes(msgs)) {
                    consumeRequest.getProcessQueue().makeMessageToCosumeAgain(msgs);
                    this.submitConsumeRequestLater(//
                            consumeRequest.getProcessQueue(), //
                            consumeRequest.getMessageQueue(), //
                            context.getSuspendCurrentQueueTimeMillis());
                    continueConsume = false;
                } else {
                    commitOffset = consumeRequest.getProcessQueue().commit();
                }
                break;
            default:
                break;
        }
    } else {
        switch (status) {
            case SUCCESS:
                this.getConsumerStatsManager().incConsumeOKTPS(consumerGroup, consumeRequest.getMessageQueue().getTopic(), msgs.size());
                break;
            case COMMIT:
                commitOffset = consumeRequest.getProcessQueue().commit();
                break;
            case ROLLBACK:
                consumeRequest.getProcessQueue().rollback();
                this.submitConsumeRequestLater(//
                        consumeRequest.getProcessQueue(), //
                        consumeRequest.getMessageQueue(), //
                        context.getSuspendCurrentQueueTimeMillis());
                continueConsume = false;
                break;
            case SUSPEND_CURRENT_QUEUE_A_MOMENT:
                this.getConsumerStatsManager().incConsumeFailedTPS(consumerGroup, consumeRequest.getMessageQueue().getTopic(), msgs.size());
                if (checkReconsumeTimes(msgs)) {
                    consumeRequest.getProcessQueue().makeMessageToCosumeAgain(msgs);
                    this.submitConsumeRequestLater(//
                            consumeRequest.getProcessQueue(), //
                            consumeRequest.getMessageQueue(), //
                            context.getSuspendCurrentQueueTimeMillis());
                    continueConsume = false;
                }
                break;
            default:
                break;
        }
    }

    if (commitOffset >= 0 && !consumeRequest.getProcessQueue().isDropped()) {
        this.defaultMQPushConsumerImpl.getOffsetStore().updateOffset(consumeRequest.getMessageQueue(), commitOffset, false);
    }

    return continueConsume;
}
 
Example #9
Source File: ConsumeMessageOrderlyService.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
public boolean processConsumeResult(//
        final List<MessageExt> msgs, //
        final ConsumeOrderlyStatus status, //
        final ConsumeOrderlyContext context, //
        final ConsumeRequest consumeRequest//
) {
    boolean continueConsume = true;
    long commitOffset = -1L;
    if (context.isAutoCommit()) {
        switch (status) {
        case COMMIT:
        case ROLLBACK:
            log.warn(
                "the message queue consume result is illegal, we think you want to ack these message {}",
                consumeRequest.getMessageQueue());
        case SUCCESS:
            commitOffset = consumeRequest.getProcessQueue().commit();
            this.getConsumerStatsManager().incConsumeOKTPS(consumerGroup,
                consumeRequest.getMessageQueue().getTopic(), msgs.size());
            break;
        case SUSPEND_CURRENT_QUEUE_A_MOMENT:
            consumeRequest.getProcessQueue().makeMessageToCosumeAgain(msgs);
            this.submitConsumeRequestLater(//
                consumeRequest.getProcessQueue(), //
                consumeRequest.getMessageQueue(), //
                context.getSuspendCurrentQueueTimeMillis());
            continueConsume = false;

            this.getConsumerStatsManager().incConsumeFailedTPS(consumerGroup,
                consumeRequest.getMessageQueue().getTopic(), msgs.size());
            break;
        default:
            break;
        }
    }
    else {
        switch (status) {
        case SUCCESS:
            this.getConsumerStatsManager().incConsumeOKTPS(consumerGroup,
                consumeRequest.getMessageQueue().getTopic(), msgs.size());
            break;
        case COMMIT:
            commitOffset = consumeRequest.getProcessQueue().commit();
            break;
        case ROLLBACK:
            consumeRequest.getProcessQueue().rollback();
            this.submitConsumeRequestLater(//
                consumeRequest.getProcessQueue(), //
                consumeRequest.getMessageQueue(), //
                context.getSuspendCurrentQueueTimeMillis());
            continueConsume = false;
            break;
        case SUSPEND_CURRENT_QUEUE_A_MOMENT:
            consumeRequest.getProcessQueue().makeMessageToCosumeAgain(msgs);
            this.submitConsumeRequestLater(//
                consumeRequest.getProcessQueue(), //
                consumeRequest.getMessageQueue(), //
                context.getSuspendCurrentQueueTimeMillis());
            continueConsume = false;
            this.getConsumerStatsManager().incConsumeFailedTPS(consumerGroup,
                consumeRequest.getMessageQueue().getTopic(), msgs.size());
            break;
        default:
            break;
        }
    }

    if (commitOffset >= 0) {
        this.defaultMQPushConsumerImpl.getOffsetStore().updateOffset(consumeRequest.getMessageQueue(),
            commitOffset, false);
    }

    return continueConsume;
}
 
Example #10
Source File: ConsumeMessageOrderlyService.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
@Override
public ConsumeMessageDirectlyResult consumeMessageDirectly(MessageExt msg, String brokerName) {
    ConsumeMessageDirectlyResult result = new ConsumeMessageDirectlyResult();
    result.setOrder(true);

    List<MessageExt> msgs = new ArrayList<MessageExt>();
    msgs.add(msg);
    MessageQueue mq = new MessageQueue();
    mq.setBrokerName(brokerName);
    mq.setTopic(msg.getTopic());
    mq.setQueueId(msg.getQueueId());

    ConsumeOrderlyContext context = new ConsumeOrderlyContext(mq);

    final long beginTime = System.currentTimeMillis();

    log.info("consumeMessageDirectly receive new messge: {}", msg);

    try {
        ConsumeOrderlyStatus status = this.messageListener.consumeMessage(msgs, context);
        if (status != null) {
            switch (status) {
            case COMMIT:
                result.setConsumeResult(CMResult.CR_COMMIT);
                break;
            case ROLLBACK:
                result.setConsumeResult(CMResult.CR_ROLLBACK);
                break;
            case SUCCESS:
                result.setConsumeResult(CMResult.CR_SUCCESS);
                break;
            case SUSPEND_CURRENT_QUEUE_A_MOMENT:
                result.setConsumeResult(CMResult.CR_LATER);
                break;
            default:
                break;
            }
        }
        else {
            result.setConsumeResult(CMResult.CR_RETURN_NULL);
        }
    }
    catch (Throwable e) {
        result.setConsumeResult(CMResult.CR_THROW_EXCEPTION);
        result.setRemark(RemotingHelper.exceptionSimpleDesc(e));

        log.warn(String.format("consumeMessageDirectly exception: %s Group: %s Msgs: %s MQ: %s", //
            RemotingHelper.exceptionSimpleDesc(e), //
            ConsumeMessageOrderlyService.this.consumerGroup, //
            msgs, //
            mq), e);
    }

    result.setAutoCommit(context.isAutoCommit());
    result.setSpentTimeMills(System.currentTimeMillis() - beginTime);

    log.info("consumeMessageDirectly Result: {}", result);

    return result;
}
 
Example #11
Source File: Consumer.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws MQClientException {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_3");
    /**
     * 设置Consumer第一次启动是从队列头部开始消费还是队列尾部开始消费<br>
     * 如果非第一次启动,那么按照上次消费的位置继续消费
     */
    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.println(Thread.currentThread().getName() + " Receive New Messages: " + 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.println("Consumer Started.");
}