1 23 package com.sun.enterprise.web.connector.grizzly; 24 25 import java.util.concurrent.LinkedBlockingQueue ; 26 import java.util.concurrent.ThreadPoolExecutor ; 27 import java.util.concurrent.TimeUnit ; 28 29 34 public class ExecutorServicePipeline implements Pipeline { 35 36 39 private int waitingThreads = 0; 40 41 42 45 private int maxThreads = 50; 46 47 48 51 private int minThreads = 10; 52 53 56 private int minSpareThreads = 5; 57 58 59 62 private int port = 8080; 63 64 65 68 private int threadCount =0; 69 70 71 74 private String name; 75 76 77 80 private int priority = Thread.NORM_PRIORITY; 81 82 83 86 private boolean isStarted = false; 87 88 89 92 private ThreadPoolExecutor workerThreads; 93 94 95 98 protected PipelineStatistic pipelineStat; 99 100 101 104 private int maxQueueSizeInBytes = -1; 105 107 111 public void initPipeline(){ 112 if (isStarted){ 113 return; 114 } 115 isStarted = true; 116 117 if (maxThreads < minSpareThreads){ 118 maxThreads = minSpareThreads; 119 } 120 workerThreads = new ThreadPoolExecutor ( 121 maxThreads, 122 maxThreads, 123 0L, 124 TimeUnit.MILLISECONDS, 125 new LinkedBlockingQueue <Runnable >(), 126 new GrizzlyThreadFactory(name,port,priority)); 127 } 128 129 132 public void startPipeline(){ 133 if (isStarted){ 134 return; 135 } 136 ; } 138 139 140 143 public void stopPipeline(){ 144 if (!isStarted){ 145 return; 146 } 147 isStarted = false; 148 workerThreads.shutdown(); 149 } 150 151 152 155 public boolean interruptThread(long threadID){ 156 return ((GrizzlyThreadFactory)workerThreads.getThreadFactory()) 157 .interruptThread(threadID); 158 } 159 161 162 165 public void addTask(Task task){ 166 if (workerThreads.getQueue().size() > maxQueueSizeInBytes ){ 167 task.cancelTask("Maximum Connections Reached: " 168 + pipelineStat.getQueueSizeInBytes() 169 + " -- Retry later", HtmlHelper.OK); 170 task.getSelectorThread().returnTask(task); 171 return; 172 } 173 workerThreads.execute((Runnable )task); 174 175 if ( pipelineStat != null) { 176 pipelineStat.gather(size()); 178 } 179 } 180 181 182 186 public Task getTask() { 187 return null; 188 } 189 190 191 196 public int size() { 197 return workerThreads.getQueue().size(); 198 } 199 200 201 203 206 public int getWaitingThread(){ 207 return workerThreads.getPoolSize() - workerThreads.getActiveCount(); 208 } 209 210 211 214 public void setMaxThreads(int maxThreads){ 215 this.maxThreads = maxThreads; 216 } 217 218 219 222 public int getMaxThreads(){ 223 return maxThreads; 224 } 225 226 227 230 public int getCurrentThreadCount() { 231 return workerThreads.getPoolSize() ; 232 } 233 234 235 239 public int getCurrentThreadsBusy(){ 240 return workerThreads.getActiveCount(); 241 } 242 243 244 247 public int getMaxSpareThreads() { 248 return getWaitingThread(); 249 } 250 251 252 255 public int getMinSpareThreads() { 256 return 0; 257 } 258 259 260 263 public void setMinSpareThreads(int minSpareThreads) { 264 this.minSpareThreads = minSpareThreads; 265 } 266 267 268 271 public void setPriority(int priority){ 272 this.priority = priority; 273 } 274 275 276 279 public void setName(String name){ 280 this.name = name; 281 } 282 283 284 288 public String getName(){ 289 return name+port; 290 } 291 292 293 297 public void setPort(int port){ 298 this.port = port; 299 } 300 301 302 307 public void setMinThreads(int minThreads){ 308 this.minThreads = minThreads; 309 } 310 311 312 public String toString(){ 313 return "name: " + name + " maxThreads: " + maxThreads 314 + " minThreads:" + minThreads; 315 } 316 317 318 322 public void setQueueSizeInBytes(int maxQueueSizeInBytes){ 323 this.maxQueueSizeInBytes = maxQueueSizeInBytes; 324 if ( pipelineStat != null ) 325 pipelineStat.setQueueSizeInBytes(maxQueueSizeInBytes); 326 } 327 328 329 333 public int getQueueSizeInBytes(){ 334 return maxQueueSizeInBytes; 335 } 336 337 338 public void setThreadsIncrement(int threadsIncrement){ 339 ; } 341 342 343 public void setThreadsTimeout(int threadsTimeout){ 344 ; } 346 347 348 352 public void setPipelineStatistic(PipelineStatistic pipelineStatistic){ 353 this.pipelineStat = pipelineStatistic; 354 } 355 356 357 361 public PipelineStatistic getPipelineStatistic(){ 362 return pipelineStat; 363 } 364 365 } 366 | Popular Tags |