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 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
33
34
35
36
37
38
39
40
41 public class UptimeClient {
42
43
44 static final int RECONNECT_DELAY = 5;
45
46
47 private static final int READ_TIMEOUT = 10;
48
49 public static void main(String[] args) throws Exception {
50
51 if (args.length != 2) {
52 System.err.println(
53 "Usage: " + UptimeClient.class.getSimpleName() +
54 " <host> <port>");
55 return;
56 }
57
58
59 String host = args[0];
60 int port = Integer.parseInt(args[1]);
61
62
63 final Timer timer = new HashedWheelTimer();
64
65
66 final ClientBootstrap bootstrap = new ClientBootstrap(
67 new NioClientSocketChannelFactory(
68 Executors.newCachedThreadPool(),
69 Executors.newCachedThreadPool()));
70
71
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
84
85 bootstrap.connect();
86 }
87 }