Changeset 1991 for trunk/autoquest-httpmonitor/src/main/java/de/ugoe/cs/autoquest/httpmonitor/proxy/HttpMonitorRemoteExchangeHandler.java
- Timestamp:
- 07/08/15 09:03:46 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/autoquest-httpmonitor/src/main/java/de/ugoe/cs/autoquest/httpmonitor/proxy/HttpMonitorRemoteExchangeHandler.java
r1614 r1991 17 17 import java.io.IOException; 18 18 import java.io.OutputStreamWriter; 19 import java.util.HashSet;20 import java.util.Set;21 19 import java.util.logging.Level; 22 20 … … 25 23 import javax.xml.bind.Marshaller; 26 24 27 import org.eclipse.jetty.client.HttpClient;28 25 import org.eclipse.jetty.client.api.Request; 29 import org.eclipse.jetty.client.api.Response.CompleteListener;30 26 import org.eclipse.jetty.client.api.Result; 31 27 import org.eclipse.jetty.client.util.OutputStreamContentProvider; 32 28 import org.eclipse.jetty.http.HttpMethod; 33 29 import org.eclipse.jetty.http.HttpVersion; 34 import org.eclipse.jetty.util.thread.QueuedThreadPool;35 30 36 31 import de.ugoe.cs.autoquest.httpmonitor.HttpMonitor; 37 import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorComponent;38 import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorException;39 32 import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorExchangeHandler; 40 33 import de.ugoe.cs.autoquest.plugin.http.logdata.HttpExchange; … … 48 41 * request/response. It then creates an HTTP request to the central monitor and sends it there. 49 42 * It is initialized with the name of the server and the port on which the central monitor runs. 50 * If the exchanges can 43 * If the exchanges cannot be forwarded to the central server, they are discarded. 51 44 * </p> 52 45 * … … 54 47 */ 55 48 public class HttpMonitorRemoteExchangeHandler 56 implements CompleteListener, HttpMonitorExchangeHandler, HttpMonitorComponent 49 extends HttpMonitorRemoteConnection 50 implements HttpMonitorExchangeHandler 57 51 { 58 59 /**60 * <p>61 * the HTTP client used internally to send data to the central server62 * </p>63 */64 private HttpClient httpClient;65 66 /**67 * <p>68 * the host name of the central server69 * </p>70 */71 private String httpMonitorServer;72 73 /**74 * <p>75 * the port of the central server76 * </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 yet83 * </p>84 */85 private Set<Request> openRequests = new HashSet<Request>();86 52 87 53 /** … … 94 60 */ 95 61 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); 138 63 } 139 64 … … 144 69 public synchronized void handleHttpExchange(HttpExchange httpExchange) { 145 70 // send the exchange to the server and wait for completion 146 Request httpMonitorRequest = httpClient.newRequest(httpMonitorServer, httpMonitorPort);71 Request httpMonitorRequest = super.newRequest(); 147 72 httpMonitorRequest.method(HttpMethod.POST); 148 73 httpMonitorRequest.version(HttpVersion.HTTP_1_1); … … 151 76 httpMonitorRequest.content(out); 152 77 153 openRequests.add(httpMonitorRequest); 154 httpMonitorRequest.send(this); 78 super.sendRequest(httpMonitorRequest); 155 79 156 80 try { … … 173 97 Console.logException(e); 174 98 } 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 199 103 if (result.isFailed()) { 200 104 Console.traceln … … 202 106 Console.logException((Exception) result.getFailure()); 203 107 } 204 205 openRequests.remove(result.getRequest());206 this.notify();207 }208 209 /**210 * <p>211 * convenience method to create an initialize the utilized HTTP client212 * </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;229 108 } 230 109 }
Note: See TracChangeset
for help on using the changeset viewer.