Search in sources :

Example 6 with CommandCustomHeader

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();
}
Also used : CommandCustomHeader(org.apache.rocketmq.remoting.CommandCustomHeader) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 7 with CommandCustomHeader

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;
}
Also used : CommandCustomHeader(org.apache.rocketmq.remoting.CommandCustomHeader)

Example 8 with CommandCustomHeader

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();
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) CommandCustomHeader(org.apache.rocketmq.remoting.CommandCustomHeader) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException) Test(org.junit.Test)

Example 9 with CommandCustomHeader

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);
    }
}
Also used : Field(java.lang.reflect.Field) CommandCustomHeader(org.apache.rocketmq.remoting.CommandCustomHeader) TreeMap(java.util.TreeMap)

Example 10 with CommandCustomHeader

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();
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) CommandCustomHeader(org.apache.rocketmq.remoting.CommandCustomHeader) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException) 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