1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
34
35
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
42 static final int RECONNECT_DELAY = Integer.parseInt(System.getProperty("reconnectDelay", "5"));
43
44 static final int READ_TIMEOUT = Integer.parseInt(System.getProperty("readTimeout", "10"));
45
46 public static void main(String[] args) throws Exception {
47
48 final Timer timer = new HashedWheelTimer();
49
50
51 final ClientBootstrap bootstrap = new ClientBootstrap(
52 new NioClientSocketChannelFactory(
53 Executors.newCachedThreadPool(),
54 Executors.newCachedThreadPool()));
55
56
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
70
71 bootstrap.connect();
72 }
73 }