2.19 ExchangeChannel
Channel 接口的功能以及 Transport 层对 Channel 接口的实现。在 Exchange 层中定义了 ExchangeChannel 接口,它在 Channel 接口之上抽象了 Exchange 层的网络连接。ExchangeChannel 接口的定义如下:
public interface ExchangeChannel extends Channel {
// ...
}

2.19.1 继承关系


2.19.2 核心方法
💻 1. 基础请求方法
CompletableFuture<Object> request(Object request) throws RemotingException;
- 作用: 发送请求并返回异步响应结果
- 参数: request - 请求对象
- 返回值: CompletableFuture<Object> - 异步响应结果
- 特点: 使用默认超时时间,已标记为 @Deprecated
🔑 2. 带超时的请求方法
CompletableFuture<Object> request(Object request, int timeout) throws RemotingException;
- 作用: 发送请求并在指定超时时间内等待响应
- 参数:
- request - 请求对象
- timeout - 超时时间(毫秒)
- 返回值: CompletableFuture<Object> - 异步响应结果
- 特点: 可自定义超时时间,已标记为 @Deprecated
🎲 3. 带执行器的请求方法
CompletableFuture<Object> request(Object request, ExecutorService executor) throws RemotingException;
- 作用: 使用指定执行器发送请求并处理响应
- 参数:
- request - 请求对象
- executor - 执行器服务,用于处理响应回调
- 返回值: CompletableFuture<Object> - 异步响应结果
- 特点: 支持自定义线程池处理响应
☎️ 4. 带超时和执行器的请求方法
CompletableFuture<Object> request(Object request, int timeout, ExecutorService executor) throws RemotingException;
- 作用: 使用指定执行器发送请求并在指定超时时间内等待响应
- 参数:
- request - 请求对象
- timeout - 超时时间(毫秒)
- executor - 执行器服务
- 返回值: CompletableFuture<Object> - 异步响应结果
- 特点: 最完整的请求方法,支持自定义超时和执行器
📞 5. 其它重要的方法
// 获取交换处理器
ExchangeHandler getExchangeHandler();
- 作用: 获取与此通道关联的消息处理器
- 返回值: ExchangeHandler - 处理请求和响应的处理器
- 用途: 用于处理接收到的请求和响应消
void close(int timeout);
- 作用: 优雅地关闭通道
- 参数: timeout - 关闭超时时间
- 特点: 确保正在进行的请求得到适当处理后再关闭连接
2.19.3 设计特点
- 异步通信: 所有请求方法都返回 CompletableFuture,支持异步非阻塞通信
- 灵活性: 提供多种重载方法满足不同场景需求
- 可扩展性: 通过 ExecutorService 参数支持自定义线程池
- 向后兼容: 保留了旧版方法但标记为废弃
- SPI支持: 接口标记为 SPI,允许不同的实现
2.19.4 使用场景
- 服务消费者向服务提供者发送调用请求
- 心跳检测机制中的ping/pong消息交互
- 任意需要双向通信的场景
- 需要异步处理响应的应用场景

1015

被折叠的 条评论
为什么被折叠?



