Search in sources :

Example 1 with CommandCustomHeader

use of org.apache.rocketmq.remoting.CommandCustomHeader in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class RemotingCommand method createResponseCommand.

// 构建
public static RemotingCommand createResponseCommand(int code, String remark, Class<? extends CommandCustomHeader> classHeader) {
    RemotingCommand cmd = new RemotingCommand();
    cmd.markResponseType();
    cmd.setCode(code);
    cmd.setRemark(remark);
    setCmdVersion(cmd);
    if (classHeader != null) {
        try {
            CommandCustomHeader objectHeader = classHeader.newInstance();
            cmd.customHeader = objectHeader;
        } catch (InstantiationException e) {
            return null;
        } catch (IllegalAccessException e) {
            return null;
        }
    }
    return cmd;
}
Also used : CommandCustomHeader(org.apache.rocketmq.remoting.CommandCustomHeader)

Example 2 with CommandCustomHeader

use of org.apache.rocketmq.remoting.CommandCustomHeader in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class ExtFieldsHeader method testEncodeAndDecode_FilledBodyWithExtFields.

@Test
public void testEncodeAndDecode_FilledBodyWithExtFields() throws RemotingCommandException {
    System.setProperty(RemotingCommand.REMOTING_VERSION_KEY, "2333");
    // org.apache.rocketmq.common.protocol.RequestCode.REGISTER_BROKER
    int code = 103;
    CommandCustomHeader header = new ExtFieldsHeader();
    RemotingCommand cmd = RemotingCommand.createRequestCommand(code, header);
    cmd.addExtField("key", "value");
    ByteBuffer buffer = cmd.encode();
    // Simulate buffer being read in NettyDecoder
    buffer.getInt();
    byte[] bytes = new byte[buffer.limit() - 4];
    buffer.get(bytes, 0, buffer.limit() - 4);
    buffer = ByteBuffer.wrap(bytes);
    RemotingCommand decodedCommand = RemotingCommand.decode(buffer);
    assertThat(decodedCommand.getExtFields().get("stringValue")).isEqualTo("bilibili");
    assertThat(decodedCommand.getExtFields().get("intValue")).isEqualTo("2333");
    assertThat(decodedCommand.getExtFields().get("longValue")).isEqualTo("23333333");
    assertThat(decodedCommand.getExtFields().get("booleanValue")).isEqualTo("true");
    assertThat(decodedCommand.getExtFields().get("doubleValue")).isEqualTo("0.618");
    assertThat(decodedCommand.getExtFields().get("key")).isEqualTo("value");
    CommandCustomHeader decodedHeader = decodedCommand.decodeCommandCustomHeader(ExtFieldsHeader.class);
    assertThat(((ExtFieldsHeader) decodedHeader).getStringValue()).isEqualTo("bilibili");
    assertThat(((ExtFieldsHeader) decodedHeader).getIntValue()).isEqualTo(2333);
    assertThat(((ExtFieldsHeader) decodedHeader).getLongValue()).isEqualTo(23333333l);
    assertThat(((ExtFieldsHeader) decodedHeader).isBooleanValue()).isEqualTo(true);
    assertThat(((ExtFieldsHeader) decodedHeader).getDoubleValue()).isBetween(0.617, 0.619);
}
Also used : CommandCustomHeader(org.apache.rocketmq.remoting.CommandCustomHeader) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 3 with CommandCustomHeader

use of org.apache.rocketmq.remoting.CommandCustomHeader in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class ExtFieldsHeader method testEncodeAndDecode_FilledBody.

@Test
public void testEncodeAndDecode_FilledBody() {
    System.setProperty(RemotingCommand.REMOTING_VERSION_KEY, "2333");
    // org.apache.rocketmq.common.protocol.RequestCode.REGISTER_BROKER
    int code = 103;
    CommandCustomHeader header = new SampleCommandCustomHeader();
    RemotingCommand cmd = RemotingCommand.createRequestCommand(code, header);
    cmd.setBody(new byte[] { 0, 1, 2, 3, 4 });
    ByteBuffer buffer = cmd.encode();
    // Simulate buffer being read in NettyDecoder
    buffer.getInt();
    byte[] bytes = new byte[buffer.limit() - 4];
    buffer.get(bytes, 0, buffer.limit() - 4);
    buffer = ByteBuffer.wrap(bytes);
    RemotingCommand decodedCommand = RemotingCommand.decode(buffer);
    assertThat(decodedCommand.getSerializeTypeCurrentRPC()).isEqualTo(SerializeType.JSON);
    assertThat(decodedCommand.getBody()).isEqualTo(new byte[] { 0, 1, 2, 3, 4 });
}
Also used : CommandCustomHeader(org.apache.rocketmq.remoting.CommandCustomHeader) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 4 with CommandCustomHeader

use of org.apache.rocketmq.remoting.CommandCustomHeader in project rocketmq by apache.

the class ExtFieldsHeader method testEncodeAndDecode_FilledBodyWithExtFields.

@Test
public void testEncodeAndDecode_FilledBodyWithExtFields() throws RemotingCommandException {
    System.setProperty(RemotingCommand.REMOTING_VERSION_KEY, "2333");
    // org.apache.rocketmq.common.protocol.RequestCode.REGISTER_BROKER
    int code = 103;
    CommandCustomHeader header = new ExtFieldsHeader();
    RemotingCommand cmd = RemotingCommand.createRequestCommand(code, header);
    cmd.addExtField("key", "value");
    ByteBuffer buffer = cmd.encode();
    // Simulate buffer being read in NettyDecoder
    buffer.getInt();
    byte[] bytes = new byte[buffer.limit() - 4];
    buffer.get(bytes, 0, buffer.limit() - 4);
    buffer = ByteBuffer.wrap(bytes);
    RemotingCommand decodedCommand = RemotingCommand.decode(buffer);
    assertThat(decodedCommand.getExtFields().get("stringValue")).isEqualTo("bilibili");
    assertThat(decodedCommand.getExtFields().get("intValue")).isEqualTo("2333");
    assertThat(decodedCommand.getExtFields().get("longValue")).isEqualTo("23333333");
    assertThat(decodedCommand.getExtFields().get("booleanValue")).isEqualTo("true");
    assertThat(decodedCommand.getExtFields().get("doubleValue")).isEqualTo("0.618");
    assertThat(decodedCommand.getExtFields().get("key")).isEqualTo("value");
    CommandCustomHeader decodedHeader = decodedCommand.decodeCommandCustomHeader(ExtFieldsHeader.class);
    assertThat(((ExtFieldsHeader) decodedHeader).getStringValue()).isEqualTo("bilibili");
    assertThat(((ExtFieldsHeader) decodedHeader).getIntValue()).isEqualTo(2333);
    assertThat(((ExtFieldsHeader) decodedHeader).getLongValue()).isEqualTo(23333333l);
    assertThat(((ExtFieldsHeader) decodedHeader).isBooleanValue()).isEqualTo(true);
    assertThat(((ExtFieldsHeader) decodedHeader).getDoubleValue()).isBetween(0.617, 0.619);
}
Also used : CommandCustomHeader(org.apache.rocketmq.remoting.CommandCustomHeader) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 5 with CommandCustomHeader

use of org.apache.rocketmq.remoting.CommandCustomHeader in project rocketmq by apache.

the class ExtFieldsHeader method testEncodeAndDecode_FilledBody.

@Test
public void testEncodeAndDecode_FilledBody() {
    System.setProperty(RemotingCommand.REMOTING_VERSION_KEY, "2333");
    // org.apache.rocketmq.common.protocol.RequestCode.REGISTER_BROKER
    int code = 103;
    CommandCustomHeader header = new SampleCommandCustomHeader();
    RemotingCommand cmd = RemotingCommand.createRequestCommand(code, header);
    cmd.setBody(new byte[] { 0, 1, 2, 3, 4 });
    ByteBuffer buffer = cmd.encode();
    // Simulate buffer being read in NettyDecoder
    buffer.getInt();
    byte[] bytes = new byte[buffer.limit() - 4];
    buffer.get(bytes, 0, buffer.limit() - 4);
    buffer = ByteBuffer.wrap(bytes);
    RemotingCommand decodedCommand = RemotingCommand.decode(buffer);
    assertThat(decodedCommand.getSerializeTypeCurrentRPC()).isEqualTo(SerializeType.JSON);
    assertThat(decodedCommand.getBody()).isEqualTo(new byte[] { 0, 1, 2, 3, 4 });
}
Also used : CommandCustomHeader(org.apache.rocketmq.remoting.CommandCustomHeader) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

CommandCustomHeader (org.apache.rocketmq.remoting.CommandCustomHeader)15 Test (org.junit.Test)10 ByteBuffer (java.nio.ByteBuffer)6 RemotingCommandException (org.apache.rocketmq.remoting.exception.RemotingCommandException)4 Field (java.lang.reflect.Field)3 JSONField (com.alibaba.fastjson.annotation.JSONField)2 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)2 Annotation (java.lang.annotation.Annotation)1 TreeMap (java.util.TreeMap)1