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 org.jboss.netty.bootstrap.ClientBootstrap;
19  import org.jboss.netty.channel.ChannelHandler;
20  import org.jboss.netty.channel.ChannelPipeline;
21  import org.jboss.netty.channel.ChannelPipelineFactory;
22  import org.jboss.netty.channel.Channels;
23  import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
24  import org.jboss.netty.handler.timeout.ReadTimeoutHandler;
25  import org.jboss.netty.util.HashedWheelTimer;
26  import org.jboss.netty.util.Timer;
27  
28  import java.net.InetSocketAddress;
29  import java.util.concurrent.Executors;
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 final class UptimeClient {
38  
39      static final String HOST = System.getProperty("host", "127.0.0.1");
40      static final int PORT = Integer.parseInt(System.getProperty("port", "8080"));
41      // Sleep 5 seconds before a reconnection attempt.
42      static final int RECONNECT_DELAY = Integer.parseInt(System.getProperty("reconnectDelay", "5"));
43      // Reconnect when the server sends nothing for 10 seconds.
44      static final int READ_TIMEOUT = Integer.parseInt(System.getProperty("readTimeout", "10"));
45  
46      public static void main(String[] args) throws Exception {
47          // Initialize the timer that schedules subsequent reconnection attempts.
48          final Timer timer = new HashedWheelTimer();
49  
50          // Configure the client.
51          final ClientBootstrap bootstrap = new ClientBootstrap(
52                  new NioClientSocketChannelFactory(
53                          Executors.newCachedThreadPool(),
54                          Executors.newCachedThreadPool()));
55  
56          // Configure the pipeline factory.
57          bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
58  
59              private final ChannelHandler timeoutHandler = new ReadTimeoutHandler(timer, READ_TIMEOUT);
60              private final ChannelHandler uptimeHandler = new UptimeClientHandler(bootstrap, timer);
61  
62              public ChannelPipeline getPipeline() {
63                  return Channels.pipeline(timeoutHandler, uptimeHandler);
64              }
65          });
66  
67          bootstrap.setOption("remoteAddress", new InetSocketAddress(HOST, PORT));
68  
69          // Initiate the first connection attempt - the rest is handled by
70          // UptimeClientHandler.
71          bootstrap.connect();
72      }
73  }