Changeset 1991 for trunk/autoquest-httpmonitor/src/main/java
- Timestamp:
- 07/08/15 09:03:46 (9 years ago)
- Location:
- trunk/autoquest-httpmonitor/src/main/java/de/ugoe/cs/autoquest/httpmonitor
- Files:
-
- 4 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/autoquest-httpmonitor/src/main/java/de/ugoe/cs/autoquest/httpmonitor/HttpMonitor.java
r1384 r1991 22 22 * <p> 23 23 * The HTTP monitor starts a web server ({@link HttpMonitorServer}) that receives exchanges 24 * of the HTTP proxy. These exchanges are logged using the {@link HttpMonitorLogManager}. The 25 * class assures that on shutdown e.g. caused by CTRL-C the server and the log manager are 26 * stopped correctly. 24 * of the HTTP proxy. In addition, it provides via get a unique ordering id for requests and 25 * responses that can be used by the proxies when creating the exchanges. The exchanges are 26 * logged using the {@link HttpMonitorLogManager}. The class assures that on shutdown e.g. caused 27 * by CTRL-C the server and the log manager are stopped correctly. 27 28 * </p> 28 29 * … … 50 51 */ 51 52 private HttpMonitorLogManager logManager; 53 54 /** 55 * the id generator used for getting new unique ids 56 */ 57 private IdGenerator idGenerator; 52 58 53 59 /** … … 92 98 93 99 try { 100 idGenerator = new SimpleIdGenerator(); 101 idGenerator.init(); 102 94 103 logManager = new HttpMonitorLogManager(logFileBaseDir); 95 104 logManager.init(); 96 105 97 Servlet servlet = new HttpMonitorServlet(logManager );106 Servlet servlet = new HttpMonitorServlet(logManager, idGenerator); 98 107 server = new HttpMonitorServer(port, servlet); 99 108 server.init(); … … 118 127 try { 119 128 Runtime.getRuntime().addShutdownHook(shutdownHook); 129 idGenerator.start(); 120 130 logManager.start(); 121 131 server.start(); … … 139 149 server.stop(); 140 150 logManager.stop(); 151 idGenerator.stop(); 141 152 142 153 server = null; 143 154 logManager = null; 155 idGenerator = null; 144 156 } 145 157 -
trunk/autoquest-httpmonitor/src/main/java/de/ugoe/cs/autoquest/httpmonitor/HttpMonitorServer.java
r1384 r1991 55 55 * 56 56 * @param port the port to listen on 57 * @param servlet t ehservlet to be deployed57 * @param servlet the servlet to be deployed 58 58 */ 59 59 public HttpMonitorServer(int port, Servlet servlet) { -
trunk/autoquest-httpmonitor/src/main/java/de/ugoe/cs/autoquest/httpmonitor/HttpMonitorServlet.java
r1561 r1991 33 33 * the servlet deployed in the web server that receives all recorded exchanges from the proxy. The 34 34 * exchanges are parsed and forwarded to the provided exchange handler. If an exchange can not 35 * be parsed it is discarded. Exchanges are only received via the POST HTTP method. 35 * be parsed it is discarded. Exchanges are only received via the POST HTTP method. Via the GET 36 * method, the servlet provides unique ordering ids as a header parameter in the response. The 37 * header parameter is named "X-AutoQUEST-OrderingId". For creating the ids it uses a provided id 38 * generator. 36 39 * </p> 37 40 * … … 47 50 */ 48 51 private transient HttpMonitorExchangeHandler exchangeHandler; 52 53 /** 54 * the id generator used to return new unique ids. 55 */ 56 private transient IdGenerator idGenerator; 49 57 50 58 /** 51 59 * <p> 52 60 * initializes the servlet with the exchange handler to which all exchanges shall be forwarded 61 * and the id generator to be used for returning new ids 53 62 * </p> 54 63 * 55 64 * @param exchangeHandler the exchange handler that shall receive all exchanges 65 * @param idGenerator the id generator used to generate new ids 56 66 */ 57 HttpMonitorServlet(HttpMonitorExchangeHandler exchangeHandler ) {67 HttpMonitorServlet(HttpMonitorExchangeHandler exchangeHandler, IdGenerator idGenerator) { 58 68 this.exchangeHandler = exchangeHandler; 69 this.idGenerator = idGenerator; 59 70 } 60 71 … … 87 98 } 88 99 } 100 101 /** 102 * this implements handling of doGet. For this servlet this means that we return a unique 103 * ordering id which can be used by proxies as ordering ids for requests and responses. 104 * 105 * (non-Javadoc) (non-Javadoc) 106 * @see HttpServlet#doGet(HttpServletRequest, HttpServletResponse) 107 */ 108 @Override 109 protected void doGet(HttpServletRequest request, HttpServletResponse response) 110 throws ServletException, IOException 111 { 112 response.setHeader("X-AutoQUEST-OrderingId", Long.toString(idGenerator.getNextId())); 113 } 89 114 90 115 } -
trunk/autoquest-httpmonitor/src/main/java/de/ugoe/cs/autoquest/httpmonitor/proxy/ExchangeListener.java
r1561 r1991 27 27 28 28 import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorExchangeHandler; 29 import de.ugoe.cs.autoquest.httpmonitor.IdGenerator; 29 30 import de.ugoe.cs.autoquest.plugin.http.logdata.Address; 30 31 import de.ugoe.cs.autoquest.plugin.http.logdata.Content; … … 46 47 * recording an exchange can not be done in one step. This is due to the fact, that the proxy 47 48 * servlet notifies different processing states for requests and response. An exchange listener 48 * records all these event. On the occurrence of the final event, it compiles an 49 * {@link HttpExchange} and forwards it to the exchange handler. 49 * records all these events. On the occurrence of the final event, it compiles an 50 * {@link HttpExchange} and forwards it to the exchange handler. When receiving the first event 51 * of the request, it also determines a respective ordering id. The same applies for the response. 52 * The ordering ids are retrieved from a provided id generator. 50 53 * </p> 51 54 * … … 63 66 /** 64 67 * <p> 68 * the id generator used to generate ordering ids for requests and responses 69 * </p> 70 */ 71 private IdGenerator idGenerator; 72 73 /** 74 * <p> 65 75 * the request of compiled exchange 66 76 * </p> 67 77 */ 68 78 private HttpServletRequest request; 79 80 /** 81 * <p> 82 * the ordering id for the request 83 * </p> 84 */ 85 private long requestOrderingId; 69 86 70 87 /** … … 81 98 */ 82 99 private HttpServletResponse response; 100 101 /** 102 * <p> 103 * the ordering id for the response 104 * </p> 105 */ 106 private long responseOrderingId; 83 107 84 108 /** … … 99 123 * <p> 100 124 * initialized the exchange listener with the exchange handler to forward compiled exchanges to 125 * and the id generator to be used for generating ordering ids for requests and responses 101 126 * </p> 102 127 * 103 128 * @param exchangeHandler the exchange handler to forward compiled exchanges to 104 */ 105 ExchangeListener(HttpMonitorExchangeHandler exchangeHandler) { 129 * @param idGenerator the id generator to used for generating ordering ids 130 */ 131 ExchangeListener(HttpMonitorExchangeHandler exchangeHandler, IdGenerator idGenerator) { 106 132 this.exchangeHandler = exchangeHandler; 133 this.idGenerator = idGenerator; 107 134 } 108 135 … … 123 150 lastUpdate = System.currentTimeMillis(); 124 151 this.request = request; 152 this.requestOrderingId = idGenerator.getNextId(); 125 153 } 126 154 … … 159 187 lastUpdate = System.currentTimeMillis(); 160 188 this.response = response; 189 this.responseOrderingId = idGenerator.getNextId(); 161 190 } 162 191 … … 239 268 exchange.setReceiver(address); 240 269 241 exchange.setRequest(map(request, eventObjectFactory));242 exchange.setResponse(map(response, eventObjectFactory));270 exchange.setRequest(map(request, requestOrderingId, eventObjectFactory)); 271 exchange.setResponse(map(response, responseOrderingId, eventObjectFactory)); 243 272 244 273 exchangeHandler.handleHttpExchange(exchange); … … 250 279 * </p> 251 280 */ 252 private HttpRequest map(HttpServletRequest request, ObjectFactory eventObjectFactory) { 281 private HttpRequest map(HttpServletRequest request, 282 long requestOrderingId, 283 ObjectFactory eventObjectFactory) 284 { 253 285 HttpRequest eventRequest = eventObjectFactory.createHttpRequest(); 286 eventRequest.setOrderingId(requestOrderingId); 254 287 eventRequest.setMethod(Method.fromValue(request.getMethod())); 255 288 eventRequest.setProtocol(Protocol.fromValue(request.getProtocol())); … … 311 344 * </p> 312 345 */ 313 private HttpResponse map(HttpServletResponse response, ObjectFactory eventObjectFactory) { 346 private HttpResponse map(HttpServletResponse response, 347 long responseOrderingId, 348 ObjectFactory eventObjectFactory) 349 { 314 350 HttpResponse eventResponse = eventObjectFactory.createHttpResponse(); 315 351 316 352 eventResponse.setStatus(BigInteger.valueOf(response.getStatus())); 353 eventResponse.setOrderingId(responseOrderingId); 317 354 318 355 Headers headers = eventObjectFactory.createHeaders(); -
trunk/autoquest-httpmonitor/src/main/java/de/ugoe/cs/autoquest/httpmonitor/proxy/ExchangeListenerManager.java
r1561 r1991 30 30 import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorException; 31 31 import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorExchangeHandler; 32 import de.ugoe.cs.autoquest.httpmonitor.IdGenerator; 32 33 import de.ugoe.cs.autoquest.plugin.http.logdata.Status; 33 34 import de.ugoe.cs.util.console.Console; … … 60 61 /** 61 62 * <p> 63 * the id generator used to generate ordering ids for requests and responses 64 * </p> 65 */ 66 private IdGenerator idGenerator; 67 68 /** 69 * <p> 62 70 * the mapping of requests handled by the proxy to the respective exchange listeners 63 71 * </p> … … 79 87 * 80 88 * @param exchangeHandler the exchange handler to be forwarded to the exchange listeners 81 */ 82 ExchangeListenerManager(HttpMonitorExchangeHandler exchangeHandler) { 89 * @param idGenerator the id generator to used for generating ordering ids 90 */ 91 ExchangeListenerManager(HttpMonitorExchangeHandler exchangeHandler, IdGenerator idGenerator) { 83 92 this.exchangeHandler = exchangeHandler; 93 this.idGenerator = idGenerator; 84 94 } 85 95 … … 219 229 if (listener == null) { 220 230 Console.traceln(Level.FINEST, "creating exchange listener for " + request); 221 listener = new ExchangeListener(exchangeHandler );231 listener = new ExchangeListener(exchangeHandler, idGenerator); 222 232 listeners.put(request, listener); 223 233 } -
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 } -
trunk/autoquest-httpmonitor/src/main/java/de/ugoe/cs/autoquest/httpmonitor/proxy/HttpMonitoringProxy.java
r1384 r1991 22 22 import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorLogManager; 23 23 import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorServer; 24 import de.ugoe.cs.autoquest.httpmonitor.IdGenerator; 24 25 import de.ugoe.cs.autoquest.httpmonitor.Runner; 25 26 import de.ugoe.cs.autoquest.httpmonitor.ShutdownHook; 27 import de.ugoe.cs.autoquest.httpmonitor.SimpleIdGenerator; 26 28 import de.ugoe.cs.util.console.Console; 27 29 … … 29 31 * <p> 30 32 * The HTTP monitory proxy monitor starts a web server ({@link HttpMonitorServer}) that receives 31 * proxie sHTTP messages and response. Each exchange of a request and a response is forwarded to33 * proxied HTTP messages and response. Each exchange of a request and a response is forwarded to 32 34 * an exchange handler. The exchange handler is either a local log manager 33 35 * ({@link HttpMonitorLogManager}) or a connection to an external and central HTTP monitor via 34 * an {@link HttpMonitorRemoteExchangeHandler}. The class ensures that on shutdown e.g. caused 36 * an {@link HttpMonitorRemoteExchangeHandler}. It also holds a reference to an id generator. If 37 * the exchanges are handled locally, then also a local {@link SimpleIdGenerator} is used. If 38 * the exchanges are send to a central HTTP monitor, then this monitor is used as id generator via 39 * an {@link HttpMonitorRemoteIdGenerator}. The class ensures that on shutdown e.g. caused 35 40 * by CTRL-C the server and all other requied components are stopped correctly. 36 41 * </p> … … 59 64 */ 60 65 private HttpMonitorExchangeHandler exchangeHandler; 66 67 /** 68 * the id generator used to generate order ids for the requests and responses 69 */ 70 private IdGenerator idGenerator; 61 71 62 72 /** … … 170 180 exchangeHandler = 171 181 new HttpMonitorRemoteExchangeHandler(httpMonitorServer, httpMonitorPort); 182 183 idGenerator = new HttpMonitorRemoteIdGenerator(httpMonitorServer, httpMonitorPort); 172 184 } 173 185 else { 174 186 Console.println("storing logs locally into directory " + logFileBaseDir); 175 187 exchangeHandler = new HttpMonitorLogManager(logFileBaseDir); 188 idGenerator = new SimpleIdGenerator(); 176 189 } 177 190 } … … 187 200 188 201 exchangeHandler.init(); 189 190 exchangeListenerManager = new ExchangeListenerManager(exchangeHandler); 202 idGenerator.init(); 203 204 exchangeListenerManager = new ExchangeListenerManager(exchangeHandler, idGenerator); 191 205 exchangeListenerManager.init(); 192 206 … … 205 219 @Override 206 220 public synchronized void start() { 207 if ((exchangeHandler == null) || (exchangeListenerManager == null) || (server == null)) { 221 if ((exchangeHandler == null) || (idGenerator == null) || 222 (exchangeListenerManager == null) || (server == null)) 223 { 208 224 throw new IllegalStateException("not initialized."); 209 225 } … … 212 228 Runtime.getRuntime().addShutdownHook(shutdownHook); 213 229 exchangeHandler.start(); 230 idGenerator.start(); 214 231 exchangeListenerManager.start(); 215 232 server.start(); … … 226 243 @Override 227 244 public synchronized void stop() { 228 if ((exchangeHandler == null) || (exchangeListenerManager == null) || (server == null)) { 245 if ((exchangeHandler == null) || (idGenerator == null) || 246 (exchangeListenerManager == null) || (server == null)) 247 { 229 248 throw new IllegalStateException("not initialized."); 230 249 } … … 233 252 server.stop(); 234 253 exchangeListenerManager.stop(); 254 idGenerator.stop(); 235 255 exchangeHandler.stop(); 236 256 237 257 server = null; 238 258 exchangeListenerManager = null; 259 idGenerator = null; 239 260 exchangeHandler = null; 240 261 }
Note: See TracChangeset
for help on using the changeset viewer.