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;
}
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);
}
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 });
}
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);
}
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 });
}
Aggregations