Search in sources :

Example 11 with CommandCustomHeader

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

the class ExtFieldsHeader method testCreateRequestCommand_RegisterBroker.

@Test
public void testCreateRequestCommand_RegisterBroker() {
    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);
    assertThat(cmd.getCode()).isEqualTo(code);
    assertThat(cmd.getVersion()).isEqualTo(2333);
    // flag bit 0: 0 presents request
    assertThat(cmd.getFlag() & 0x01).isEqualTo(0);
}
Also used : CommandCustomHeader(org.apache.rocketmq.remoting.CommandCustomHeader) Test(org.junit.Test)

Example 12 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_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 13 with CommandCustomHeader

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

the class RemotingCommand method decodeCommandCustomHeader.

/**
 *解码过程
 */
public CommandCustomHeader decodeCommandCustomHeader(Class<? extends CommandCustomHeader> classHeader) throws RemotingCommandException {
    CommandCustomHeader objectHeader;
    try {
        objectHeader = classHeader.newInstance();
    } catch (InstantiationException e) {
        return null;
    } catch (IllegalAccessException e) {
        return null;
    }
    if (this.extFields != null) {
        Field[] fields = getClazzFields(classHeader);
        for (Field field : fields) {
            if (!Modifier.isStatic(field.getModifiers())) {
                String fieldName = field.getName();
                if (!fieldName.startsWith("this")) {
                    try {
                        // 字段不允许为空
                        // 从扩展字段里面根据key取值,如果值为空,看看是否标记注解不为空,如果标记了那么表示有问题了
                        String value = this.extFields.get(fieldName);
                        if (null == value) {
                            // 进行了缓存 设计模式享元模式 欢迎参考【匠心零度】公众号文章,http://mp.weixin.qq.com/s/aMTVoawP_795kSio5ysh7Q
                            Annotation annotation = getNotNullAnnotation(field);
                            if (annotation != null) {
                                throw new RemotingCommandException("the custom field <" + fieldName + "> is null");
                            }
                            continue;
                        }
                        field.setAccessible(true);
                        String type = getCanonicalName(field.getType());
                        Object valueParsed;
                        if (type.equals(STRING_CANONICAL_NAME)) {
                            valueParsed = value;
                        } else if (type.equals(INTEGER_CANONICAL_NAME_1) || type.equals(INTEGER_CANONICAL_NAME_2)) {
                            valueParsed = Integer.parseInt(value);
                        } else if (type.equals(LONG_CANONICAL_NAME_1) || type.equals(LONG_CANONICAL_NAME_2)) {
                            valueParsed = Long.parseLong(value);
                        } else if (type.equals(BOOLEAN_CANONICAL_NAME_1) || type.equals(BOOLEAN_CANONICAL_NAME_2)) {
                            valueParsed = Boolean.parseBoolean(value);
                        } else if (type.equals(DOUBLE_CANONICAL_NAME_1) || type.equals(DOUBLE_CANONICAL_NAME_2)) {
                            valueParsed = Double.parseDouble(value);
                        } else {
                            throw new RemotingCommandException("the custom field <" + fieldName + "> type is not supported");
                        }
                        field.set(objectHeader, valueParsed);
                    } catch (Throwable e) {
                        log.error("Failed field [{}] decoding", fieldName, e);
                    }
                }
            }
        }
        objectHeader.checkFields();
    }
    return objectHeader;
}
Also used : JSONField(com.alibaba.fastjson.annotation.JSONField) Field(java.lang.reflect.Field) CommandCustomHeader(org.apache.rocketmq.remoting.CommandCustomHeader) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException) Annotation(java.lang.annotation.Annotation)

Example 14 with CommandCustomHeader

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

the class RemotingCommand method decodeCommandCustomHeader.

public CommandCustomHeader decodeCommandCustomHeader(Class<? extends CommandCustomHeader> classHeader) throws RemotingCommandException {
    CommandCustomHeader objectHeader;
    try {
        objectHeader = classHeader.newInstance();
    } catch (InstantiationException e) {
        return null;
    } catch (IllegalAccessException e) {
        return null;
    }
    if (this.extFields != null) {
        Field[] fields = getClazzFields(classHeader);
        for (Field field : fields) {
            if (!Modifier.isStatic(field.getModifiers())) {
                String fieldName = field.getName();
                if (!fieldName.startsWith("this")) {
                    try {
                        String value = this.extFields.get(fieldName);
                        if (null == value) {
                            if (!isFieldNullable(field)) {
                                throw new RemotingCommandException("the custom field <" + fieldName + "> is null");
                            }
                            continue;
                        }
                        field.setAccessible(true);
                        String type = getCanonicalName(field.getType());
                        Object valueParsed;
                        if (type.equals(STRING_CANONICAL_NAME)) {
                            valueParsed = value;
                        } else if (type.equals(INTEGER_CANONICAL_NAME_1) || type.equals(INTEGER_CANONICAL_NAME_2)) {
                            valueParsed = Integer.parseInt(value);
                        } else if (type.equals(LONG_CANONICAL_NAME_1) || type.equals(LONG_CANONICAL_NAME_2)) {
                            valueParsed = Long.parseLong(value);
                        } else if (type.equals(BOOLEAN_CANONICAL_NAME_1) || type.equals(BOOLEAN_CANONICAL_NAME_2)) {
                            valueParsed = Boolean.parseBoolean(value);
                        } else if (type.equals(DOUBLE_CANONICAL_NAME_1) || type.equals(DOUBLE_CANONICAL_NAME_2)) {
                            valueParsed = Double.parseDouble(value);
                        } else {
                            throw new RemotingCommandException("the custom field <" + fieldName + "> type is not supported");
                        }
                        field.set(objectHeader, valueParsed);
                    } catch (Throwable e) {
                        log.error("Failed field [{}] decoding", fieldName, e);
                    }
                }
            }
        }
        objectHeader.checkFields();
    }
    return objectHeader;
}
Also used : JSONField(com.alibaba.fastjson.annotation.JSONField) Field(java.lang.reflect.Field) CommandCustomHeader(org.apache.rocketmq.remoting.CommandCustomHeader) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException)

Example 15 with CommandCustomHeader

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

the class ExtFieldsHeader method testCreateRequestCommand_RegisterBroker.

@Test
public void testCreateRequestCommand_RegisterBroker() {
    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);
    assertThat(cmd.getCode()).isEqualTo(code);
    assertThat(cmd.getVersion()).isEqualTo(2333);
    // flag bit 0: 0 presents request
    assertThat(cmd.getFlag() & 0x01).isEqualTo(0);
}
Also used : CommandCustomHeader(org.apache.rocketmq.remoting.CommandCustomHeader) 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