聊聊rocketmq的RemotingCommandException

  • 2019 年 12 月 18 日
  • 笔记

本文主要研究一下rocketmq的RemotingCommandException

RemotingCommandException

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

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

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