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    *   http://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 org.jboss.netty.example.uptime;
17  
18  import java.net.InetSocketAddress;
19  import java.util.concurrent.Executors;
20  
21  import org.jboss.netty.bootstrap.ClientBootstrap;
22  import org.jboss.netty.channel.ChannelHandler;
23  import org.jboss.netty.channel.ChannelPipeline;
24  import org.jboss.netty.channel.ChannelPipelineFactory;
25  import org.jboss.netty.channel.Channels;
26  import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
27  import org.jboss.netty.handler.timeout.ReadTimeoutHandler;
28  import org.jboss.netty.util.HashedWheelTimer;
29  import org.jboss.netty.util.Timer;
30  
31  
32  /**
33   * Connects to a server periodically to measure and print the uptime of the
34   * server.  This example demonstrates how to implement reliable reconnection
35   * mechanism in Netty.
36   */
37  public class UptimeClient {
38  
39      // Sleep 5 seconds before a reconnection attempt.
40      static final int RECONNECT_DELAY = 5;
41  
42      // Reconnect when the server sends nothing for 10 seconds.
43      private static final int READ_TIMEOUT = 10;
44  
45      private final String host;
46      private final int port;
47  
48      public UptimeClient(String host, int port) {
49          this.host = host;
50          this.port = port;
51      }
52  
53      public void run() {
54          // Initialize the timer that schedules subsequent reconnection attempts.
55          final Timer timer = new HashedWheelTimer();
56  
57          // Configure the client.
58          final ClientBootstrap bootstrap = new ClientBootstrap(
59                  new NioClientSocketChannelFactory(
60                          Executors.newCachedThreadPool(),
61                          Executors.newCachedThreadPool()));
62  
63          // Configure the pipeline factory.
64          bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
65  
66              private final ChannelHandler timeoutHandler =
67                  new ReadTimeoutHandler(timer, READ_TIMEOUT);
68              private final ChannelHandler uptimeHandler =
69                  new UptimeClientHandler(bootstrap, timer);
70  
71              public ChannelPipeline getPipeline() throws Exception {
72                  return Channels.pipeline(
73                          timeoutHandler, uptimeHandler);
74              }
75          });
76  
77          bootstrap.setOption(
78                  "remoteAddress", new InetSocketAddress(host, port));
79  
80          // Initiate the first connection attempt - the rest is handled by
81          // UptimeClientHandler.
82          bootstrap.connect();
83      }
84  
85      public static void main(String[] args) throws Exception {
86          // Print usage if no argument is specified.
87          if (args.length != 2) {
88              System.err.println(
89                      "Usage: " + UptimeClient.class.getSimpleName() +
90                      " <host> <port>");
91              return;
92          }
93  
94          // Parse options.
95          String host = args[0];
96          int port = Integer.parseInt(args[1]);
97  
98          new UptimeClient(host, port).run();
99      }
100 }