View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty5.example.uptime;
17  
18  import io.netty5.bootstrap.Bootstrap;
19  import io.netty5.channel.ChannelInitializer;
20  import io.netty5.channel.EventLoopGroup;
21  import io.netty5.channel.MultithreadEventLoopGroup;
22  import io.netty5.channel.nio.NioHandler;
23  import io.netty5.channel.socket.SocketChannel;
24  import io.netty5.channel.socket.nio.NioSocketChannel;
25  import io.netty5.handler.timeout.IdleStateHandler;
26  
27  
28  /**
29   * Connects to a server periodically to measure and print the uptime of the
30   * server.  This example demonstrates how to implement reliable reconnection
31   * mechanism in Netty.
32   */
33  public final class UptimeClient {
34  
35      static final String HOST = System.getProperty("host", "127.0.0.1");
36      static final int PORT = Integer.parseInt(System.getProperty("port", "8080"));
37      // Sleep 5 seconds before a reconnection attempt.
38      static final int RECONNECT_DELAY = Integer.parseInt(System.getProperty("reconnectDelay", "5"));
39      // Reconnect when the server sends nothing for 10 seconds.
40      private static final int READ_TIMEOUT = Integer.parseInt(System.getProperty("readTimeout", "10"));
41  
42      private static final UptimeClientHandler handler = new UptimeClientHandler();
43      private static final Bootstrap bs = new Bootstrap();
44  
45      public static void main(String[] args) throws Exception {
46          EventLoopGroup group = new MultithreadEventLoopGroup(NioHandler.newFactory());
47          bs.group(group)
48                  .channel(NioSocketChannel.class)
49                  .remoteAddress(HOST, PORT)
50                  .handler(new ChannelInitializer<SocketChannel>() {
51                      @Override
52                      protected void initChannel(SocketChannel ch) throws Exception {
53                          ch.pipeline().addLast(new IdleStateHandler(READ_TIMEOUT, 0, 0), handler);
54                      }
55                  });
56          bs.connect();
57      }
58  
59      static void connect() {
60          bs.connect().addListener(future -> {
61              if (future.cause() != null) {
62                  handler.startTime = -1;
63                  handler.println("Failed to connect: " + future.cause());
64              }
65          });
66      }
67  }