KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > performance > http > load > HttpLoadClient


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tctest.performance.http.load;
5
6 import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;
7
8 import java.io.IOException JavaDoc;
9 import java.util.concurrent.LinkedBlockingQueue JavaDoc;
10
11 final class HttpLoadClient {
12
13   private final LinkedBlockingQueue JavaDoc workQ;
14   private final PooledExecutor requestExecutor;
15   private final RequestCounter counter;
16   private final StatsCollector collector;
17
18   protected HttpLoadClient(LinkedBlockingQueue JavaDoc workQ, PooledExecutor requestExecutor, String JavaDoc[] hosts, int sessionCount,
19                            int stickyRatio, RequestCounter counter) {
20
21     this.counter = counter;
22     this.workQ = workQ;
23     this.requestExecutor = requestExecutor;
24     this.collector = new StatsCollector();
25   }
26
27   public void execute() throws InterruptedException JavaDoc {
28     while (true) {
29       WorkItem workItem = (WorkItem) workQ.take();
30       if (workItem.expired(System.currentTimeMillis())) continue;
31       if (workItem.stop()) break;
32       requestExecutor.execute(new Requestor(workItem));
33     }
34     requestExecutor.shutdownAfterProcessingCurrentlyQueuedTasks();
35     requestExecutor.awaitTerminationAfterShutdown();
36
37     collector.finalStat();
38   }
39
40   public StatsCollector getCollector() {
41     return collector;
42   }
43
44   private void err(String JavaDoc str) {
45     System.err.println(str);
46   }
47
48   private class Requestor implements Runnable JavaDoc {
49     private WorkItem workItem;
50
51     private Requestor(WorkItem workItem) {
52       this.workItem = workItem;
53     }
54
55     public void run() {
56       try {
57         workItem.execute(collector);
58         counter.increment();
59       } catch (IOException JavaDoc e) {
60         err("--Host Response Dropped");
61       } catch (NullPointerException JavaDoc e) {
62         err("--Client Connection Stale");
63       } catch (IllegalStateException JavaDoc e) {
64         err("--Client Connection is not Open");
65       } finally {
66         workItem.done();
67       }
68     }
69   }
70
71 }
72
Popular Tags