KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.commons.httpclient.HttpClient;
7 import org.apache.commons.httpclient.HttpMethod;
8 import org.apache.commons.httpclient.HttpState;
9 import org.apache.commons.httpclient.methods.GetMethod;
10
11 import java.io.IOException JavaDoc;
12
13 /**
14  * This is a work item that's associated with a specific session
15  */

16
17 public class SessionWorkItem extends ExpiringWorkItem {
18
19   private final String JavaDoc url;
20   private final HttpMethod method;
21   private final boolean gatherStatistic;
22   private final HttpClientAdapter clientAdapter;
23
24   public SessionWorkItem(HttpClientAdapter clientAdapter, String JavaDoc urlPart, boolean gatherStatistic, long expire) {
25     super(expire);
26     this.clientAdapter = clientAdapter;
27     this.url = "http://" + clientAdapter.getHost() + urlPart;
28     this.method = new GetMethod(url);
29     this.gatherStatistic = gatherStatistic;
30   }
31
32   public void executeImpl(HttpClient httpClient, StatsCollector collector) throws IOException JavaDoc {
33     HttpState state = clientAdapter.getSession();
34     httpClient.setState(state);
35
36     long startTime = System.currentTimeMillis();
37     try {
38       int statusCode = httpClient.executeMethod(method);
39       if (gatherStatistic) {
40         collector.addStat(new ResponseStatistic(startTime, System.currentTimeMillis(), url, statusCode));
41       }
42       consumeResponse(method);
43     } finally {
44       method.releaseConnection();
45     }
46   }
47
48 }
Popular Tags