public HttpClientTransmitterImpl() { protocolMap = new TreeMap<String,Protocol>(); protocolMap.put(HTTP_SCHEME_NAME, httpProtocol); protocolMap.put(HTTPS_SCHEME_NAME, httpsProtocol); httpClient = new HttpClient(); httpClient.setHttpConnectionManager(new MultiThreadedHttpConnectionManager()); httpMethodFactory = new StandardHttpMethodFactoryImpl(); jsonErrorSerializer = new ExceptionJsonSerializer(); // Create an HTTP Proxy Host if appropriate system properties are set httpProxyHost = HttpClientHelper.createProxyHost("http.proxyHost", "http.proxyPort", DEFAULT_HTTP_PORT); httpProxyCredentials = HttpClientHelper.createProxyCredentials("http.proxyUser", "http.proxyPassword"); httpAuthScope = createProxyAuthScope(httpProxyHost); // Create an HTTPS Proxy Host if appropriate system properties are set httpsProxyHost = HttpClientHelper.createProxyHost("https.proxyHost", "https.proxyPort", DEFAULT_HTTPS_PORT); httpsProxyCredentials = HttpClientHelper.createProxyCredentials("https.proxyUser", "https.proxyPassword"); httpsAuthScope = createProxyAuthScope(httpsProxyHost); }
protected HttpClient constructHttpClient() { MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); HttpClient httpClient = new HttpClient(connectionManager); HttpClientParams params = httpClient.getParams(); params.setBooleanParameter(HttpConnectionParams.TCP_NODELAY, true); params.setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, true); if (socketTimeout != null) { params.setSoTimeout(socketTimeout); } HttpConnectionManagerParams connectionManagerParams = httpClient.getHttpConnectionManager().getParams(); connectionManagerParams.setMaxTotalConnections(maxTotalConnections); connectionManagerParams.setDefaultMaxConnectionsPerHost(maxHostConnections); connectionManagerParams.setConnectionTimeout(connectionTimeout); return httpClient; }
/** * Gets the maximum number of connections to be used for a particular host config. If * the value has not been specified for the given host the default value will be * returned. * * @param hostConfiguration The host config. * @return The maximum number of connections to be used for the given host config. * * @see #MAX_HOST_CONNECTIONS */ public int getMaxConnectionsPerHost(HostConfiguration hostConfiguration) { Map m = (Map) getParameter(MAX_HOST_CONNECTIONS); if (m == null) { // MAX_HOST_CONNECTIONS have not been configured, using the default value return MultiThreadedHttpConnectionManager.DEFAULT_MAX_HOST_CONNECTIONS; } else { Integer max = (Integer) m.get(hostConfiguration); if (max == null && hostConfiguration != HostConfiguration.ANY_HOST_CONFIGURATION) { // the value has not been configured specifically for this host config, // use the default value return getMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION); } else { return ( max == null ? MultiThreadedHttpConnectionManager.DEFAULT_MAX_HOST_CONNECTIONS : max.intValue() ); } } }
public static InputStream getInputStreamFromUrl(final String url, final String user, final String password) { try { final Pair<String, Integer> hostAndPort = validateUrl(url); final HttpClient httpclient = new HttpClient(new MultiThreadedHttpConnectionManager()); if ((user != null) && (password != null)) { httpclient.getParams().setAuthenticationPreemptive(true); final Credentials defaultcreds = new UsernamePasswordCredentials(user, password); httpclient.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()); } // Execute the method. final GetMethod method = new GetMethod(url); final int statusCode = httpclient.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { s_logger.error("Failed to read from URL: " + url); return null; } return method.getResponseBodyAsStream(); } catch (final Exception ex) { s_logger.error("Failed to read from URL: " + url); return null; } }
private HttpClient getHttpClient() { if (s_client == null) { final MultiThreadedHttpConnectionManager mgr = new MultiThreadedHttpConnectionManager(); mgr.getParams().setDefaultMaxConnectionsPerHost(4); // TODO make it configurable mgr.getParams().setMaxTotalConnections(1000); s_client = new HttpClient(mgr); final HttpClientParams clientParams = new HttpClientParams(); clientParams.setSoTimeout(ClusterServiceAdapter.ClusterMessageTimeOut.value() * 1000); s_client.setParams(clientParams); } return s_client; }
public DiamondSDKManagerImpl(int connection_timeout, int require_timeout) throws IllegalArgumentException { if (connection_timeout < 0) throw new IllegalArgumentException("连接超时时间设置必须大于0[单位(毫秒)]!"); if (require_timeout < 0) throw new IllegalArgumentException("请求超时时间设置必须大于0[单位(毫秒)]!"); this.connection_timeout = connection_timeout; this.require_timeout = require_timeout; int maxHostConnections = 50; MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); connectionManager.getParams().setDefaultMaxConnectionsPerHost(maxHostConnections); connectionManager.getParams().setStaleCheckingEnabled(true); this.client = new HttpClient(connectionManager); // 设置连接超时时间 client.getHttpConnectionManager().getParams().setConnectionTimeout(this.connection_timeout); // 设置读超时为1分钟 client.getHttpConnectionManager().getParams().setSoTimeout(60 * 1000); client.getParams().setContentCharset("UTF-8"); log.info("设置连接超时时间为: " + this.connection_timeout + "毫秒"); }
private synchronized void init() { client = new HttpClient(new MultiThreadedHttpConnectionManager()); HttpClientParams params = client.getParams(); if (encode != null && !encode.trim().equals("")) { params.setParameter("http.protocol.content-charset", encode); params.setContentCharset(encode); } if (timeout > 0) { params.setSoTimeout(timeout); } if (null != proxy) { HostConfiguration hc = new HostConfiguration(); hc.setProxy(proxy.getHost(), proxy.getPort()); client.setHostConfiguration(hc); client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(proxy.getUser(), proxy.getPassword())); } initialized = true; }
@Bean public SAMLProcessorImpl processor() { HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager()); ArtifactResolutionProfileImpl artifactResolutionProfile = new ArtifactResolutionProfileImpl(httpClient); HTTPSOAP11Binding soapBinding = new HTTPSOAP11Binding(parserPool()); artifactResolutionProfile.setProcessor(new SAMLProcessorImpl(soapBinding)); VelocityEngine velocityEngine = VelocityFactory.getEngine(); Collection<SAMLBinding> bindings = new ArrayList<>(); bindings.add(new HTTPRedirectDeflateBinding(parserPool())); bindings.add(new HTTPPostBinding(parserPool(), velocityEngine)); bindings.add(new HTTPArtifactBinding(parserPool(), velocityEngine, artifactResolutionProfile)); bindings.add(new HTTPSOAP11Binding(parserPool())); bindings.add(new HTTPPAOS11Binding(parserPool())); return new SAMLProcessorImpl(bindings); }
/** * @param multiThreadedHttpConnectionManager * The * @link {@link MultiThreadedHttpConnectionManager} that the fulltext search * engine will use * @throws FullTextSearchException * If an error occured */ @Autowired public FullTextSearchEngine( @Qualifier("multiThreadedHttpConnectionManager") MultiThreadedHttpConnectionManager multiThreadedHttpConnectionManager) throws FullTextSearchException { Assert.notNull(multiThreadedHttpConnectionManager, "multiThreadedHttpConnectionManager can not be null"); HttpConnectionManagerParams p = new HttpConnectionManagerParams(); p.setSoTimeout(0); p.setConnectionTimeout(0); multiThreadedHttpConnectionManager.setParams(p); this.httpClient = new HttpClient(multiThreadedHttpConnectionManager); if (this.httpClient == null) { throw new FullTextSearchException( "Can not instanciate http client with multiThreadedHttpConnectionManager : " + multiThreadedHttpConnectionManager); } }
/** * @param solrUrl * The solr URL of the server to connect */ @Autowired public SolrClient(@Qualifier("fulltextSearchUrl") String solrUrl, @Qualifier("multiThreadedHttpConnectionManager") MultiThreadedHttpConnectionManager multiThreadedHttpConnectionManager) { try { Assert.notNull(solrUrl, "solrClient does not accept null solrUrl"); Assert .notNull(multiThreadedHttpConnectionManager, "solrClient does not accept null multiThreadedHttpConnectionManager"); this.multiThreadedHttpConnectionManager = multiThreadedHttpConnectionManager; this.server = new CommonsHttpSolrServer(new URL(solrUrl), new HttpClient(multiThreadedHttpConnectionManager)); this.URL = !solrUrl.endsWith("/") ? solrUrl + "/" : solrUrl ; logger.info("connecting to solr on " + this.URL + "..."); } catch (MalformedURLException e) { throw new RuntimeException("Error connecting to Solr! : " + e.getMessage()); } }
@Test public void testIsAlive() { assertTrue(fullTextSearchEngine.isAlive()); FullTextSearchEngine fullTextSearchEngineTobadUrl = new FullTextSearchEngine( new MultiThreadedHttpConnectionManager()); IsolrClient mockSolClient = EasyMock.createMock(IsolrClient.class); EasyMock.expect(mockSolClient.isServerAlive()).andReturn(false); EasyMock.replay(mockSolClient); fullTextSearchEngineTobadUrl.setSolrClient(mockSolClient); assertFalse(fullTextSearchEngineTobadUrl.isAlive()); EasyMock.verify(mockSolClient); FullTextSearchEngine fullTextSearchEngineWithNullSolrClient = new FullTextSearchEngine( new MultiThreadedHttpConnectionManager()); fullTextSearchEngineWithNullSolrClient.setSolrClient(null); assertFalse(fullTextSearchEngineWithNullSolrClient.isAlive()); }
/** * * @throws Exception . */ private void init() throws Exception { httpClientManager = new MultiThreadedHttpConnectionManager(); HttpConnectionManagerParams params = httpClientManager.getParams(); params.setStaleCheckingEnabled(true); params.setMaxTotalConnections(1000); params.setDefaultMaxConnectionsPerHost(500); params.setConnectionTimeout(2000); params.setSoTimeout(3000); /** 设置从连接池中获取连接超时。*/ HttpClientParams clientParams = new HttpClientParams(); clientParams.setConnectionManagerTimeout(1000); httpClient = new HttpClient(clientParams, httpClientManager); }
private void initConfigurationContext() throws Exception { HttpConnectionManager multiThreadedHttpConnectionManager = new MultiThreadedHttpConnectionManager(); HttpClient httpClient = new HttpClient(multiThreadedHttpConnectionManager); File configFile = new File(DEFAULT_AXIS2_XML); if (!configFile.exists()) { configurationContext = ConfigurationContextFactory.createDefaultConfigurationContext(); configurationContext.setProperty(HTTPConstants.DEFAULT_MAX_CONNECTIONS_PER_HOST, MAX_CONNECTIONS_PER_HOST); } else { configurationContext = ConfigurationContextFactory. createConfigurationContextFromFileSystem(DEFAULT_CLIENT_REPO, DEFAULT_AXIS2_XML); } configurationContext.setProperty(HTTPConstants.CACHED_HTTP_CLIENT, httpClient); configurationContext.setProperty(HTTPConstants.REUSE_HTTP_CLIENT, Constants.VALUE_TRUE); Map<String, TransportOutDescription> transportsOut = configurationContext.getAxisConfiguration().getTransportsOut(); for (TransportOutDescription transportOutDescription : transportsOut.values()) { if (Constants.TRANSPORT_HTTP.equals(transportOutDescription.getName()) || Constants.TRANSPORT_HTTPS.equals(transportOutDescription.getName())) { transportOutDescription.getSender().init(configurationContext, transportOutDescription); } } }
/** * A HttpClient with basic authentication and no host or port setting. Can only be used to retrieve absolute URLs * * @param user * can be NULL * @param password * can be NULL * @return HttpClient */ public static HttpClient getHttpClientInstance(String user, String password) { HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); HttpConnectionParams params = connectionManager.getParams(); // wait max 10 seconds to establish connection params.setConnectionTimeout(10000); // a read() call on the InputStream associated with this Socket // will block for only this amount params.setSoTimeout(10000); HttpClient c = new HttpClient(connectionManager); // use basic authentication if available if (user != null && user.length() > 0) { AuthScope authScope = new AuthScope(null, -1, null); Credentials credentials = new UsernamePasswordCredentials(user, password); c.getState().setCredentials(authScope, credentials); } return c; }
public void shutdown() { super.shutdown(); try { AlfrescoSolrDataModel.getInstance().close(); SOLRAPIClientFactory.close(); MultiThreadedHttpConnectionManager.shutdownAll(); boolean testcase = Boolean.parseBoolean(System.getProperty("alfresco.test", "false")); if(testcase) { if (!scheduler.isShutdown()) { scheduler.pauseAll(); scheduler.shutdown(); } } } catch(Exception e) { log.info("", e); } }
/** * Load proxy configuration when proxy config has changed */ private void loadProxyConfig(){ connectionManager = new MultiThreadedHttpConnectionManager(); HttpConnectionManagerParams params = new HttpConnectionManagerParams(); params.setSoTimeout(proxyConfig.getSoTimeout()); params.setConnectionTimeout(proxyConfig.getConnectionTimeout()); params.setMaxTotalConnections(proxyConfig.getMaxTotalConnections()); params.setDefaultMaxConnectionsPerHost(proxyConfig .getDefaultMaxConnectionsPerHost()); connectionManager.setParams(params); httpClient = new HttpClient(connectionManager); configureCallbacks(); }
@Override public void init(ServletConfig config) throws ServletException { super.init(config); // Create a pool of HTTP connections to use to get the favicons client = new HttpClient(new MultiThreadedHttpConnectionManager()); HttpConnectionManagerParams params = client.getHttpConnectionManager().getParams(); params.setConnectionTimeout(2000); params.setSoTimeout(2000); // Load the default favicon to use when no favicon was found of a remote host try { URL resource = config.getServletContext().getResource("/images/server_16x16.gif"); defaultBytes = getImage(resource.toString()); } catch (MalformedURLException e) { e.printStackTrace(); } // Initialize caches. missesCache = CacheFactory.createCache("Favicon Misses"); hitsCache = CacheFactory.createCache("Favicon Hits"); }
/** * * Public Section * */ public void connectHTTP(String strUser, String strPass, String strHost, boolean insecure) { //Connect WebDAV with credentials hostConfig = new HostConfiguration(); hostConfig.setHost(strHost); connectionManager = new MultiThreadedHttpConnectionManager(); connectionManagerParams = new HttpConnectionManagerParams(); connectionManagerParams.setMaxConnectionsPerHost(hostConfig, this.intMaxConnections); connectionManager.setParams(connectionManagerParams); client = new HttpClient(connectionManager); creds = new UsernamePasswordCredentials(strUser, strPass); client.getState().setCredentials(AuthScope.ANY, creds); client.setHostConfiguration(hostConfig); if (insecure) { Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443); Protocol.registerProtocol("https", easyhttps); } Status.print("WebDav Connection generated"); }
/** * distroy the ConfigurationContext */ @Override public void destroy() { //stoping listner manager try { if (configContext != null) { configContext.terminate(); } } catch (AxisFault axisFault) { log.info(axisFault.getMessage()); } try { super.destroy(); } catch (Exception e) { log.info(e.getMessage()); } // AXIS2-4898: MultiThreadedHttpConnectionManager starts a thread that is not stopped by the // shutdown of the connection manager. If we want to avoid a resource leak, we need to call // shutdownAll here. MultiThreadedHttpConnectionManager.shutdownAll(); }
public RobotsTxtParser(int timeout, int cacheSize) { robotsTxtCache = ExpiringLRUMap.create(cacheSize); int timeToWaitForResponse = (int) (timeout * DateUtils.MILLIS_PER_SECOND); HttpConnectionManagerParams hmcp = new HttpConnectionManagerParams(); hmcp.setSoTimeout(timeToWaitForResponse); hmcp.setConnectionTimeout(timeToWaitForResponse); hcm = new MultiThreadedHttpConnectionManager(); hcm.setParams(hmcp); client = new HttpClient(hcm); String proxyHost = FWProps.getStringProperty("http.proxyHost"); int proxyPort = FWProps.getIntegerProperty("http.proxyPort"); if (StringUtils.isNotBlank(proxyHost) && proxyPort > 0) { client.getHostConfiguration().setProxy(proxyHost, proxyPort); } }
/** * Get list of urls on metalink * @param metalinkUrl * @return */ public static List<String> getMetalinkUrls(String metalinkUrl) { HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager()); GetMethod getMethod = new GetMethod(metalinkUrl); List<String> urls = new ArrayList<>(); try ( InputStream is = getMethod.getResponseBodyAsStream() ) { if (httpClient.executeMethod(getMethod) == HttpStatus.SC_OK) { Map<String, List<String>> metalinkUrlsMap = getMultipleValuesFromXML(is, new String[] {"url"}); if (metalinkUrlsMap.containsKey("url")) { List<String> metalinkUrls = metalinkUrlsMap.get("url"); urls.addAll(metalinkUrls); } } } catch (IOException e) { s_logger.warn(e.getMessage()); } return urls; }
public static void checkUrlExistence(String url) { if (url.toLowerCase().startsWith("http") || url.toLowerCase().startsWith("https")) { HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager()); HeadMethod httphead = new HeadMethod(url); try { if (httpClient.executeMethod(httphead) != HttpStatus.SC_OK) { throw new IllegalArgumentException("Invalid URL: " + url); } if (url.endsWith("metalink") && !checkUrlExistenceMetalink(url)) { throw new IllegalArgumentException("Invalid URLs defined on metalink: " + url); } } catch (HttpException hte) { throw new IllegalArgumentException("Cannot reach URL: " + url); } catch (IOException ioe) { throw new IllegalArgumentException("Cannot reach URL: " + url); } } }
public static InputStream getInputStreamFromUrl(String url, String user, String password) { try { Pair<String, Integer> hostAndPort = validateUrl(url); HttpClient httpclient = new HttpClient(new MultiThreadedHttpConnectionManager()); if ((user != null) && (password != null)) { httpclient.getParams().setAuthenticationPreemptive(true); Credentials defaultcreds = new UsernamePasswordCredentials(user, password); httpclient.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()); } // Execute the method. GetMethod method = new GetMethod(url); int statusCode = httpclient.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { s_logger.error("Failed to read from URL: " + url); return null; } return method.getResponseBodyAsStream(); } catch (Exception ex) { s_logger.error("Failed to read from URL: " + url); return null; } }
protected void configureDefaultHttpClientParams(HttpParams params) { params.setParameter(HttpClientParams.CONNECTION_MANAGER_CLASS, MultiThreadedHttpConnectionManager.class); params.setParameter(HttpMethodParams.COOKIE_POLICY, CookiePolicy.RFC_2109); params.setLongParameter(HttpClientParams.CONNECTION_MANAGER_TIMEOUT, 10000); Map<HostConfiguration, Integer> maxHostConnectionsMap = new HashMap<HostConfiguration, Integer>(); maxHostConnectionsMap.put(HostConfiguration.ANY_HOST_CONFIGURATION, new Integer(20)); params.setParameter(HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, maxHostConnectionsMap); params.setIntParameter(HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, 20); params.setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 10000); params.setIntParameter(HttpConnectionParams.SO_TIMEOUT, 2*60*1000); boolean retrySocketException = new Boolean(ConfigContext.getCurrentContextConfig().getProperty(RETRY_SOCKET_EXCEPTION_PROPERTY)); if (retrySocketException) { LOG.info("Installing custom HTTP retry handler to retry requests in face of SocketExceptions"); params.setParameter(HttpMethodParams.RETRY_HANDLER, new CustomHttpMethodRetryHandler()); } }
/** * 私有的构造方法 */ private HttpProtocolHandler() { // 创建一个线程安全的HTTP连接池 connectionManager = new MultiThreadedHttpConnectionManager(); connectionManager.getParams().setDefaultMaxConnectionsPerHost(defaultMaxConnPerHost); connectionManager.getParams().setMaxTotalConnections(defaultMaxTotalConn); IdleConnectionTimeoutThread ict = new IdleConnectionTimeoutThread(); ict.addConnectionManager(connectionManager); ict.setConnectionTimeout(defaultIdleConnTimeout); ict.start(); }
public BitlyUrlShortenerImpl() { httpClient = new HttpClient(); httpClient.setHttpConnectionManager(new MultiThreadedHttpConnectionManager()); HostConfiguration hostConfiguration = new HostConfiguration(); hostConfiguration.setHost("api-ssl.bitly.com", 443, Protocol.getProtocol("https")); httpClient.setHostConfiguration(hostConfiguration); }