聊聊rocketmq的RemotingException

本文主要研究一下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

RocketMQ 是阿里巴巴开源的一个分布式消息中间件,MessageExt 是 RocketMQ 中的消息实体类,它是实际传递的数据结构。在 Java 中,你可以使用 `MessageExt` 来构建、发送和接收消息。 以下是一个简单的使用案例: ```java import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer; import org.apache.rocketmq.client.exception.MQClientException; import org.apache.rocketmq.common.message.Message; import org.apache.rocketmq.remoting.exception.RemotingException; public class RocketMQConsumerExample { private static final String GROUP_ID = "your_group_id"; private static final String QUEUE_NAME = "your_queue_name"; public static void main(String[] args) { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(GROUP_ID); try { // 设置消费者属性 consumer.setNamesrvAddr("localhost:9876"); // RocketMQ broker地址 consumer.subscribe(QUEUE_NAME, "*"); // 订阅主题 // 开始消费 consumer.start(); while (true) { Message msg = consumer.pull(); // 拉取消息 if (msg != null) { System.out.printf("Received message from topic %s: %s%n", msg.getTopic(), new String(msg.getBody())); // 处理接收到的消息 consumer.commitOffset(msg); // 提交偏移量,确认已处理 } else { break; // 如果没有新消息,退出循环 } } } catch (MQClientException | RemotingException e) { e.printStackTrace(); } finally { consumer.shutdown(); // 关闭消费者 } } } ``` 在这个例子中,我们创建了一个 `DefaultMQPushConsumer` 实例,设置了服务器地址,订阅了指定的主题,然后开始拉取消息并进行处理。当消息被消费后,我们会提交偏移量以表示已经处理过。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值