public RemotingCommand viewMessageById(ChannelHandlerContext ctx, RemotingCommand request)
throws RemotingCommandException {
final RemotingCommand response = RemotingCommand.createResponseCommand(null);
final ViewMessageRequestHeader requestHeader =
(ViewMessageRequestHeader) request.decodeCommandCustomHeader(ViewMessageRequestHeader.class);
// 由于使用sendfile,所以必须要设置
response.setOpaque(request.getOpaque());
final SelectMapedBufferResult selectMapedBufferResult =
this.brokerController.getMessageStore().selectOneMessageByOffset(requestHeader.getOffset());
if (selectMapedBufferResult != null) {
response.setCode(ResponseCode.SUCCESS_VALUE);
response.setRemark(null);
try {
FileRegion fileRegion =
new OneMessageTransfer(response.encodeHeader(selectMapedBufferResult.getSize()),
selectMapedBufferResult);
ctx.channel().writeAndFlush(fileRegion).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
selectMapedBufferResult.release();
if (!future.isSuccess()) {
log.error("transfer one message by pagecache failed, ", future.cause());
}
}
});
}
catch (Throwable e) {
log.error("", e);
selectMapedBufferResult.release();
}
return null;
}
else {
response.setCode(ResponseCode.SYSTEM_ERROR_VALUE);
response.setRemark("can not find message by the offset, " + requestHeader.getOffset());
}
return response;
}