Ignore:
Timestamp:
07/08/15 09:03:46 (9 years ago)
Author:
pharms
Message:
  • added ordering id for requests and responses
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/autoquest-httpmonitor/src/main/java/de/ugoe/cs/autoquest/httpmonitor/proxy/HttpMonitorRemoteExchangeHandler.java

    r1614 r1991  
    1717import java.io.IOException; 
    1818import java.io.OutputStreamWriter; 
    19 import java.util.HashSet; 
    20 import java.util.Set; 
    2119import java.util.logging.Level; 
    2220 
     
    2523import javax.xml.bind.Marshaller; 
    2624 
    27 import org.eclipse.jetty.client.HttpClient; 
    2825import org.eclipse.jetty.client.api.Request; 
    29 import org.eclipse.jetty.client.api.Response.CompleteListener; 
    3026import org.eclipse.jetty.client.api.Result; 
    3127import org.eclipse.jetty.client.util.OutputStreamContentProvider; 
    3228import org.eclipse.jetty.http.HttpMethod; 
    3329import org.eclipse.jetty.http.HttpVersion; 
    34 import org.eclipse.jetty.util.thread.QueuedThreadPool; 
    3530 
    3631import de.ugoe.cs.autoquest.httpmonitor.HttpMonitor; 
    37 import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorComponent; 
    38 import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorException; 
    3932import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorExchangeHandler; 
    4033import de.ugoe.cs.autoquest.plugin.http.logdata.HttpExchange; 
     
    4841 * request/response. It then creates an HTTP request to the central monitor and sends it there. 
    4942 * It is initialized with the name of the server and the port on which the central monitor runs. 
    50  * If the exchanges can not be forwarded to the central server, they are discarded. 
     43 * If the exchanges cannot be forwarded to the central server, they are discarded. 
    5144 * </p> 
    5245 *  
     
    5447 */ 
    5548public class HttpMonitorRemoteExchangeHandler 
    56     implements CompleteListener, HttpMonitorExchangeHandler, HttpMonitorComponent 
     49    extends HttpMonitorRemoteConnection 
     50    implements HttpMonitorExchangeHandler 
    5751{ 
    58  
    59     /** 
    60      * <p> 
    61      * the HTTP client used internally to send data to the central server 
    62      * </p> 
    63      */ 
    64     private HttpClient httpClient; 
    65      
    66     /** 
    67      * <p> 
    68      * the host name of the central server 
    69      * </p> 
    70      */ 
    71     private String httpMonitorServer; 
    72      
    73     /** 
    74      * <p> 
    75      * the port of the central server 
    76      * </p> 
    77      */ 
    78     private int httpMonitorPort; 
    79  
    80     /** 
    81      * <p> 
    82      * a set of requests send to the central server for which the response was not received yet 
    83      * </p> 
    84      */ 
    85     private Set<Request> openRequests = new HashSet<Request>(); 
    8652     
    8753    /** 
     
    9460     */ 
    9561    public HttpMonitorRemoteExchangeHandler(String httpMonitorServer, int httpMonitorPort) { 
    96         this.httpMonitorServer = httpMonitorServer; 
    97         this.httpMonitorPort = httpMonitorPort; 
    98     } 
    99  
    100     /* (non-Javadoc) 
    101      * @see de.ugoe.cs.autoquest.httpmonitor.HttpMonitorComponent#init() 
    102      */ 
    103     @Override 
    104     public void init() throws IllegalStateException, HttpMonitorException { 
    105         httpClient = createHttpClient(); 
    106     } 
    107  
    108     /* (non-Javadoc) 
    109      * @see de.ugoe.cs.autoquest.httpmonitor.HttpMonitorComponent#start() 
    110      */ 
    111     @Override 
    112     public void start() throws IllegalStateException, HttpMonitorException { 
    113         try { 
    114             httpClient.start(); 
    115             httpClient.getContentDecoderFactories().clear(); 
    116         } 
    117         catch (Exception e) { 
    118             throw new HttpMonitorException("could not start client for HTTP-Monitor", e); 
    119         } 
    120     } 
    121  
    122     /* (non-Javadoc) 
    123      * @see de.ugoe.cs.autoquest.httpmonitor.HttpMonitorComponent#stop() 
    124      */ 
    125     @Override 
    126     public void stop() { 
    127         if (httpClient != null) { 
    128             try { 
    129                 httpClient.stop(); 
    130             } 
    131             catch (Exception e) { 
    132                 Console.traceln(Level.WARNING, "could not stop client for HTTP-Monitor"); 
    133                 Console.logException(e); 
    134             } 
    135         } 
    136          
    137         httpClient = null; 
     62        super(httpMonitorServer, httpMonitorPort); 
    13863    } 
    13964 
     
    14469    public synchronized void handleHttpExchange(HttpExchange httpExchange) { 
    14570        // send the exchange to the server and wait for completion 
    146         Request httpMonitorRequest = httpClient.newRequest(httpMonitorServer, httpMonitorPort); 
     71        Request httpMonitorRequest = super.newRequest(); 
    14772        httpMonitorRequest.method(HttpMethod.POST); 
    14873        httpMonitorRequest.version(HttpVersion.HTTP_1_1); 
     
    15176        httpMonitorRequest.content(out); 
    15277         
    153         openRequests.add(httpMonitorRequest); 
    154         httpMonitorRequest.send(this); 
     78        super.sendRequest(httpMonitorRequest); 
    15579 
    15680        try { 
     
    17397            Console.logException(e); 
    17498        } 
    175  
    176         try { 
    177             // wait for the request to be removed from the list asynchronously 
    178             int count = 30; 
    179             while (openRequests.contains(httpMonitorRequest)) { 
    180                 this.wait(1000); 
    181                  
    182                 if (--count == 0) { 
    183                     // after 30 seconds, cancel the sending of the loggin request 
    184                     openRequests.remove(httpMonitorRequest); 
    185                     break; 
    186                 } 
    187             } 
    188         } 
    189         catch (InterruptedException e) { 
    190             // ignore, as this may only happen on system shutdown 
    191         } 
    192     } 
    193  
    194     /* (non-Javadoc) 
    195      * @see org.eclipse.jetty.client.api.Response.CompleteListener#onComplete(Result) 
    196      */ 
    197     @Override 
    198     public synchronized void onComplete(Result result) { 
     99         
     100        // wait to ensure that the request was received 
     101        Result result = super.getResult(httpMonitorRequest); 
     102         
    199103        if (result.isFailed()) { 
    200104            Console.traceln 
     
    202106            Console.logException((Exception) result.getFailure()); 
    203107        } 
    204          
    205         openRequests.remove(result.getRequest()); 
    206         this.notify(); 
    207     } 
    208  
    209     /** 
    210      * <p> 
    211      * convenience method to create an initialize the utilized HTTP client 
    212      * </p> 
    213      */ 
    214     private HttpClient createHttpClient() { 
    215         HttpClient client = new HttpClient(); 
    216          
    217         QueuedThreadPool executor = new QueuedThreadPool(10); 
    218         executor.setName("HttpMonitorClients"); 
    219         client.setExecutor(executor); 
    220  
    221         client.setMaxConnectionsPerDestination(10000); 
    222         client.setIdleTimeout(30000); 
    223         client.setConnectTimeout(30000); 
    224         client.setStopTimeout(30000); 
    225         client.setRequestBufferSize(1024*1024); 
    226         client.setResponseBufferSize(1024*1024); 
    227          
    228         return client; 
    229108    } 
    230109} 
Note: See TracChangeset for help on using the changeset viewer.