Search in sources :

Example 1 with QueryMessageResult

use of org.apache.rocketmq.store.QueryMessageResult in project rocketmq by apache.

the class QueryMessageProcessor method queryMessage.

public RemotingCommand queryMessage(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(QueryMessageResponseHeader.class);
    final QueryMessageResponseHeader responseHeader = (QueryMessageResponseHeader) response.readCustomHeader();
    final QueryMessageRequestHeader requestHeader = (QueryMessageRequestHeader) request.decodeCommandCustomHeader(QueryMessageRequestHeader.class);
    response.setOpaque(request.getOpaque());
    String isUniqueKey = request.getExtFields().get(MixAll.UNIQUE_MSG_QUERY_FLAG);
    if (isUniqueKey != null && isUniqueKey.equals("true")) {
        requestHeader.setMaxNum(this.brokerController.getMessageStoreConfig().getDefaultQueryMaxNum());
    }
    final QueryMessageResult queryMessageResult = this.brokerController.getMessageStore().queryMessage(requestHeader.getTopic(), requestHeader.getKey(), requestHeader.getMaxNum(), requestHeader.getBeginTimestamp(), requestHeader.getEndTimestamp());
    assert queryMessageResult != null;
    responseHeader.setIndexLastUpdatePhyoffset(queryMessageResult.getIndexLastUpdatePhyoffset());
    responseHeader.setIndexLastUpdateTimestamp(queryMessageResult.getIndexLastUpdateTimestamp());
    if (queryMessageResult.getBufferTotalSize() > 0) {
        response.setCode(ResponseCode.SUCCESS);
        response.setRemark(null);
        try {
            FileRegion fileRegion = new QueryMessageTransfer(response.encodeHeader(queryMessageResult.getBufferTotalSize()), queryMessageResult);
            ctx.channel().writeAndFlush(fileRegion).addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    queryMessageResult.release();
                    if (!future.isSuccess()) {
                        log.error("transfer query message by page cache failed, ", future.cause());
                    }
                }
            });
        } catch (Throwable e) {
            log.error("", e);
            queryMessageResult.release();
        }
        return null;
    }
    response.setCode(ResponseCode.QUERY_NOT_FOUND);
    response.setRemark("can not find message, maybe time range not correct");
    return response;
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) ChannelFuture(io.netty.channel.ChannelFuture) QueryMessageTransfer(org.apache.rocketmq.broker.pagecache.QueryMessageTransfer) QueryMessageResponseHeader(org.apache.rocketmq.common.protocol.header.QueryMessageResponseHeader) QueryMessageRequestHeader(org.apache.rocketmq.common.protocol.header.QueryMessageRequestHeader) QueryMessageResult(org.apache.rocketmq.store.QueryMessageResult) FileRegion(io.netty.channel.FileRegion) ChannelFutureListener(io.netty.channel.ChannelFutureListener) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException)

Example 2 with QueryMessageResult

use of org.apache.rocketmq.store.QueryMessageResult in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class QueryMessageProcessor method queryMessage.

// 查询消息
public RemotingCommand queryMessage(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(QueryMessageResponseHeader.class);
    final QueryMessageResponseHeader responseHeader = (QueryMessageResponseHeader) response.readCustomHeader();
    final QueryMessageRequestHeader requestHeader = (QueryMessageRequestHeader) request.decodeCommandCustomHeader(QueryMessageRequestHeader.class);
    response.setOpaque(request.getOpaque());
    String isUniqueKey = request.getExtFields().get(MixAll.UNIQUE_MSG_QUERY_FLAG);
    if (isUniqueKey != null && isUniqueKey.equals("true")) {
        requestHeader.setMaxNum(this.brokerController.getMessageStoreConfig().getDefaultQueryMaxNum());
    }
    final QueryMessageResult queryMessageResult = this.brokerController.getMessageStore().queryMessage(requestHeader.getTopic(), requestHeader.getKey(), requestHeader.getMaxNum(), requestHeader.getBeginTimestamp(), requestHeader.getEndTimestamp());
    assert queryMessageResult != null;
    responseHeader.setIndexLastUpdatePhyoffset(queryMessageResult.getIndexLastUpdatePhyoffset());
    responseHeader.setIndexLastUpdateTimestamp(queryMessageResult.getIndexLastUpdateTimestamp());
    // 说明找到消息
    if (queryMessageResult.getBufferTotalSize() > 0) {
        response.setCode(ResponseCode.SUCCESS);
        response.setRemark(null);
        try {
            FileRegion fileRegion = new QueryMessageTransfer(response.encodeHeader(queryMessageResult.getBufferTotalSize()), queryMessageResult);
            ctx.channel().writeAndFlush(fileRegion).addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    queryMessageResult.release();
                    if (!future.isSuccess()) {
                        log.error("transfer query message by page cache failed, ", future.cause());
                    }
                }
            });
        } catch (Throwable e) {
            log.error("", e);
            queryMessageResult.release();
        }
        return null;
    }
    response.setCode(ResponseCode.QUERY_NOT_FOUND);
    response.setRemark("can not find message, maybe time range not correct");
    return response;
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) ChannelFuture(io.netty.channel.ChannelFuture) QueryMessageTransfer(org.apache.rocketmq.broker.pagecache.QueryMessageTransfer) QueryMessageResponseHeader(org.apache.rocketmq.common.protocol.header.QueryMessageResponseHeader) QueryMessageRequestHeader(org.apache.rocketmq.common.protocol.header.QueryMessageRequestHeader) QueryMessageResult(org.apache.rocketmq.store.QueryMessageResult) FileRegion(io.netty.channel.FileRegion) ChannelFutureListener(io.netty.channel.ChannelFutureListener) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException)

Aggregations

ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelFutureListener (io.netty.channel.ChannelFutureListener)2 FileRegion (io.netty.channel.FileRegion)2 QueryMessageTransfer (org.apache.rocketmq.broker.pagecache.QueryMessageTransfer)2 QueryMessageRequestHeader (org.apache.rocketmq.common.protocol.header.QueryMessageRequestHeader)2 QueryMessageResponseHeader (org.apache.rocketmq.common.protocol.header.QueryMessageResponseHeader)2 RemotingCommandException (org.apache.rocketmq.remoting.exception.RemotingCommandException)2 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)2 QueryMessageResult (org.apache.rocketmq.store.QueryMessageResult)2