Search in sources :

Example 1 with FilterContext

use of org.apache.rocketmq.common.filter.FilterContext 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 2 with FilterContext

use of org.apache.rocketmq.common.filter.FilterContext in project rocketmq by apache.

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)

Aggregations

ArrayList (java.util.ArrayList)2 DefaultMQPullConsumer (org.apache.rocketmq.client.consumer.DefaultMQPullConsumer)2 PullCallback (org.apache.rocketmq.client.consumer.PullCallback)2 PullResult (org.apache.rocketmq.client.consumer.PullResult)2 FilterContext (org.apache.rocketmq.common.filter.FilterContext)2 MessageExt (org.apache.rocketmq.common.message.MessageExt)2 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)2 PullMessageRequestHeader (org.apache.rocketmq.common.protocol.header.PullMessageRequestHeader)2 PullMessageResponseHeader (org.apache.rocketmq.common.protocol.header.PullMessageResponseHeader)2 FilterClassInfo (org.apache.rocketmq.filtersrv.filter.FilterClassInfo)2 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)2