Author: sebb
Date: Tue Apr  7 20:45:28 2009
New Revision: 762953

URL: http://svn.apache.org/viewvc?rev=762953&view=rev
Log:
Make some samplers interruptible: HTTP (both)

Modified:
    
jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler.java
    
jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler2.java
    jakarta/jmeter/trunk/xdocs/changes.xml

Modified: 
jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler.java
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler.java?rev=762953&r1=762952&r2=762953&view=diff
==============================================================================
--- 
jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler.java
 (original)
+++ 
jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler.java
 Tue Apr  7 20:45:28 2009
@@ -39,6 +39,7 @@
 import org.apache.jmeter.protocol.http.control.Header;
 import org.apache.jmeter.protocol.http.control.HeaderManager;
 
+import org.apache.jmeter.samplers.Interruptible;
 import org.apache.jmeter.samplers.SampleResult;
 import org.apache.jmeter.testelement.property.CollectionProperty;
 import org.apache.jmeter.testelement.property.PropertyIterator;
@@ -55,7 +56,7 @@
  * HTTP requests, including cookies and authentication.
  *
  */
-public class HTTPSampler extends HTTPSamplerBase {
+public class HTTPSampler extends HTTPSamplerBase implements Interruptible {
     private static final long serialVersionUID = 233L;
 
     private static final Logger log = LoggingManager.getLoggerForClass();
@@ -73,6 +74,8 @@
     /** Handles writing of a post or put request */
     private transient PostWriter postOrPutWriter;
 
+    private volatile HttpURLConnection savedConn;
+
     /**
      * Constructor for the HTTPSampler object.
      *
@@ -214,6 +217,7 @@
         // works OK even if ContentEncoding is null
         boolean gzipped = ENCODING_GZIP.equals(conn.getContentEncoding());
 
+        savedConn = conn;
         try {
             if (gzipped) {
                 in = new BufferedInputStream(new 
GZIPInputStream(conn.getInputStream()));
@@ -251,6 +255,8 @@
                 log.error("Cause: "+cause);
             }
             in = new BufferedInputStream(conn.getErrorStream());
+        } finally {
+            savedConn = null;
         }
         return readResponse(res, in, contentLength);
     }
@@ -430,6 +436,7 @@
                 try {
                     conn = setupConnection(url, method, res);
                     // Attempt the connection:
+                    savedConn = conn;
                     conn.connect();
                     break;
                 } catch (BindException e) {
@@ -446,6 +453,8 @@
                 } catch (IOException e) {
                     log.debug("Connection failed, giving up");
                     throw e;
+                } finally {
+                    savedConn = null;                    
                 }
             }
             if (retry > MAX_CONN_RETRIES) {
@@ -580,4 +589,14 @@
             }
         }
     }
+
+    /** {...@inheritdoc} */
+    public boolean interrupt() {
+        HttpURLConnection conn = savedConn;
+        if (conn != null) {
+            savedConn = null;
+            conn.disconnect();
+        }
+        return conn != null;
+    }
 }

Modified: 
jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler2.java
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler2.java?rev=762953&r1=762952&r2=762953&view=diff
==============================================================================
--- 
jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler2.java
 (original)
+++ 
jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler2.java
 Tue Apr  7 20:45:28 2009
@@ -39,6 +39,7 @@
 import org.apache.commons.httpclient.Header;
 import org.apache.commons.httpclient.HostConfiguration;
 import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpConnectionManager;
 import org.apache.commons.httpclient.HttpMethod;
 import org.apache.commons.httpclient.HttpMethodBase;
 import org.apache.commons.httpclient.HttpVersion;
@@ -75,6 +76,7 @@
 import org.apache.jmeter.protocol.http.util.HTTPFileArg;
 import org.apache.jmeter.protocol.http.util.LoopbackHttpClientSocketFactory;
 import org.apache.jmeter.protocol.http.util.SlowHttpClientSocketFactory;
+import org.apache.jmeter.samplers.Interruptible;
 import org.apache.jmeter.testelement.property.CollectionProperty;
 import org.apache.jmeter.testelement.property.PropertyIterator;
 import org.apache.jmeter.util.JMeterUtils;
@@ -88,7 +90,7 @@
  * HTTP requests, including cookies and authentication.
  *
  */
-public class HTTPSampler2 extends HTTPSamplerBase {
+public class HTTPSampler2 extends HTTPSamplerBase implements Interruptible {
 
     private static final Logger log = LoggingManager.getLoggerForClass();
 
@@ -235,6 +237,7 @@
         }
     }
 
+    private volatile HttpClient savedClient;
 
     /**
      * Constructor for the HTTPSampler2 object.
@@ -836,6 +839,7 @@
             }
 
 
+            savedClient = client;
             int statusCode = client.executeMethod(httpMethod);
 
             // Needs to be done after execute to pick up all the headers
@@ -917,6 +921,7 @@
             err.setSampleLabel("Error: " + url.toString());
             return err;
         } finally {
+            savedClient = null;
             JOrphanUtils.closeQuietly(instream);
             if (httpMethod != null) {
                 httpMethod.releaseConnection();
@@ -1091,4 +1096,18 @@
             map.clear();
         }
     }
+
+    /** {...@inheritdoc} */
+    public boolean interrupt() {
+        HttpClient client = savedClient;
+        if (client != null) {
+            savedClient = null;
+            // TODO - not sure this is the best method
+            final HttpConnectionManager httpConnectionManager = 
client.getHttpConnectionManager();
+            if (httpConnectionManager instanceof SimpleHttpConnectionManager) 
{// Should be true
+                
((SimpleHttpConnectionManager)httpConnectionManager).shutdown();
+            }
+        }
+        return client != null;
+    }
 }

Modified: jakarta/jmeter/trunk/xdocs/changes.xml
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/xdocs/changes.xml?rev=762953&r1=762952&r2=762953&view=diff
==============================================================================
--- jakarta/jmeter/trunk/xdocs/changes.xml (original)
+++ jakarta/jmeter/trunk/xdocs/changes.xml Tue Apr  7 20:45:28 2009
@@ -238,6 +238,7 @@
 <li>Bug 40045 - Allow Results monitor to select a specific connector</li>
 <li>Bug 46636 - rmi ports</li>
 <li>Mirror server now supports "X-Sleep" header - if this is set, the 
responding thread will wait for the specified number of milliseconds</li>
+<li>Make some samplers interruptible: HTTP (both)</li>
 </ul>
 
 <h3>Non-functional changes</h3>



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to