前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊rocketmq的RemotingCommandException

聊聊rocketmq的RemotingCommandException

原创
作者头像
code4it
修改2019-12-18 10:21:26
4080
修改2019-12-18 10:21:26
举报
文章被收录于专栏:码匠的流水账码匠的流水账

本文主要研究一下rocketmq的RemotingCommandException

RemotingCommandException

rocketmq-all-4.6.0-source-release/remoting/src/main/java/org/apache/rocketmq/remoting/exception/RemotingCommandException.java

代码语言:javascript
复制
public class RemotingCommandException extends RemotingException {
    private static final long serialVersionUID = -6061365915274953096L;
​
    public RemotingCommandException(String message) {
        super(message, null);
    }
​
    public RemotingCommandException(String message, Throwable cause) {
        super(message, cause);
    }
}
  • RemotingCommandException继承了RemotingException

RemotingCommand

rocketmq-all-4.6.0-source-release/remoting/src/main/java/org/apache/rocketmq/remoting/protocol/RemotingCommand.java

代码语言:javascript
复制
public class RemotingCommand {
​
    //......
​
    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;
    }
​
    private boolean isFieldNullable(Field field) {
        if (!NULLABLE_FIELD_CACHE.containsKey(field)) {
            Annotation annotation = field.getAnnotation(CFNotNull.class);
            synchronized (NULLABLE_FIELD_CACHE) {
                NULLABLE_FIELD_CACHE.put(field, annotation == null);
            }
        }
        return NULLABLE_FIELD_CACHE.get(field);
    }
​
    //......
}
  • decodeCommandCustomHeader方法在value为null且标注了CFNotNull时候以及解析不到field.getType()时会抛出RemotingCommandException

小结

RemotingCommand的decodeCommandCustomHeader方法在value为null且标注了CFNotNull时候以及解析不到field.getType()时会抛出RemotingCommandException

doc

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • RemotingCommandException
  • RemotingCommand
  • 小结
  • doc
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档