private void submitBatch(boolean force) { if (currentBatch.isEmpty()) { return; } if (isSubmitBatch(force)) { // Synchronized block is to ensure only one of the writers will actually write the batch. synchronized (currentBatch) { // If two threads pass the if condition and then one of them actually writes, // other will do the same since they were blocked sync block. this ensures that // others will not post it even if multiple threads were to wait at sync block at same time // https://en.wikipedia.org/wiki/Double-checked_locking if (isSubmitBatch(force)) { logger.debug("Submitting a batch of " + currentBatch.size() + " requests to GA"); httpClient.postBatch(new HttpBatchRequest().setUrl(config.getBatchUrl()).setRequests(currentBatch)); currentBatch.clear(); } } } }
@Override public HttpBatchResponse postBatch(HttpBatchRequest req) { HttpBatchResponse resp = new HttpBatchResponse(); CloseableHttpResponse httpResp = null; try { List<List<NameValuePair>> listOfReqPairs = req.getRequests().stream().map(this::createNameValuePairs).collect(Collectors.toList()); httpResp = execute(req.getUrl(), new BatchUrlEncodedFormEntity(listOfReqPairs)); resp.setStatusCode(httpResp.getStatusLine().getStatusCode()); } catch (Exception e) { if (e instanceof UnknownHostException) { logger.warn("Couldn't connect to Google Analytics. Internet may not be available. " + e.toString()); } else { logger.warn("Exception while sending the Google Analytics tracker request " + req, e); } } finally { EntityUtils.consumeQuietly(httpResp.getEntity()); try { httpResp.close(); } catch (Exception e2) { // ignore } } return resp; } }
@Override public HttpBatchResponse postBatch(HttpBatchRequest req) { HttpBatchResponse resp = new HttpBatchResponse(); CloseableHttpResponse httpResp = null; try { List<List<NameValuePair>> listOfReqPairs = req.getRequests().stream().map(this::createNameValuePairs).collect(Collectors.toList()); httpResp = execute(req.getUrl(), new BatchUrlEncodedFormEntity(listOfReqPairs)); resp.setStatusCode(httpResp.getStatusLine().getStatusCode()); } catch (Exception e) { if (e instanceof UnknownHostException) { logger.warn("Couldn't connect to Google Analytics. Internet may not be available. " + e.toString()); } else { logger.warn("Exception while sending the Google Analytics tracker request " + req, e); } } finally { EntityUtils.consumeQuietly(httpResp.getEntity()); try { httpResp.close(); } catch (Exception e2) { // ignore } } return resp; } }
private void submitBatch(boolean force) { if (currentBatch.isEmpty()) { return; } if (isSubmitBatch(force)) { // Synchronized block is to ensure only one of the writers will actually write the batch. synchronized (currentBatch) { // If two threads pass the if condition and then one of them actually writes, // other will do the same since they were blocked sync block. this ensures that // others will not post it even if multiple threads were to wait at sync block at same time // https://en.wikipedia.org/wiki/Double-checked_locking if (isSubmitBatch(force)) { logger.debug("Submitting a batch of " + currentBatch.size() + " requests to GA"); httpClient.postBatch(new HttpBatchRequest().setUrl(config.getBatchUrl()).setRequests(currentBatch)); currentBatch.clear(); } } } }