1 23 package com.sun.enterprise.web.connector.grizzly; 24 25 import java.nio.channels.SelectionKey ; 26 import java.util.List ; 27 28 import java.util.concurrent.ConcurrentHashMap ; 29 import java.util.concurrent.ConcurrentLinkedQueue ; 30 import java.util.concurrent.ScheduledThreadPoolExecutor ; 31 import java.util.concurrent.TimeUnit ; 32 33 34 40 public class KeepAlivePipeline implements Pipeline{ 41 42 43 public final static int KEEP_ALIVE_RULE = 0; 44 45 48 private int maxThreads = 1; 49 50 51 54 private int priority = Thread.NORM_PRIORITY; 55 56 57 60 private int port = 8080; 61 62 63 66 private String name; 67 68 69 72 private boolean isStarted = false; 73 74 75 79 private int firstReadTimeout = Constants.DEFAULT_TIMEOUT; 80 81 85 private int keepAliveTimeoutInSeconds = Constants.DEFAULT_TIMEOUT; 86 87 88 91 protected ConcurrentHashMap <SelectionKey ,Integer > keepAliveCounts; 92 93 94 97 protected int maxKeepAliveRequests = -1; 98 99 100 103 protected PipelineStatistic pipelineStat; 104 105 106 109 private KeepAliveStats keepAliveStats; 110 111 112 114 117 public KeepAlivePipeline(){ 118 initPipeline(); 119 } 120 121 123 127 public void initPipeline(){ 128 if (isStarted){ 129 return; 130 } 131 isStarted = true; 132 keepAliveCounts = new ConcurrentHashMap <SelectionKey ,Integer >(); 133 } 134 135 136 140 public void startPipeline(){ 141 if (isStarted){ 142 return; 143 } 144 ; } 146 147 148 152 public void stopPipeline(){ 153 if (!isStarted){ 154 return; 155 } 156 isStarted = false; 157 keepAliveCounts.clear(); 158 } 159 160 162 163 166 public void addTask(Task task){ 167 throw new UnsupportedOperationException (); 168 } 169 170 171 176 public Task getTask() { 177 return null; 178 } 179 180 181 186 public int size() { 187 return 0; 188 } 189 190 191 193 196 public int getWaitingThread(){ 197 return 0; 198 } 199 200 201 204 public void setMaxThreads(int maxThreads){ 205 this.maxThreads = maxThreads; 206 } 207 208 209 212 public int getMaxThreads(){ 213 return maxThreads; 214 } 215 216 217 220 public int getCurrentThreadCount() { 221 return 1; 222 } 223 224 225 228 public int getCurrentThreadsBusy() { 229 return 1; 230 } 231 232 233 236 public int getMaxSpareThreads() { 237 return 0; 238 } 239 240 241 244 public void setPriority(int priority){ 245 this.priority = priority; 246 } 247 248 249 252 public void setName(String name){ 253 this.name = name; 254 } 255 256 257 261 public String getName(){ 262 return name+port; 263 } 264 265 266 270 public void setPort(int port){ 271 this.port = port; 272 } 273 274 275 280 public void setMinThreads(int minThreads){ 281 throw new UnsupportedOperationException (); 282 } 283 284 285 public String toString(){ 286 return "name: " + name + " maxThreads: " + maxThreads ; 287 } 288 289 290 294 public void setQueueSizeInBytes(int maxQueueSizeInBytes){ 295 throw new UnsupportedOperationException (); 296 } 297 298 299 public void setThreadsIncrement(int threadsIncrement){ 300 throw new UnsupportedOperationException (); 301 } 302 303 304 public void setThreadsTimeout(int threadsTimeout){ 305 this.firstReadTimeout = threadsTimeout; 306 } 307 308 309 312 public int getMinSpareThreads() { 313 throw new UnsupportedOperationException (); 314 } 315 316 317 320 public void setMinSpareThreads(int minSpareThreads) { 321 throw new UnsupportedOperationException (); 322 } 323 324 325 327 332 public boolean trap(SelectionKey key){ 333 if ( maxKeepAliveRequests == -1) return true; 334 335 Integer count = keepAliveCounts.get(key); 336 if ( count == null ){ 337 count = 0; 338 if (keepAliveStats != null) { 339 keepAliveStats.incrementCountConnections(); 340 } 341 } 342 343 if ((count+1) > maxKeepAliveRequests){ 344 if (keepAliveStats != null) { 345 keepAliveStats.incrementCountRefusals(); 346 } 347 return false; 348 } 349 350 keepAliveCounts.put(key, count++); 351 if (keepAliveStats != null) { 352 keepAliveStats.incrementCountHits(); 353 } 354 355 return true; 356 } 357 358 359 362 public void untrap(SelectionKey key){ 363 if ( maxKeepAliveRequests == -1) return; 364 365 keepAliveCounts.remove(key); 366 } 367 368 369 372 public int getMaxKeepAliveRequests() { 373 return maxKeepAliveRequests; 374 } 375 376 377 381 public void setMaxKeepAliveRequests(int maxKeepAliveRequests) { 382 this.maxKeepAliveRequests = maxKeepAliveRequests; 383 } 384 385 386 392 public void setKeepAliveTimeoutInSeconds(int keepAliveTimeout){ 393 this.keepAliveTimeoutInSeconds = keepAliveTimeout; 394 } 395 396 397 403 public int getKeepAliveTimeoutInSeconds(){ 404 return keepAliveTimeoutInSeconds; 405 } 406 407 408 412 public boolean dropConnection(){ 413 return (keepAliveTimeoutInSeconds == 0 414 || firstReadTimeout == 0 || maxKeepAliveRequests == 0); 415 } 416 417 418 419 423 public void setPipelineStatistic(PipelineStatistic pipelineStatistic){ 424 this.pipelineStat = pipelineStatistic; 425 } 426 427 428 432 public PipelineStatistic getPipelineStatistic(){ 433 return pipelineStat; 434 } 435 436 437 444 void setKeepAliveStats(KeepAliveStats keepAliveStats) { 445 this.keepAliveStats = keepAliveStats; 446 } 447 448 449 452 public boolean interruptThread(long threadId){ 453 return false; 454 } 455 } 456 457 | Popular Tags |