Search in sources :

Example 1 with ConsumeMessageDirectlyResult

use of org.apache.rocketmq.common.protocol.body.ConsumeMessageDirectlyResult in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class MQClientInstance method consumeMessageDirectly.

public // 
ConsumeMessageDirectlyResult consumeMessageDirectly(// 
final MessageExt msg, // 
final String consumerGroup, final String brokerName) {
    MQConsumerInner mqConsumerInner = this.consumerTable.get(consumerGroup);
    if (null != mqConsumerInner) {
        DefaultMQPushConsumerImpl consumer = (DefaultMQPushConsumerImpl) mqConsumerInner;
        ConsumeMessageDirectlyResult result = consumer.getConsumeMessageService().consumeMessageDirectly(msg, brokerName);
        return result;
    }
    return null;
}
Also used : MQConsumerInner(org.apache.rocketmq.client.impl.consumer.MQConsumerInner) DefaultMQPushConsumerImpl(org.apache.rocketmq.client.impl.consumer.DefaultMQPushConsumerImpl) ConsumeMessageDirectlyResult(org.apache.rocketmq.common.protocol.body.ConsumeMessageDirectlyResult)

Example 2 with ConsumeMessageDirectlyResult

use of org.apache.rocketmq.common.protocol.body.ConsumeMessageDirectlyResult in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class MQClientAPIImpl method consumeMessageDirectly.

public // 
ConsumeMessageDirectlyResult consumeMessageDirectly(// 
final String addr, // 
String consumerGroup, // 
String clientId, // 
String msgId, final long timeoutMillis) throws RemotingException, MQClientException, InterruptedException {
    ConsumeMessageDirectlyResultRequestHeader requestHeader = new ConsumeMessageDirectlyResultRequestHeader();
    requestHeader.setConsumerGroup(consumerGroup);
    requestHeader.setClientId(clientId);
    requestHeader.setMsgId(msgId);
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.CONSUME_MESSAGE_DIRECTLY, requestHeader);
    RemotingCommand response = this.remotingClient.invokeSync(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr), request, timeoutMillis);
    assert response != null;
    switch(response.getCode()) {
        case ResponseCode.SUCCESS:
            {
                byte[] body = response.getBody();
                if (body != null) {
                    ConsumeMessageDirectlyResult info = ConsumeMessageDirectlyResult.decode(body, ConsumeMessageDirectlyResult.class);
                    return info;
                }
            }
        default:
            break;
    }
    throw new MQClientException(response.getCode(), response.getRemark());
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) ConsumeMessageDirectlyResult(org.apache.rocketmq.common.protocol.body.ConsumeMessageDirectlyResult) ConsumeMessageDirectlyResultRequestHeader(org.apache.rocketmq.common.protocol.header.ConsumeMessageDirectlyResultRequestHeader) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 3 with ConsumeMessageDirectlyResult

use of org.apache.rocketmq.common.protocol.body.ConsumeMessageDirectlyResult in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class ConsumeMessageConcurrentlyService method consumeMessageDirectly.

@Override
public ConsumeMessageDirectlyResult consumeMessageDirectly(MessageExt msg, String brokerName) {
    ConsumeMessageDirectlyResult result = new ConsumeMessageDirectlyResult();
    result.setOrder(false);
    result.setAutoCommit(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());
    ConsumeConcurrentlyContext context = new ConsumeConcurrentlyContext(mq);
    this.resetRetryTopic(msgs);
    final long beginTime = System.currentTimeMillis();
    log.info("consumeMessageDirectly receive new messge: {}", msg);
    try {
        ConsumeConcurrentlyStatus status = this.messageListener.consumeMessage(msgs, context);
        if (status != null) {
            switch(status) {
                case CONSUME_SUCCESS:
                    result.setConsumeResult(CMResult.CR_SUCCESS);
                    break;
                case RECONSUME_LATER:
                    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), // 
        ConsumeMessageConcurrentlyService.this.consumerGroup, // 
        msgs, mq), e);
    }
    result.setSpentTimeMills(System.currentTimeMillis() - beginTime);
    log.info("consumeMessageDirectly Result: {}", result);
    return result;
}
Also used : ConsumeConcurrentlyContext(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext) MessageExt(org.apache.rocketmq.common.message.MessageExt) ConsumeMessageDirectlyResult(org.apache.rocketmq.common.protocol.body.ConsumeMessageDirectlyResult) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) ArrayList(java.util.ArrayList) ConsumeConcurrentlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus)

Example 4 with ConsumeMessageDirectlyResult

use of org.apache.rocketmq.common.protocol.body.ConsumeMessageDirectlyResult in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class ConsumeMessageOrderlyService method consumeMessageDirectly.

@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;
}
Also used : MessageExt(org.apache.rocketmq.common.message.MessageExt) ConsumeMessageDirectlyResult(org.apache.rocketmq.common.protocol.body.ConsumeMessageDirectlyResult) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) ConsumeOrderlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus) ArrayList(java.util.ArrayList) ConsumeOrderlyContext(org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext)

Example 5 with ConsumeMessageDirectlyResult

use of org.apache.rocketmq.common.protocol.body.ConsumeMessageDirectlyResult in project rocketmq-externals by apache.

the class MessageController method consumeMessageDirectly.

@RequestMapping(value = "/consumeMessageDirectly.do", method = RequestMethod.POST)
@ResponseBody
public Object consumeMessageDirectly(@RequestParam String topic, @RequestParam String consumerGroup, @RequestParam String msgId, @RequestParam(required = false) String clientId) {
    logger.info("msgId={} consumerGroup={} clientId={}", msgId, consumerGroup, clientId);
    ConsumeMessageDirectlyResult consumeMessageDirectlyResult = messageService.consumeMessageDirectly(topic, msgId, consumerGroup, clientId);
    logger.info("consumeMessageDirectlyResult={}", JsonUtil.obj2String(consumeMessageDirectlyResult));
    return consumeMessageDirectlyResult;
}
Also used : ConsumeMessageDirectlyResult(org.apache.rocketmq.common.protocol.body.ConsumeMessageDirectlyResult) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

ConsumeMessageDirectlyResult (org.apache.rocketmq.common.protocol.body.ConsumeMessageDirectlyResult)15 MQClientException (org.apache.rocketmq.client.exception.MQClientException)6 MessageExt (org.apache.rocketmq.common.message.MessageExt)6 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)4 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)4 ConsumeMessageDirectlyResultRequestHeader (org.apache.rocketmq.common.protocol.header.ConsumeMessageDirectlyResultRequestHeader)4 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)4 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)4 SubCommandException (org.apache.rocketmq.tools.command.SubCommandException)4 ConsumeConcurrentlyContext (org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext)2 ConsumeConcurrentlyStatus (org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus)2 ConsumeOrderlyContext (org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext)2 ConsumeOrderlyStatus (org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus)2 DefaultMQPushConsumerImpl (org.apache.rocketmq.client.impl.consumer.DefaultMQPushConsumerImpl)2 MQConsumerInner (org.apache.rocketmq.client.impl.consumer.MQConsumerInner)2 DefaultMQAdminExt (org.apache.rocketmq.tools.admin.DefaultMQAdminExt)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1