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.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   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
38   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
39   *
40   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
41   */
42  public class UptimeClient {
43  
44      // Sleep 5 seconds before a reconnection attempt.
45      static final int RECONNECT_DELAY = 5;
46  
47      // Reconnect when the server sends nothing for 10 seconds.
48      private static final int READ_TIMEOUT = 10;
49  
50      public static void main(String[] args) throws Exception {
51          // Print usage if no argument is specified.
52          if (args.length != 2) {
53              System.err.println(
54                      "Usage: " + UptimeClient.class.getSimpleName() +
55                      " <host> <port>");
56              return;
57          }
58  
59          // Parse options.
60          String host = args[0];
61          int port = Integer.parseInt(args[1]);
62  
63          // Initialize the timer that schedules subsequent reconnection attempts.
64          final Timer timer = new HashedWheelTimer();
65  
66          // Configure the client.
67          final ClientBootstrap bootstrap = new ClientBootstrap(
68                  new NioClientSocketChannelFactory(
69                          Executors.newCachedThreadPool(),
70                          Executors.newCachedThreadPool()));
71  
72          // Configure the pipeline factory.
73          bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
74  
75              private final ChannelHandler timeoutHandler =
76                  new ReadTimeoutHandler(timer, READ_TIMEOUT);
77              private final ChannelHandler uptimeHandler =
78                  new UptimeClientHandler(bootstrap, timer);
79  
80              public ChannelPipeline getPipeline() throws Exception {
81                  return Channels.pipeline(
82                          timeoutHandler, uptimeHandler);
83              }
84          });
85  
86          bootstrap.setOption(
87                  "remoteAddress", new InetSocketAddress(host, port));
88  
89          // Initiate the first connection attempt - the rest is handled by
90          // UptimeClientHandler.
91          bootstrap.connect();
92      }
93  }