首页 > 解决方案 > Netty 4 ChannelGroup.find(ChannelId id)但没有找到(String/Integer id)

问题描述

当我从 Netty 3 升级到 Netty 4 时,我发现 ChannelGroup 有 find(Integer id) API 来获取 Netty3 中的 Channel,而它在 Netty 4 中更改为 find(ChannelId id)。当我从数据库中找到一个 id 时,我可以'在 Netty 4 中找不到任何 API 来通过 id 和 String/Integer 类型参数获取 Channel。也不能使用 String/Integer id 来查找 ChannelId。提前致谢!

我在网上找了很久。有什么想法吗?Netty 4 使用 ConcurrentMap 来维护 channelGroup 中的通道。如果无法解决,现在我想自己维护一个。

Netty3

public interface ChannelGroup extends Set<Channel>, Comparable<ChannelGroup> {
    Channel find(Integer var1);
    // ...
}

Netty4

public interface ChannelGroup extends Set<Channel>, Comparable<ChannelGroup> {
    Channel find(ChannelId var1);
    // ...
}

我希望它有像 ChannelGroup.find(String/Integer id) 这样的 API 来获取 Netty 4 中的 Channel 或使用 id 来获取 ChannelId。

标签: netty

解决方案


在 Netty 4.x 中,Channel id 不仅仅是整数,以减少冲突的风险。你ChannelId就是Serializable这样,你可以从一个byte[]tho 重新创建它。


推荐阅读