Search in sources :

Example 1 with ViewMessageRequestHeader

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

the class QueryMessageProcessor method viewMessageById.

public RemotingCommand viewMessageById(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    final ViewMessageRequestHeader requestHeader = (ViewMessageRequestHeader) request.decodeCommandCustomHeader(ViewMessageRequestHeader.class);
    response.setOpaque(request.getOpaque());
    final SelectMappedBufferResult selectMappedBufferResult = this.brokerController.getMessageStore().selectOneMessageByOffset(requestHeader.getOffset());
    if (selectMappedBufferResult != null) {
        response.setCode(ResponseCode.SUCCESS);
        response.setRemark(null);
        try {
            FileRegion fileRegion = new OneMessageTransfer(response.encodeHeader(selectMappedBufferResult.getSize()), selectMappedBufferResult);
            ctx.channel().writeAndFlush(fileRegion).addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    selectMappedBufferResult.release();
                    if (!future.isSuccess()) {
                        log.error("Transfer one message from page cache failed, ", future.cause());
                    }
                }
            });
        } catch (Throwable e) {
            log.error("", e);
            selectMappedBufferResult.release();
        }
        return null;
    } else {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark("can not find message by the offset, " + requestHeader.getOffset());
    }
    return response;
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) ViewMessageRequestHeader(org.apache.rocketmq.common.protocol.header.ViewMessageRequestHeader) ChannelFuture(io.netty.channel.ChannelFuture) OneMessageTransfer(org.apache.rocketmq.broker.pagecache.OneMessageTransfer) FileRegion(io.netty.channel.FileRegion) SelectMappedBufferResult(org.apache.rocketmq.store.SelectMappedBufferResult) ChannelFutureListener(io.netty.channel.ChannelFutureListener) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException)

Example 2 with ViewMessageRequestHeader

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

the class MQClientAPIImpl method viewMessage.

public MessageExt viewMessage(final String addr, final long phyoffset, final long timeoutMillis) throws RemotingException, MQBrokerException, InterruptedException {
    ViewMessageRequestHeader requestHeader = new ViewMessageRequestHeader();
    requestHeader.setOffset(phyoffset);
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.VIEW_MESSAGE_BY_ID, requestHeader);
    RemotingCommand response = this.remotingClient.invokeSync(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr), request, timeoutMillis);
    assert response != null;
    switch(response.getCode()) {
        case ResponseCode.SUCCESS:
            {
                ByteBuffer byteBuffer = ByteBuffer.wrap(response.getBody());
                MessageExt messageExt = MessageDecoder.clientDecode(byteBuffer, true);
                return messageExt;
            }
        default:
            break;
    }
    throw new MQBrokerException(response.getCode(), response.getRemark());
}
Also used : ViewMessageRequestHeader(org.apache.rocketmq.common.protocol.header.ViewMessageRequestHeader) RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) MessageExt(org.apache.rocketmq.common.message.MessageExt) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) ByteBuffer(java.nio.ByteBuffer)

Example 3 with ViewMessageRequestHeader

use of org.apache.rocketmq.common.protocol.header.ViewMessageRequestHeader in project rocketmq by apache.

the class MQClientAPIImpl method viewMessage.

public MessageExt viewMessage(final String addr, final long phyoffset, final long timeoutMillis) throws RemotingException, MQBrokerException, InterruptedException {
    ViewMessageRequestHeader requestHeader = new ViewMessageRequestHeader();
    requestHeader.setOffset(phyoffset);
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.VIEW_MESSAGE_BY_ID, requestHeader);
    RemotingCommand response = this.remotingClient.invokeSync(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr), request, timeoutMillis);
    assert response != null;
    switch(response.getCode()) {
        case ResponseCode.SUCCESS:
            {
                ByteBuffer byteBuffer = ByteBuffer.wrap(response.getBody());
                MessageExt messageExt = MessageDecoder.clientDecode(byteBuffer, true);
                return messageExt;
            }
        default:
            break;
    }
    throw new MQBrokerException(response.getCode(), response.getRemark());
}
Also used : ViewMessageRequestHeader(org.apache.rocketmq.common.protocol.header.ViewMessageRequestHeader) RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) MessageExt(org.apache.rocketmq.common.message.MessageExt) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) ByteBuffer(java.nio.ByteBuffer)

Example 4 with ViewMessageRequestHeader

use of org.apache.rocketmq.common.protocol.header.ViewMessageRequestHeader in project rocketmq by apache.

the class QueryMessageProcessor method viewMessageById.

public RemotingCommand viewMessageById(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    final ViewMessageRequestHeader requestHeader = (ViewMessageRequestHeader) request.decodeCommandCustomHeader(ViewMessageRequestHeader.class);
    response.setOpaque(request.getOpaque());
    final SelectMappedBufferResult selectMappedBufferResult = this.brokerController.getMessageStore().selectOneMessageByOffset(requestHeader.getOffset());
    if (selectMappedBufferResult != null) {
        response.setCode(ResponseCode.SUCCESS);
        response.setRemark(null);
        try {
            FileRegion fileRegion = new OneMessageTransfer(response.encodeHeader(selectMappedBufferResult.getSize()), selectMappedBufferResult);
            ctx.channel().writeAndFlush(fileRegion).addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    selectMappedBufferResult.release();
                    if (!future.isSuccess()) {
                        log.error("Transfer one message from page cache failed, ", future.cause());
                    }
                }
            });
        } catch (Throwable e) {
            log.error("", e);
            selectMappedBufferResult.release();
        }
        return null;
    } else {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark("can not find message by the offset, " + requestHeader.getOffset());
    }
    return response;
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) ViewMessageRequestHeader(org.apache.rocketmq.common.protocol.header.ViewMessageRequestHeader) ChannelFuture(io.netty.channel.ChannelFuture) OneMessageTransfer(org.apache.rocketmq.broker.pagecache.OneMessageTransfer) FileRegion(io.netty.channel.FileRegion) SelectMappedBufferResult(org.apache.rocketmq.store.SelectMappedBufferResult) ChannelFutureListener(io.netty.channel.ChannelFutureListener) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException)

Aggregations

ViewMessageRequestHeader (org.apache.rocketmq.common.protocol.header.ViewMessageRequestHeader)4 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)4 ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelFutureListener (io.netty.channel.ChannelFutureListener)2 FileRegion (io.netty.channel.FileRegion)2 ByteBuffer (java.nio.ByteBuffer)2 OneMessageTransfer (org.apache.rocketmq.broker.pagecache.OneMessageTransfer)2 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)2 MessageExt (org.apache.rocketmq.common.message.MessageExt)2 RemotingCommandException (org.apache.rocketmq.remoting.exception.RemotingCommandException)2 SelectMappedBufferResult (org.apache.rocketmq.store.SelectMappedBufferResult)2