Search in sources :

Example 31 with PullResult

use of org.apache.rocketmq.client.consumer.PullResult in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class DefaultRequestProcessor method pullMessageForward.

private RemotingCommand pullMessageForward(final ChannelHandlerContext ctx, final RemotingCommand request) throws Exception {
    final RemotingCommand response = RemotingCommand.createResponseCommand(PullMessageResponseHeader.class);
    final PullMessageResponseHeader responseHeader = (PullMessageResponseHeader) response.readCustomHeader();
    final PullMessageRequestHeader requestHeader = (PullMessageRequestHeader) request.decodeCommandCustomHeader(PullMessageRequestHeader.class);
    final FilterContext filterContext = new FilterContext();
    filterContext.setConsumerGroup(requestHeader.getConsumerGroup());
    response.setOpaque(request.getOpaque());
    DefaultMQPullConsumer pullConsumer = this.filtersrvController.getDefaultMQPullConsumer();
    final FilterClassInfo findFilterClass = this.filtersrvController.getFilterClassManager().findFilterClass(requestHeader.getConsumerGroup(), requestHeader.getTopic());
    if (null == findFilterClass) {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark("Find Filter class failed, not registered");
        return response;
    }
    if (null == findFilterClass.getMessageFilter()) {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark("Find Filter class failed, registered but no class");
        return response;
    }
    responseHeader.setSuggestWhichBrokerId(MixAll.MASTER_ID);
    MessageQueue mq = new MessageQueue();
    mq.setTopic(requestHeader.getTopic());
    mq.setQueueId(requestHeader.getQueueId());
    mq.setBrokerName(this.filtersrvController.getBrokerName());
    long offset = requestHeader.getQueueOffset();
    int maxNums = requestHeader.getMaxMsgNums();
    final PullCallback pullCallback = new PullCallback() {

        @Override
        public void onSuccess(PullResult pullResult) {
            responseHeader.setMaxOffset(pullResult.getMaxOffset());
            responseHeader.setMinOffset(pullResult.getMinOffset());
            responseHeader.setNextBeginOffset(pullResult.getNextBeginOffset());
            response.setRemark(null);
            switch(pullResult.getPullStatus()) {
                case FOUND:
                    response.setCode(ResponseCode.SUCCESS);
                    List<MessageExt> msgListOK = new ArrayList<MessageExt>();
                    try {
                        for (MessageExt msg : pullResult.getMsgFoundList()) {
                            boolean match = findFilterClass.getMessageFilter().match(msg, filterContext);
                            if (match) {
                                msgListOK.add(msg);
                            }
                        }
                        if (!msgListOK.isEmpty()) {
                            returnResponse(requestHeader.getConsumerGroup(), requestHeader.getTopic(), ctx, response, msgListOK);
                            return;
                        } else {
                            response.setCode(ResponseCode.PULL_RETRY_IMMEDIATELY);
                        }
                    } catch (Throwable e) {
                        final String error = String.format("do Message Filter Exception, ConsumerGroup: %s Topic: %s ", requestHeader.getConsumerGroup(), requestHeader.getTopic());
                        log.error(error, e);
                        response.setCode(ResponseCode.SYSTEM_ERROR);
                        response.setRemark(error + RemotingHelper.exceptionSimpleDesc(e));
                        returnResponse(requestHeader.getConsumerGroup(), requestHeader.getTopic(), ctx, response, null);
                        return;
                    }
                    break;
                case NO_MATCHED_MSG:
                    response.setCode(ResponseCode.PULL_RETRY_IMMEDIATELY);
                    break;
                case NO_NEW_MSG:
                    response.setCode(ResponseCode.PULL_NOT_FOUND);
                    break;
                case OFFSET_ILLEGAL:
                    response.setCode(ResponseCode.PULL_OFFSET_MOVED);
                    break;
                default:
                    break;
            }
            returnResponse(requestHeader.getConsumerGroup(), requestHeader.getTopic(), ctx, response, null);
        }

        @Override
        public void onException(Throwable e) {
            response.setCode(ResponseCode.SYSTEM_ERROR);
            response.setRemark("Pull Callback Exception, " + RemotingHelper.exceptionSimpleDesc(e));
            returnResponse(requestHeader.getConsumerGroup(), requestHeader.getTopic(), ctx, response, null);
            return;
        }
    };
    pullConsumer.pullBlockIfNotFound(mq, null, offset, maxNums, pullCallback);
    return null;
}
Also used : ArrayList(java.util.ArrayList) FilterClassInfo(org.apache.rocketmq.filtersrv.filter.FilterClassInfo) DefaultMQPullConsumer(org.apache.rocketmq.client.consumer.DefaultMQPullConsumer) PullResult(org.apache.rocketmq.client.consumer.PullResult) PullMessageResponseHeader(org.apache.rocketmq.common.protocol.header.PullMessageResponseHeader) RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) PullMessageRequestHeader(org.apache.rocketmq.common.protocol.header.PullMessageRequestHeader) PullCallback(org.apache.rocketmq.client.consumer.PullCallback) MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) FilterContext(org.apache.rocketmq.common.filter.FilterContext)

Example 32 with PullResult

use of org.apache.rocketmq.client.consumer.PullResult in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class MonitorService method computeUndoneMsgs.

private void computeUndoneMsgs(final UndoneMsgs undoneMsgs, final ConsumeStats consumeStats) {
    long total = 0;
    long singleMax = 0;
    long delayMax = 0;
    Iterator<Entry<MessageQueue, OffsetWrapper>> it = consumeStats.getOffsetTable().entrySet().iterator();
    while (it.hasNext()) {
        Entry<MessageQueue, OffsetWrapper> next = it.next();
        MessageQueue mq = next.getKey();
        OffsetWrapper ow = next.getValue();
        long diff = ow.getBrokerOffset() - ow.getConsumerOffset();
        if (diff > singleMax) {
            singleMax = diff;
        }
        if (diff > 0) {
            total += diff;
        }
        // Delay
        if (ow.getLastTimestamp() > 0) {
            try {
                long maxOffset = this.defaultMQPullConsumer.maxOffset(mq);
                if (maxOffset > 0) {
                    PullResult pull = this.defaultMQPullConsumer.pull(mq, "*", maxOffset - 1, 1);
                    switch(pull.getPullStatus()) {
                        case FOUND:
                            long delay = pull.getMsgFoundList().get(0).getStoreTimestamp() - ow.getLastTimestamp();
                            if (delay > delayMax) {
                                delayMax = delay;
                            }
                            break;
                        case NO_MATCHED_MSG:
                        case NO_NEW_MSG:
                        case OFFSET_ILLEGAL:
                            break;
                        default:
                            break;
                    }
                }
            } catch (Exception e) {
            }
        }
    }
    undoneMsgs.setUndoneMsgsTotal(total);
    undoneMsgs.setUndoneMsgsSingleMQ(singleMax);
    undoneMsgs.setUndoneMsgsDelayTimeMills(delayMax);
}
Also used : Entry(java.util.Map.Entry) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) OffsetWrapper(org.apache.rocketmq.common.admin.OffsetWrapper) PullResult(org.apache.rocketmq.client.consumer.PullResult) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException)

Example 33 with PullResult

use of org.apache.rocketmq.client.consumer.PullResult in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class QueryMsgByOffsetSubCommand method execute.

@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException {
    DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt(rpcHook);
    DefaultMQPullConsumer defaultMQPullConsumer = new DefaultMQPullConsumer(MixAll.TOOLS_CONSUMER_GROUP, rpcHook);
    defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis()));
    defaultMQPullConsumer.setInstanceName(Long.toString(System.currentTimeMillis()));
    try {
        String topic = commandLine.getOptionValue('t').trim();
        String brokerName = commandLine.getOptionValue('b').trim();
        String queueId = commandLine.getOptionValue('i').trim();
        String offset = commandLine.getOptionValue('o').trim();
        MessageQueue mq = new MessageQueue();
        mq.setTopic(topic);
        mq.setBrokerName(brokerName);
        mq.setQueueId(Integer.parseInt(queueId));
        defaultMQPullConsumer.start();
        defaultMQAdminExt.start();
        PullResult pullResult = defaultMQPullConsumer.pull(mq, "*", Long.parseLong(offset), 1);
        if (pullResult != null) {
            switch(pullResult.getPullStatus()) {
                case FOUND:
                    QueryMsgByIdSubCommand.printMsg(defaultMQAdminExt, pullResult.getMsgFoundList().get(0));
                    break;
                case NO_MATCHED_MSG:
                case NO_NEW_MSG:
                case OFFSET_ILLEGAL:
                default:
                    break;
            }
        }
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        defaultMQPullConsumer.shutdown();
        defaultMQAdminExt.shutdown();
    }
}
Also used : MessageQueue(org.apache.rocketmq.common.message.MessageQueue) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) DefaultMQAdminExt(org.apache.rocketmq.tools.admin.DefaultMQAdminExt) DefaultMQPullConsumer(org.apache.rocketmq.client.consumer.DefaultMQPullConsumer) PullResult(org.apache.rocketmq.client.consumer.PullResult) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException)

Example 34 with PullResult

use of org.apache.rocketmq.client.consumer.PullResult in project rocketmq by apache.

the class QueryMsgByOffsetSubCommand method execute.

@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException {
    DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt(rpcHook);
    DefaultMQPullConsumer defaultMQPullConsumer = new DefaultMQPullConsumer(MixAll.TOOLS_CONSUMER_GROUP, rpcHook);
    defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis()));
    defaultMQPullConsumer.setInstanceName(Long.toString(System.currentTimeMillis()));
    try {
        String topic = commandLine.getOptionValue('t').trim();
        String brokerName = commandLine.getOptionValue('b').trim();
        String queueId = commandLine.getOptionValue('i').trim();
        String offset = commandLine.getOptionValue('o').trim();
        MessageQueue mq = new MessageQueue();
        mq.setTopic(topic);
        mq.setBrokerName(brokerName);
        mq.setQueueId(Integer.parseInt(queueId));
        defaultMQPullConsumer.start();
        defaultMQAdminExt.start();
        PullResult pullResult = defaultMQPullConsumer.pull(mq, "*", Long.parseLong(offset), 1);
        if (pullResult != null) {
            switch(pullResult.getPullStatus()) {
                case FOUND:
                    QueryMsgByIdSubCommand.printMsg(defaultMQAdminExt, pullResult.getMsgFoundList().get(0));
                    break;
                case NO_MATCHED_MSG:
                case NO_NEW_MSG:
                case OFFSET_ILLEGAL:
                default:
                    break;
            }
        }
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        defaultMQPullConsumer.shutdown();
        defaultMQAdminExt.shutdown();
    }
}
Also used : MessageQueue(org.apache.rocketmq.common.message.MessageQueue) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) DefaultMQAdminExt(org.apache.rocketmq.tools.admin.DefaultMQAdminExt) DefaultMQPullConsumer(org.apache.rocketmq.client.consumer.DefaultMQPullConsumer) PullResult(org.apache.rocketmq.client.consumer.PullResult) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException)

Example 35 with PullResult

use of org.apache.rocketmq.client.consumer.PullResult in project rocketmq by apache.

the class PullConsumerTest method main.

public static void main(String[] args) throws MQClientException {
    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("please_rename_unique_group_name_5");
    consumer.start();
    try {
        MessageQueue mq = new MessageQueue();
        mq.setQueueId(0);
        mq.setTopic("TopicTest3");
        mq.setBrokerName("vivedeMacBook-Pro.local");
        long offset = 26;
        long beginTime = System.currentTimeMillis();
        PullResult pullResult = consumer.pullBlockIfNotFound(mq, null, offset, 32);
        System.out.printf("%s%n", System.currentTimeMillis() - beginTime);
        System.out.printf("%s%n", pullResult);
    } catch (Exception e) {
        e.printStackTrace();
    }
    consumer.shutdown();
}
Also used : MessageQueue(org.apache.rocketmq.common.message.MessageQueue) DefaultMQPullConsumer(org.apache.rocketmq.client.consumer.DefaultMQPullConsumer) PullResult(org.apache.rocketmq.client.consumer.PullResult) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Aggregations

PullResult (org.apache.rocketmq.client.consumer.PullResult)38 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)29 DefaultMQPullConsumer (org.apache.rocketmq.client.consumer.DefaultMQPullConsumer)19 MQClientException (org.apache.rocketmq.client.exception.MQClientException)19 MessageExt (org.apache.rocketmq.common.message.MessageExt)12 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)8 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)8 HashMap (java.util.HashMap)7 PullCallback (org.apache.rocketmq.client.consumer.PullCallback)6 SubscriptionData (org.apache.rocketmq.common.protocol.heartbeat.SubscriptionData)6 SubCommandException (org.apache.rocketmq.tools.command.SubCommandException)6 ArrayList (java.util.ArrayList)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 List (java.util.List)4 PullMessageRequestHeader (org.apache.rocketmq.common.protocol.header.PullMessageRequestHeader)4 Test (org.junit.Test)4 Date (java.util.Date)3 Event (org.apache.flume.Event)3 MQPullConsumer (org.apache.rocketmq.client.consumer.MQPullConsumer)3 PullTaskCallback (org.apache.rocketmq.client.consumer.PullTaskCallback)3