Java Code Examples for org.apache.rocketmq.remoting.protocol.RemotingCommand#encodeHeader()

The following examples show how to use org.apache.rocketmq.remoting.protocol.RemotingCommand#encodeHeader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NettyEncoder.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
@Override
public void encode(ChannelHandlerContext ctx, RemotingCommand remotingCommand, ByteBuf out)
    throws Exception {
    try {
        ByteBuffer header = remotingCommand.encodeHeader();
        out.writeBytes(header);
        byte[] body = remotingCommand.getBody();
        if (body != null) {
            out.writeBytes(body);
        }
    } catch (Exception e) {
        log.error("encode exception, " + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), e);
        if (remotingCommand != null) {
            log.error(remotingCommand.toString());
        }
        RemotingUtil.closeChannel(ctx.channel());
    }
}
 
Example 2
Source File: Broker2Client.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public void checkProducerTransactionState(
    final Channel channel,
    final CheckTransactionStateRequestHeader requestHeader,
    final SelectMappedBufferResult selectMappedBufferResult) {
    RemotingCommand request =
        RemotingCommand.createRequestCommand(RequestCode.CHECK_TRANSACTION_STATE, requestHeader);
    request.markOnewayRPC();

    try {
        FileRegion fileRegion =
            new OneMessageTransfer(request.encodeHeader(selectMappedBufferResult.getSize()),
                selectMappedBufferResult);
        channel.writeAndFlush(fileRegion).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                selectMappedBufferResult.release();
                if (!future.isSuccess()) {
                    log.error("invokeProducer failed,", future.cause());
                }
            }
        });
    } catch (Throwable e) {
        log.error("invokeProducer exception", e);
        selectMappedBufferResult.release();
    }
}
 
Example 3
Source File: NettyEncoder.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
@Override
    public void encode(ChannelHandlerContext ctx, RemotingCommand remotingCommand, ByteBuf out)
        throws Exception {
        try {
//            对消息头进行编码,rocketmq只对消息头进行了编码
            ByteBuffer header = remotingCommand.encodeHeader();
//            往buf中写消息头
            out.writeBytes(header);
//            获取消息体
            byte[] body = remotingCommand.getBody();
            if (body != null) {
//                写消息体
                out.writeBytes(body);
            }
        } catch (Exception e) {
            log.error("encode exception, " + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), e);
            if (remotingCommand != null) {
                log.error(remotingCommand.toString());
            }
//            出现异常关闭channel
            RemotingUtil.closeChannel(ctx.channel());
        }
    }
 
Example 4
Source File: NettyEncoder.java    From rocketmq_trans_message with Apache License 2.0 6 votes vote down vote up
@Override
public void encode(ChannelHandlerContext ctx, RemotingCommand remotingCommand, ByteBuf out)
    throws Exception {
    try {
        ByteBuffer header = remotingCommand.encodeHeader();
        out.writeBytes(header);
        byte[] body = remotingCommand.getBody();
        if (body != null) {
            out.writeBytes(body);
        }
    } catch (Exception e) {
        log.error("encode exception, " + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), e);
        if (remotingCommand != null) {
            log.error(remotingCommand.toString());
        }
        RemotingUtil.closeChannel(ctx.channel());
    }
}
 
Example 5
Source File: Broker2Client.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public void checkProducerTransactionState(
    final Channel channel,
    final CheckTransactionStateRequestHeader requestHeader,
    final SelectMappedBufferResult selectMappedBufferResult) {
    RemotingCommand request =
        RemotingCommand.createRequestCommand(RequestCode.CHECK_TRANSACTION_STATE, requestHeader);
    request.markOnewayRPC();

    try {
        FileRegion fileRegion =
            new OneMessageTransfer(request.encodeHeader(selectMappedBufferResult.getSize()),
                selectMappedBufferResult);
        channel.writeAndFlush(fileRegion).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                selectMappedBufferResult.release();
                if (!future.isSuccess()) {
                    log.error("invokeProducer failed,", future.cause());
                }
            }
        });
    } catch (Throwable e) {
        log.error("invokeProducer exception", e);
        selectMappedBufferResult.release();
    }
}
 
Example 6
Source File: NettyEncoder.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
@Override
public void encode(ChannelHandlerContext ctx, RemotingCommand remotingCommand, ByteBuf out)
    throws Exception {
    try {
        ByteBuffer header = remotingCommand.encodeHeader();
        out.writeBytes(header);
        byte[] body = remotingCommand.getBody();
        if (body != null) {
            out.writeBytes(body);
        }
    } catch (Exception e) {
        log.error("encode exception, " + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), e);
        if (remotingCommand != null) {
            log.error(remotingCommand.toString());
        }
        RemotingUtil.closeChannel(ctx.channel());
    }
}
 
Example 7
Source File: PlainAccessValidatorTest.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
@Test(expected = AclException.class)
public void validateErrorSecretKeyTest() {
    SessionCredentials sessionCredentials=new SessionCredentials();
    sessionCredentials.setAccessKey("RocketMQ");
    sessionCredentials.setSecretKey("1234");
    AclClientRPCHook aclClientRPCHook=new AclClientRPCHook(sessionCredentials);
    SendMessageRequestHeader messageRequestHeader = new SendMessageRequestHeader();
    messageRequestHeader.setTopic("topicB");
    RemotingCommand remotingCommand = RemotingCommand.createRequestCommand(RequestCode.SEND_MESSAGE, messageRequestHeader);
    aclClientRPCHook.doBeforeRequest("", remotingCommand);

    ByteBuffer buf = remotingCommand.encodeHeader();
    buf.getInt();
    buf = ByteBuffer.allocate(buf.limit() - buf.position()).put(buf);
    buf.position(0);
    PlainAccessResource accessResource = (PlainAccessResource) plainAccessValidator.parse(RemotingCommand.decode(buf), "192.168.1.1");
    plainAccessValidator.validate(accessResource);
}
 
Example 8
Source File: NettyEncoder.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
@Override
public void encode(ChannelHandlerContext ctx, RemotingCommand remotingCommand, ByteBuf out)
    throws Exception {
    try {
        ByteBuffer header = remotingCommand.encodeHeader();
        out.writeBytes(header);
        byte[] body = remotingCommand.getBody();
        if (body != null) {
            out.writeBytes(body);
        }
    } catch (Exception e) {
        log.error("encode exception, " + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), e);
        if (remotingCommand != null) {
            log.error(remotingCommand.toString());
        }
        RemotingUtil.closeChannel(ctx.channel());
    }
}
 
Example 9
Source File: PlainAccessValidatorTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Test
public void validatePullMessageTest() {
    PullMessageRequestHeader pullMessageRequestHeader=new PullMessageRequestHeader();
    pullMessageRequestHeader.setTopic("topicC");
    pullMessageRequestHeader.setConsumerGroup("consumerGroupA");
    RemotingCommand remotingCommand = RemotingCommand.createRequestCommand(RequestCode.PULL_MESSAGE,pullMessageRequestHeader);
    aclClient.doBeforeRequest("", remotingCommand);
    ByteBuffer buf = remotingCommand.encodeHeader();
    buf.getInt();
    buf = ByteBuffer.allocate(buf.limit() - buf.position()).put(buf);
    buf.position(0);
    PlainAccessResource accessResource = (PlainAccessResource) plainAccessValidator.parse(RemotingCommand.decode(buf), "192.168.0.1:9876");
    plainAccessValidator.validate(accessResource);
}
 
Example 10
Source File: PlainAccessValidatorTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Test
public void validateSendMessageV2Test() {
    SendMessageRequestHeader messageRequestHeader = new SendMessageRequestHeader();
    messageRequestHeader.setTopic("topicC");
    RemotingCommand remotingCommand = RemotingCommand.createRequestCommand(RequestCode.SEND_MESSAGE_V2, SendMessageRequestHeaderV2.createSendMessageRequestHeaderV2(messageRequestHeader));
    aclClient.doBeforeRequest("", remotingCommand);

    ByteBuffer buf = remotingCommand.encodeHeader();
    buf.getInt();
    buf = ByteBuffer.allocate(buf.limit() - buf.position()).put(buf);
    buf.position(0);
    PlainAccessResource accessResource = (PlainAccessResource) plainAccessValidator.parse(RemotingCommand.decode(buf), "192.168.0.1:9876");
    plainAccessValidator.validate(accessResource);
}
 
Example 11
Source File: QueryMessageProcessor.java    From rocketmq_trans_message with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 12
Source File: PlainAccessValidatorTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Test
public void validateSendMessageTest() {
    SendMessageRequestHeader messageRequestHeader = new SendMessageRequestHeader();
    messageRequestHeader.setTopic("topicB");
    RemotingCommand remotingCommand = RemotingCommand.createRequestCommand(RequestCode.SEND_MESSAGE, messageRequestHeader);
    aclClient.doBeforeRequest("", remotingCommand);

    ByteBuffer buf = remotingCommand.encodeHeader();
    buf.getInt();
    buf = ByteBuffer.allocate(buf.limit() - buf.position()).put(buf);
    buf.position(0);
    PlainAccessResource accessResource = (PlainAccessResource) plainAccessValidator.parse(RemotingCommand.decode(buf), "192.168.0.1");
    plainAccessValidator.validate(accessResource);
}
 
Example 13
Source File: QueryMessageProcessor.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 14
Source File: PlainAccessValidatorTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Test
public void validateConsumeMessageBackTest() {
    ConsumerSendMsgBackRequestHeader consumerSendMsgBackRequestHeader=new ConsumerSendMsgBackRequestHeader();
    consumerSendMsgBackRequestHeader.setOriginTopic("topicC");
    consumerSendMsgBackRequestHeader.setGroup("consumerGroupA");
    RemotingCommand remotingCommand = RemotingCommand.createRequestCommand(RequestCode.CONSUMER_SEND_MSG_BACK,consumerSendMsgBackRequestHeader);
    aclClient.doBeforeRequest("", remotingCommand);
    ByteBuffer buf = remotingCommand.encodeHeader();
    buf.getInt();
    buf = ByteBuffer.allocate(buf.limit() - buf.position()).put(buf);
    buf.position(0);
    PlainAccessResource accessResource = (PlainAccessResource) plainAccessValidator.parse(RemotingCommand.decode(buf), "192.168.0.1:9876");
    plainAccessValidator.validate(accessResource);
}
 
Example 15
Source File: QueryMessageProcessor.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 16
Source File: PlainAccessValidatorTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Test
public void validateQueryMessageByKeyTest() {
    QueryMessageRequestHeader queryMessageRequestHeader=new QueryMessageRequestHeader();
    queryMessageRequestHeader.setTopic("topicC");
    RemotingCommand remotingCommand = RemotingCommand.createRequestCommand(RequestCode.QUERY_MESSAGE,queryMessageRequestHeader);
    aclClient.doBeforeRequest("", remotingCommand);
    remotingCommand.addExtField(MixAll.UNIQUE_MSG_QUERY_FLAG, "false");
    ByteBuffer buf = remotingCommand.encodeHeader();
    buf.getInt();
    buf = ByteBuffer.allocate(buf.limit() - buf.position()).put(buf);
    buf.position(0);
    PlainAccessResource accessResource = (PlainAccessResource) plainAccessValidator.parse(RemotingCommand.decode(buf), "192.168.1.1:9876");
    plainAccessValidator.validate(accessResource);
}
 
Example 17
Source File: PlainAccessValidatorTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Test
public void validateGetConsumerListByGroupTest() {
    GetConsumerListByGroupRequestHeader getConsumerListByGroupRequestHeader=new GetConsumerListByGroupRequestHeader();
    getConsumerListByGroupRequestHeader.setConsumerGroup("consumerGroupA");
    RemotingCommand remotingCommand = RemotingCommand.createRequestCommand(RequestCode.GET_CONSUMER_LIST_BY_GROUP,getConsumerListByGroupRequestHeader);
    aclClient.doBeforeRequest("", remotingCommand);
    ByteBuffer buf = remotingCommand.encodeHeader();
    buf.getInt();
    buf = ByteBuffer.allocate(buf.limit() - buf.position()).put(buf);
    buf.position(0);
    PlainAccessResource accessResource = (PlainAccessResource) plainAccessValidator.parse(RemotingCommand.decode(buf), "192.168.0.1:9876");
    plainAccessValidator.validate(accessResource);
}
 
Example 18
Source File: PlainAccessValidatorTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Test
public void validateUpdateConsumerOffSetTest() {
    UpdateConsumerOffsetRequestHeader updateConsumerOffsetRequestHeader=new UpdateConsumerOffsetRequestHeader();
    updateConsumerOffsetRequestHeader.setConsumerGroup("consumerGroupA");
    RemotingCommand remotingCommand = RemotingCommand.createRequestCommand(RequestCode.UPDATE_CONSUMER_OFFSET,updateConsumerOffsetRequestHeader);
    aclClient.doBeforeRequest("", remotingCommand);
    ByteBuffer buf = remotingCommand.encodeHeader();
    buf.getInt();
    buf = ByteBuffer.allocate(buf.limit() - buf.position()).put(buf);
    buf.position(0);
    PlainAccessResource accessResource = (PlainAccessResource) plainAccessValidator.parse(RemotingCommand.decode(buf), "192.168.0.1:9876");
    plainAccessValidator.validate(accessResource);
}
 
Example 19
Source File: PlainAccessValidatorTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Test
public void validateTest() {
    SendMessageRequestHeader messageRequestHeader = new SendMessageRequestHeader();
    messageRequestHeader.setTopic("topicB");
    RemotingCommand remotingCommand = RemotingCommand.createRequestCommand(RequestCode.SEND_MESSAGE, messageRequestHeader);
    aclClient.doBeforeRequest("", remotingCommand);

    ByteBuffer buf = remotingCommand.encodeHeader();
    buf.getInt();
    buf = ByteBuffer.allocate(buf.limit() - buf.position()).put(buf);
    buf.position(0);
    PlainAccessResource accessResource = (PlainAccessResource) plainAccessValidator.parse(RemotingCommand.decode(buf), "192.168.0.1");
    plainAccessValidator.validate(accessResource);

}
 
Example 20
Source File: QueryMessageProcessor.java    From rocketmq-read with Apache License 2.0 4 votes vote down vote up
/**
 * 检查消息根据id
 * @param ctx ctx
 * @param request request
 * @return ;
 * @throws RemotingCommandException ;
 */
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());

    /*
     * 根据offset 查询一个消息
     */
    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;
}