Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/the/flash/NettyClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public void initChannel(SocketChannel ch) {
connect(bootstrap, "juejin.im", 80, MAX_RETRY);
}

// 使用退避策略来自动重连
private static void connect(Bootstrap bootstrap, String host, int port, int retry) {
bootstrap.connect(host, port).addListener(future -> {
if (future.isSuccess()) {
Expand All @@ -56,6 +57,7 @@ private static void connect(Bootstrap bootstrap, String host, int port, int retr
// 本次重连的间隔
int delay = 1 << order;
System.err.println(new Date() + ": 连接失败,第" + order + "次重连……");
// bootstrap.config().group() 返回的就是我们在一开始的时候配置的线程模型 workerGroup,调 workerGroup 的 schedule 方法即可实现定时任务逻辑
bootstrap.config().group().schedule(() -> connect(bootstrap, host, port, retry - 1), delay, TimeUnit
.SECONDS);
}
Expand Down