use of org.apache.rocketmq.remoting.CommandCustomHeader in project rocketmq by apache.
the class ExtFieldsHeader method testEncodeAndDecode_EmptyBody.
@Test
public void testEncodeAndDecode_EmptyBody() {
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);
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()).isNull();
}
use of org.apache.rocketmq.remoting.CommandCustomHeader in project rocketmq by apache.
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 by apache.
the class ClusterTestRequestProcessorTest method testGetRouteInfoByTopic.
@Test
public void testGetRouteInfoByTopic() throws RemotingCommandException {
RemotingCommand request = RemotingCommand.createRequestCommand(12, new CommandCustomHeader() {
@Override
public void checkFields() throws RemotingCommandException {
}
});
RemotingCommand remoting = clusterTestProcessor.getRouteInfoByTopic(ctx, request);
assertThat(remoting.getCode()).isEqualTo(ResponseCode.TOPIC_NOT_EXIST);
assertThat(remoting.getBody()).isNull();
assertThat(remoting.getRemark()).isNotNull();
}
use of org.apache.rocketmq.remoting.CommandCustomHeader in project java-example by 1479005017.
the class AlionsRPCHook method parseRequestContent.
protected SortedMap<String, String> parseRequestContent(RemotingCommand request, String accessKeyId, String securityToken, String onsChannel) {
CommandCustomHeader header = request.readCustomHeader();
// sort property
SortedMap<String, String> map = new TreeMap<String, String>();
map.put("AccessKey", accessKeyId);
map.put("OnsChannel", onsChannel);
if (securityToken != null) {
map.put("SecurityToken", securityToken);
}
try {
// add header properties
if (null != header) {
Field[] fields = fieldCache.get(header.getClass());
if (null == fields) {
fields = header.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
}
Field[] tmp = fieldCache.putIfAbsent(header.getClass(), fields);
if (null != tmp) {
fields = tmp;
}
}
for (Field field : fields) {
Object value = field.get(header);
if (null != value && !field.isSynthetic()) {
map.put(field.getName(), value.toString());
}
}
}
return map;
} catch (Exception e) {
throw new RuntimeException("incompatible exception.", e);
}
}
use of org.apache.rocketmq.remoting.CommandCustomHeader in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.
the class ClusterTestRequestProcessorTest method testGetRouteInfoByTopic.
@Test
public void testGetRouteInfoByTopic() throws RemotingCommandException {
RemotingCommand request = RemotingCommand.createRequestCommand(12, new CommandCustomHeader() {
@Override
public void checkFields() throws RemotingCommandException {
}
});
RemotingCommand remoting = clusterTestProcessor.getRouteInfoByTopic(ctx, request);
assertThat(remoting.getCode()).isEqualTo(ResponseCode.TOPIC_NOT_EXIST);
assertThat(remoting.getBody()).isNull();
assertThat(remoting.getRemark()).isNotNull();
}
Aggregations