1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.timeout;
17
18 import static org.jboss.netty.channel.Channels.*;
19
20 import java.util.concurrent.TimeUnit;
21
22 import org.jboss.netty.bootstrap.ServerBootstrap;
23 import org.jboss.netty.channel.ChannelFuture;
24 import org.jboss.netty.channel.ChannelFutureListener;
25 import org.jboss.netty.channel.ChannelHandlerContext;
26 import org.jboss.netty.channel.ChannelPipeline;
27 import org.jboss.netty.channel.ChannelPipelineFactory;
28 import org.jboss.netty.channel.Channels;
29 import org.jboss.netty.channel.MessageEvent;
30 import org.jboss.netty.channel.SimpleChannelDownstreamHandler;
31 import org.jboss.netty.channel.ChannelHandler.Sharable;
32 import org.jboss.netty.util.ExternalResourceReleasable;
33 import org.jboss.netty.util.HashedWheelTimer;
34 import org.jboss.netty.util.Timeout;
35 import org.jboss.netty.util.Timer;
36 import org.jboss.netty.util.TimerTask;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 @Sharable
81 public class WriteTimeoutHandler extends SimpleChannelDownstreamHandler
82 implements ExternalResourceReleasable {
83
84 static final WriteTimeoutException EXCEPTION = new WriteTimeoutException();
85
86 private final Timer timer;
87 private final long timeoutMillis;
88
89
90
91
92
93
94
95
96
97
98 public WriteTimeoutHandler(Timer timer, int timeoutSeconds) {
99 this(timer, timeoutSeconds, TimeUnit.SECONDS);
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113 public WriteTimeoutHandler(Timer timer, long timeout, TimeUnit unit) {
114 if (timer == null) {
115 throw new NullPointerException("timer");
116 }
117 if (unit == null) {
118 throw new NullPointerException("unit");
119 }
120
121 this.timer = timer;
122 if (timeout <= 0) {
123 timeoutMillis = 0;
124 } else {
125 timeoutMillis = Math.max(unit.toMillis(timeout), 1);
126 }
127 }
128
129
130
131
132
133
134 public void releaseExternalResources() {
135 timer.stop();
136 }
137
138 protected long getTimeoutMillis(MessageEvent e) {
139 return timeoutMillis;
140 }
141
142 @Override
143 public void writeRequested(ChannelHandlerContext ctx, MessageEvent e)
144 throws Exception {
145
146 long timeoutMillis = getTimeoutMillis(e);
147 if (timeoutMillis > 0) {
148
149 ChannelFuture future = e.getFuture();
150 final Timeout timeout = timer.newTimeout(
151 new WriteTimeoutTask(ctx, future),
152 timeoutMillis, TimeUnit.MILLISECONDS);
153
154 future.addListener(new TimeoutCanceller(timeout));
155 }
156
157 super.writeRequested(ctx, e);
158 }
159
160 protected void writeTimedOut(ChannelHandlerContext ctx) throws Exception {
161 Channels.fireExceptionCaught(ctx, EXCEPTION);
162 }
163
164 private final class WriteTimeoutTask implements TimerTask {
165
166 private final ChannelHandlerContext ctx;
167 private final ChannelFuture future;
168
169 WriteTimeoutTask(ChannelHandlerContext ctx, ChannelFuture future) {
170 this.ctx = ctx;
171 this.future = future;
172 }
173
174 public void run(Timeout timeout) throws Exception {
175 if (timeout.isCancelled()) {
176 return;
177 }
178
179 if (!ctx.getChannel().isOpen()) {
180 return;
181 }
182
183
184 if (future.setFailure(EXCEPTION)) {
185
186 try {
187 writeTimedOut(ctx);
188 } catch (Throwable t) {
189 fireExceptionCaught(ctx, t);
190 }
191 }
192 }
193 }
194
195
196
197
198
199
200 private static final class TimeoutCanceller implements ChannelFutureListener {
201 private final Timeout timeout;
202
203 TimeoutCanceller(Timeout timeout) {
204 this.timeout = timeout;
205 }
206
207 public void operationComplete(ChannelFuture future) throws Exception {
208 timeout.cancel();
209 }
210 }
211 }