View Javadoc

    1   /*
    2    * Copyright 2009 Red Hat, Inc.
    3    *
    4    * Red Hat licenses this file to you under the Apache License, version 2.0
    5    * (the "License"); you may not use this file except in compliance with the
    6    * 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.ChannelPipeline;
    23  import org.jboss.netty.channel.ChannelPipelineFactory;
    24  import org.jboss.netty.channel.Channels;
    25  import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
    26  import org.jboss.netty.handler.timeout.ReadTimeoutHandler;
    27  import org.jboss.netty.util.HashedWheelTimer;
    28  import org.jboss.netty.util.Timer;
    29
    30
    31  /**
    32   * Connects to a server periodically to measure and print the uptime of the
    33   * server.  This example demonstrates how to implement reliable reconnection
    34   * mechanism in Netty.
    35   *
    36   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
    37   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
    38   *
    39   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
    40   */
    41  public class UptimeClient {
    42
    43      // Sleep 5 seconds before a reconnection attempt.
    44      static final int RECONNECT_DELAY = 5;
    45
    46      // Reconnect when the server sends nothing for 10 seconds.
    47      private static final int READ_TIMEOUT = 10;
    48
    49      public static void main(String[] args) throws Exception {
    50          // Print usage if no argument is specified.
    51          if (args.length != 2) {
    52              System.err.println(
    53                      "Usage: " + UptimeClient.class.getSimpleName() +
    54                      " <host> <port>");
    55              return;
    56          }
    57
    58          // Parse options.
    59          String host = args[0];
    60          int port = Integer.parseInt(args[1]);
    61
    62          // Initialize the timer that schedules subsequent reconnection attempts.
    63          final Timer timer = new HashedWheelTimer();
    64
    65          // Configure the client.
    66          final ClientBootstrap bootstrap = new ClientBootstrap(
    67                  new NioClientSocketChannelFactory(
    68                          Executors.newCachedThreadPool(),
    69                          Executors.newCachedThreadPool()));
    70
    71          // Configure the pipeline factory.
    72          bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
    73              public ChannelPipeline getPipeline() throws Exception {
    74                  return Channels.pipeline(
    75                          new ReadTimeoutHandler(timer, READ_TIMEOUT),
    76                          new UptimeClientHandler(bootstrap, timer));
    77              }
    78          });
    79
    80          bootstrap.setOption(
    81                  "remoteAddress", new InetSocketAddress(host, port));
    82
    83          // Initiate the first connection attempt - the rest is handled by
    84          // UptimeClientHandler.
    85          bootstrap.connect();
    86      }
    87  }