本文主要研究一下rocketmq的RemotingException

RemotingException

org/apache/rocketmq/remoting/exception/RemotingException.java

public class RemotingException extends Exception {
    private static final long serialVersionUID = -5690687334570505110L;

    public RemotingException(String message) {
        super(message);
    }

    public RemotingException(String message, Throwable cause) {
        super(message, cause);
    }
}
  • 继承自checked exception,底下有RemotingCommandException、RemotingConnectException、RemotingSendRequestException、RemotingTimeoutException、RemotingTooMuchRequestException

RemotingCommandException

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);
    }
}
  • RemotingCommand解码decodeCommandCustomHeader时可能抛出的异常

RemotingConnectException

org/apache/rocketmq/remoting/exception/RemotingConnectException.java

public class RemotingConnectException extends RemotingException {
    private static final long serialVersionUID = -5565366231695911316L;

    public RemotingConnectException(String addr) {
        this(addr, null);
    }

    public RemotingConnectException(String addr, Throwable cause) {
        super("connect to <" + addr + "> failed", cause);
    }
}
  • NettyRemotingClient在channel出现问题的时候会抛出RemotingConnectException

RemotingSendRequestException

org/apache/rocketmq/remoting/exception/RemotingSendRequestException.java

public class RemotingSendRequestException extends RemotingException {
    private static final long serialVersionUID = 5391285827332471674L;

    public RemotingSendRequestException(String addr) {
        this(addr, null);
    }

    public RemotingSendRequestException(String addr, Throwable cause) {
        super("send request to <" + addr + "> failed", cause);
    }
}
  • NettyRemotingClient在发送请求失败的时候,会抛出RemotingSendRequestException

RemotingTimeoutException

org/apache/rocketmq/remoting/exception/RemotingTimeoutException.java

public class RemotingTimeoutException extends RemotingException {

    private static final long serialVersionUID = 4106899185095245979L;

    public RemotingTimeoutException(String message) {
        super(message);
    }

    public RemotingTimeoutException(String addr, long timeoutMillis) {
        this(addr, timeoutMillis, null);
    }

    public RemotingTimeoutException(String addr, long timeoutMillis, Throwable cause) {
        super("wait response on the channel <" + addr + "> timeout, " + timeoutMillis + "(ms)", cause);
    }
}
  • NettyRemotingClient在发送请求时,如果返回回来的RemotingCommand为null,但是发送请求成功,则抛出RemotingTimeoutException

RemotingTooMuchRequestException

org/apache/rocketmq/remoting/exception/RemotingTooMuchRequestException.java

public class RemotingTooMuchRequestException extends RemotingException {
    private static final long serialVersionUID = 4326919581254519654L;

    public RemotingTooMuchRequestException(String message) {
        super(message);
    }
}
  • NettyRemotingAbstract在执行请求之前要进行流控,如果获取不到信号量,则区分是否是超时,如果是无timeout获取信号量也没获取到,则表示RemotingTooMuchRequestException

小结

rocketmq的remoting模块的异常采用的是checked exception,定义了根异常RemotingException,底下有几个异常分别为RemotingCommandException、RemotingConnectException、RemotingSendRequestException、RemotingTimeoutException、RemotingTooMuchRequestException。

doc


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...


引用和评论

0 条评论