public static final void main(String[] args) throws Exception { String uri = args[0]; int reqNum = Integer.parseInt(args[1]); int reqTerm = Integer.parseInt(args[2]); try { // new ClientSimple().request1(uri, reqNum, reqTerm); new ClientSimple().request2(uri, reqNum, reqTerm); } catch (Exception e) { System.out.println("ERROR:" + e.getMessage()); } }
protected Exception postJsonToPipelineWithRetry( String endpoint, List docs, ArrayList<String> mutable, Exception lastExc, int requestId) throws Exception { Exception retryAfterException = null; try { postJsonToPipeline(endpoint, docs, requestId); if (lastExc != null) log.info( "Re-try request " + requestId + " to " + endpoint + " succeeded after seeing a " + lastExc.getMessage()); } catch (Exception exc) { log.warn("Failed to send request " + requestId + " to '" + endpoint + "' due to: " + exc); if (mutable.size() > 1) { // try another endpoint but update the cloned list to avoid re-hitting the one having an // error if (log.isDebugEnabled()) log.debug("Will re-try failed request " + requestId + " on next endpoint in the list"); mutable.remove(endpoint); retryAfterException = exc; } else { // no other endpoints to try ... brief wait and then retry log.warn( "No more endpoints available to try ... will retry to send request " + requestId + " to " + endpoint + " after waiting 1 sec"); try { Thread.sleep(1000); } catch (InterruptedException ignore) { Thread.interrupted(); } // note we want the exception to propagate from here up the stack since we re-tried and it // didn't work postJsonToPipeline(endpoint, docs, requestId); log.info("Re-try request " + requestId + " to " + endpoint + " succeeded"); retryAfterException = null; // return success condition } } return retryAfterException; }
public void request1(String uri, int reqNum, int reqTerm) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); if (reqNum == 0) { System.out.println( String.format("request to %s infinite times with term '%d' ms", uri, reqTerm)); } else { System.out.println( String.format("request to %s '%d' times with term '%d' ms", uri, reqNum, reqTerm)); } int i = 0, tick = 0; HttpGet httpGet = new HttpGet(uri); CloseableHttpResponse response; HttpEntity entity; while (true) { usleep(reqTerm); tick = (int) (Math.random() * 10) % 2; if (tick == 0) { continue; } System.out.println("request " + httpGet.getURI()); response = httpclient.execute(httpGet); System.out.println("--> response status = " + response.getStatusLine()); // response handler try { entity = response.getEntity(); EntityUtils.consume(entity); } catch (Exception e) { System.out.println(" --> http fail:" + e.getMessage()); } finally { // Thread.sleep(5000); //테스트에만 썼다. close 지연시키려고. response.close(); } System.out.println("----------------------------------------"); if (reqNum != 0 && reqNum < ++i) { break; } } }