@Nullable private static TfsException createTfsExceptionFromThrowable(Throwable throwable) { if (throwable instanceof ConnectException) { return new ConnectionFailedException(throwable); } if (throwable instanceof UnknownHostException) { return new HostNotFoundException(throwable); } if (throwable instanceof NoHttpResponseException) { return new HostNotFoundException(throwable); } if (throwable instanceof SSLHandshakeException) { return new SSLConnectionException((SSLHandshakeException)throwable); } if (throwable instanceof SOAPProcessingException) { return new ConnectionFailedException(throwable, TFSBundle.message("invalid.soap.response")); } if (throwable instanceof SocketException) { return new ConnectionFailedException(throwable); } if (throwable instanceof SocketTimeoutException || throwable instanceof ConnectTimeoutException) { return new ConnectionTimeoutException(throwable); } return null; }
protected HttpMethodRetryHandler createRetryTwiceHandler() { return new HttpMethodRetryHandler() { @Override public boolean retryMethod(final HttpMethod method, final IOException exception, int executionCount) { if (executionCount >= 2) { // Do not retry if over max retry count return false; } if (exception instanceof NoHttpResponseException) { // Retry if the server dropped connection on us return true; } if (!method.isRequestSent()) { // Retry if the request has not been sent fully or // if it's OK to retry methods that have been sent return true; } // otherwise do not retry return false; } }; }
private HttpMethodRetryHandler createRetryTwiceHandler() { return new HttpMethodRetryHandler() { @Override public boolean retryMethod(final HttpMethod method, final IOException exception, int executionCount) { if (executionCount >= 2) { // Do not retry if over max retry count return false; } if (exception instanceof NoHttpResponseException) { // Retry if the server dropped connection on us return true; } if (!method.isRequestSent()) { // Retry if the request has not been sent fully or // if it's OK to retry methods that have been sent return true; } // otherwise do not retry return false; } }; }
private static CloseableHttpClient createHttpClient(int maxTotal, int maxPerRoute, int maxRoute, String hostname, int port) { ConnectionSocketFactory plainsf = PlainConnectionSocketFactory .getSocketFactory(); LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory .getSocketFactory(); Registry<ConnectionSocketFactory> registry = RegistryBuilder .<ConnectionSocketFactory>create() .register("http", plainsf) .register("https", sslsf) .build(); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager( registry); // 将最大连接数增加 cm.setMaxTotal(maxTotal); // 将每个路由基础的连接增加 cm.setDefaultMaxPerRoute(maxPerRoute); // 将目标主机的最大连接数增加 HttpHost httpHost = new HttpHost(hostname, port); cm.setMaxPerRoute(new HttpRoute(httpHost), maxRoute); // 请求重试处理 HttpRequestRetryHandler httpRequestRetryHandler = (exception, executionCount, context) -> { if (executionCount >= 5) {// 如果已经重试了5次,就放弃 return false; } if (exception instanceof NoHttpResponseException) {// 如果服务器丢掉了连接,那么就重试 return true; } if (exception instanceof SSLHandshakeException) {// 不要重试SSL握手异常 return false; } if (exception instanceof InterruptedIOException) {// 超时 return false; } if (exception instanceof UnknownHostException) {// 目标服务器不可达 return false; } if (exception instanceof ConnectTimeoutException) {// 连接被拒绝 return false; } if (exception instanceof SSLException) {// SSL握手异常 return false; } HttpClientContext clientContext = HttpClientContext.adapt(context); HttpRequest request = clientContext.getRequest(); // 如果请求是幂等的,就再次尝试 if (!(request instanceof HttpEntityEnclosingRequest)) { return true; } return false; }; CloseableHttpClient httpClient = HttpClients.custom() .setConnectionManager(cm) .setRetryHandler(httpRequestRetryHandler) .build(); return httpClient; }
/** * @return A HttpMethodRetryHandler with given number of retries. */ public static HttpMethodRetryHandler getHttpMethodRetryHandler(final int retryCount) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Initializing new HttpMethodRetryHandler with retry count " + retryCount); } return new HttpMethodRetryHandler() { @Override public boolean retryMethod(final HttpMethod method, final IOException exception, final int executionCount) { if (executionCount >= retryCount) { // Do not retry if over max retry count return false; } if (exception instanceof NoHttpResponseException) { // Retry if the server dropped connection on us return true; } if (!method.isRequestSent()) { // Retry if the request has not been sent fully or // if it's OK to retry methods that have been sent return true; } // otherwise do not retry return false; } }; }
/** * @return A HttpMethodRetryHandler with given number of retries. */ public static HttpMethodRetryHandler getHttpMethodRetryHandler(final int retryCount) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Initializing new HttpMethodRetryHandler with retry count " + retryCount); } return new HttpMethodRetryHandler() { @Override public boolean retryMethod(final HttpMethod method, final IOException exception, int executionCount) { if (executionCount >= retryCount) { // Do not retry if over max retry count return false; } if (exception instanceof NoHttpResponseException) { // Retry if the server dropped connection on us return true; } if (!method.isRequestSent()) { // Retry if the request has not been sent fully or // if it's OK to retry methods that have been sent return true; } // otherwise do not retry return false; } }; }
public HttpTemplateDownloader(final StorageLayer storageLayer, final String downloadUrl, final String toDir, final DownloadCompleteCallback callback, final long maxTemplateSizeInBytes, final String user, final String password, final Proxy proxy, final ResourceType resourceType) { _storage = storageLayer; this.downloadUrl = downloadUrl; setToDir(toDir); status = TemplateDownloader.Status.NOT_STARTED; this.resourceType = resourceType; this.maxTemplateSizeInBytes = maxTemplateSizeInBytes; totalBytes = 0; client = new HttpClient(s_httpClientManager); myretryhandler = (method, exception, executionCount) -> { if (executionCount >= 2) { // Do not retry if over max retry count return false; } if (exception instanceof NoHttpResponseException) { // Retry if the server dropped connection on us return true; } if (!method.isRequestSent()) { // Retry if the request has not been sent fully or // if it's OK to retry methods that have been sent return true; } // otherwise do not retry return false; }; try { request = new GetMethod(downloadUrl); request.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, myretryhandler); completionCallback = callback; this.request.setFollowRedirects(true); final File f = File.createTempFile("dnld", "tmp_", new File(toDir)); if (_storage != null) { _storage.setWorldReadableAndWriteable(f); } toFile = f.getAbsolutePath(); final Pair<String, Integer> hostAndPort = UriUtils.validateUrl(downloadUrl); if (proxy != null) { client.getHostConfiguration().setProxy(proxy.getHost(), proxy.getPort()); if (proxy.getUserName() != null) { final Credentials proxyCreds = new UsernamePasswordCredentials(proxy.getUserName(), proxy.getPassword()); client.getState().setProxyCredentials(AuthScope.ANY, proxyCreds); } } if (user != null && password != null) { client.getParams().setAuthenticationPreemptive(true); final Credentials defaultcreds = new UsernamePasswordCredentials(user, password); client.getState().setCredentials(new AuthScope(hostAndPort.first(), hostAndPort.second(), AuthScope.ANY_REALM), defaultcreds); s_logger.info("Added username=" + user + ", password=" + password + "for host " + hostAndPort.first() + ":" + hostAndPort.second()); } else { s_logger.info("No credentials configured for host=" + hostAndPort.first() + ":" + hostAndPort.second()); } } catch (final IllegalArgumentException iae) { errorString = iae.getMessage(); status = TemplateDownloader.Status.UNRECOVERABLE_ERROR; inited = false; } catch (final Exception ex) { errorString = "Unable to start download -- check url? "; status = TemplateDownloader.Status.UNRECOVERABLE_ERROR; s_logger.warn("Exception in constructor -- " + ex.toString()); } }
/** * Private {@link HttpClient} initialization. */ @PostConstruct private final void afterPropertiesSet() { // Client is higher in the hierarchy than manager so set the parameters here final HttpClientParams clientParams = new HttpClientParams(); clientParams.setConnectionManagerClass(connectionManager.getClass()); clientParams.setConnectionManagerTimeout(connectionTimeout); clientParams.setSoTimeout(readTimeout); clientParams.setParameter("http.connection.timeout", new Integer( connectionTimeout)); // A retry handler for when a connection fails clientParams.setParameter(HttpMethodParams.RETRY_HANDLER, new HttpMethodRetryHandler() { @Override public boolean retryMethod(final HttpMethod method, final IOException exception, final int executionCount) { if (executionCount >= retryCount) { // Do not retry if over max retry count return false; } if (instanceOf(exception, NoHttpResponseException.class)) { // Retry if the server dropped connection on us return true; } if (instanceOf(exception, SocketException.class)) { // Retry if the server reset connection on us return true; } if (instanceOf(exception, SocketTimeoutException.class)) { // Retry if the read timed out return true; } if (!method.isRequestSent()) { // Retry if the request has not been sent fully or // if it's OK to retry methods that have been sent return true; } // otherwise do not retry return false; } }); httpClient.setParams(clientParams); final HttpConnectionManagerParams connectionManagerParams = connectionManager .getParams(); connectionManagerParams.setDefaultMaxConnectionsPerHost(maxConnectionsPerHost); connectionManager.setParams(connectionManagerParams); }