@Before public void setUp() throws Exception { this.client = HttpClients.custom() .setServiceUnavailableRetryStrategy(new DefaultServiceUnavailableRetryStrategy(3, 2000)) .setDefaultRequestConfig(RequestConfig.custom() .setSocketTimeout(10000) .setConnectTimeout(10000) .setConnectionRequestTimeout(10000) .build()).build(); this.om = new ObjectMapper(); this.wsClient = ClientManager.createClient(); wsClient.getProperties().put(ClientProperties.HANDSHAKE_TIMEOUT, 10000); if (System.getProperty(TRAVIS_ENV) != null) { System.out.println("waiting for Travis machine"); waitUrlAvailable(String.format("http://%s:%d/api?name=foo", LOCALHOST, PORT)); // Thread.sleep(1); // Ugly sleep to debug travis } }
public static HttpClientBuilder getHttpClientBuilder(final String charset, String proxyIp, int proxyPort) { HttpClientBuilder builder = HttpClients.custom(); Charset chartset = charset == null ? Charset.forName(CHARSET_UTF8) : Charset.forName(charset); ConnectionConfig.Builder connBuilder = ConnectionConfig.custom().setCharset(chartset); RequestConfig.Builder reqBuilder = RequestConfig.custom(); reqBuilder.setExpectContinueEnabled(false); reqBuilder.setSocketTimeout(10 * 60 * 1000); reqBuilder.setConnectTimeout(10 * 60 * 1000); reqBuilder.setMaxRedirects(10); if (StringUtils.isNotBlank(proxyIp) && proxyPort > 0) { logger.info("using proxy {}:{} to request ", proxyIp, String.valueOf(proxyPort)); HttpHost proxy = new HttpHost(proxyIp, proxyPort); reqBuilder.setProxy(proxy); } ServiceUnavailableRetryStrategy serviceUnavailableRetryStrategy = new DefaultServiceUnavailableRetryStrategy( 3, 3000); builder.setServiceUnavailableRetryStrategy(serviceUnavailableRetryStrategy); // 模拟浏览器,解决一些服务器程序只允许浏览器访问的问题 builder.setUserAgent( "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0"); builder.setDefaultRequestConfig(reqBuilder.build()); builder.setDefaultConnectionConfig(connBuilder.build()); builder.setConnectionManager(getConnectionManager()); // HostnameVerifier allowAllHostnameVerifier = // NoopHostnameVerifier.INSTANCE; // builder.setSSLHostnameVerifier(allowAllHostnameVerifier); return builder; }
public void start() { Runnable thread = new Runnable() { /* * (non-Javadoc) * * @see java.lang.Thread#run() */ @Override public void run() { Tracker tracker = new Tracker(getUser(), getHostName(), getIp(), getOs(), System.currentTimeMillis(), getInitiator()); AutoRetryHttpClient client = new AutoRetryHttpClient( new DefaultServiceUnavailableRetryStrategy(3, 3000)); HttpPost request = createRequest(tracker); HttpHost httpHost = createHttpHost(); try { HttpResponse resp = client.execute(httpHost, request); int code = resp.getStatusLine().getStatusCode(); if (HttpResponseStatus.OK.code() == code) { InputStream is = null; try { is = resp.getEntity().getContent(); is = new BufferedInputStream(is); InputStreamReader reader = new InputStreamReader(is); JsonStreamParser parser = new JsonStreamParser( reader); while (parser.hasNext()) { JsonElement json = parser.next(); LOGGER.debug("Tracking data sent: " + json); } } finally { if (is != null) { is.close(); } } } else { LOGGER.error(ERROR_FAILED_TO_SEND); LOGGER.debug("Response code: " + code); } } catch (IOException e) { LOGGER.warn(ERROR_FAILED_TO_SEND); } } }; m_scheduledService.scheduleAtFixedRate(thread, 0, m_interval, TimeUnit.SECONDS); }