Search in sources :

Example 6 with IOReactorConfig

use of org.apache.http.impl.nio.reactor.IOReactorConfig in project portal by ixinportal.

the class HttpAsyncClientUtil method createAsyncClient.

public CloseableHttpAsyncClient createAsyncClient(boolean proxy) throws KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, MalformedChallengeException, IOReactorException {
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectTimeout).setSocketTimeout(socketTimeout).build();
    SSLContext sslcontext = SSLContexts.createDefault();
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, credentials);
    // 设置协议http和https对应的处理socket链接工厂的对象
    Registry<SchemeIOSessionStrategy> sessionStrategyRegistry = RegistryBuilder.<SchemeIOSessionStrategy>create().register("http", NoopIOSessionStrategy.INSTANCE).register("https", new SSLIOSessionStrategy(sslcontext)).build();
    // 配置io线程
    IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setIoThreadCount(Runtime.getRuntime().availableProcessors()).build();
    // 设置连接池大小
    ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(ioReactorConfig);
    PoolingNHttpClientConnectionManager conMgr = new PoolingNHttpClientConnectionManager(ioReactor, null, sessionStrategyRegistry, null);
    if (poolSize > 0) {
        conMgr.setMaxTotal(poolSize);
    }
    if (maxPerRoute > 0) {
        conMgr.setDefaultMaxPerRoute(maxPerRoute);
    } else {
        conMgr.setDefaultMaxPerRoute(10);
    }
    ConnectionConfig connectionConfig = ConnectionConfig.custom().setMalformedInputAction(CodingErrorAction.IGNORE).setUnmappableInputAction(CodingErrorAction.IGNORE).setCharset(Consts.UTF_8).build();
    Lookup<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.BASIC, new BasicSchemeFactory()).register(AuthSchemes.DIGEST, new DigestSchemeFactory()).register(AuthSchemes.NTLM, new NTLMSchemeFactory()).register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory()).register(AuthSchemes.KERBEROS, new KerberosSchemeFactory()).build();
    conMgr.setDefaultConnectionConfig(connectionConfig);
    if (proxy) {
        return HttpAsyncClients.custom().setConnectionManager(conMgr).setDefaultCredentialsProvider(credentialsProvider).setDefaultAuthSchemeRegistry(authSchemeRegistry).setProxy(new HttpHost(host, port)).setDefaultCookieStore(new BasicCookieStore()).setDefaultRequestConfig(requestConfig).build();
    } else {
        return HttpAsyncClients.custom().setConnectionManager(conMgr).setDefaultCredentialsProvider(credentialsProvider).setDefaultAuthSchemeRegistry(authSchemeRegistry).setDefaultCookieStore(new BasicCookieStore()).build();
    }
}
Also used : ConnectingIOReactor(org.apache.http.nio.reactor.ConnectingIOReactor) DefaultConnectingIOReactor(org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor) RequestConfig(org.apache.http.client.config.RequestConfig) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) SchemeIOSessionStrategy(org.apache.http.nio.conn.SchemeIOSessionStrategy) SSLIOSessionStrategy(org.apache.http.nio.conn.ssl.SSLIOSessionStrategy) SSLContext(javax.net.ssl.SSLContext) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) IOReactorConfig(org.apache.http.impl.nio.reactor.IOReactorConfig) DefaultConnectingIOReactor(org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) HttpHost(org.apache.http.HttpHost) AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider) PoolingNHttpClientConnectionManager(org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager) ConnectionConfig(org.apache.http.config.ConnectionConfig)

Example 7 with IOReactorConfig

use of org.apache.http.impl.nio.reactor.IOReactorConfig in project gateway-dubbox by zhuzhong.

the class OpenApiHttpAsynClientServiceImpl method initHttpAsynClient.

private void initHttpAsynClient() throws IOReactorException {
    // Use custom message parser / writer to customize the way HTTP
    // messages are parsed from and written out to the data stream.
    NHttpMessageParserFactory<HttpResponse> responseParserFactory = new DefaultHttpResponseParserFactory() {

        @Override
        public NHttpMessageParser<HttpResponse> create(final SessionInputBuffer buffer, final MessageConstraints constraints) {
            LineParser lineParser = new BasicLineParser() {

                @Override
                public Header parseHeader(final CharArrayBuffer buffer) {
                    try {
                        return super.parseHeader(buffer);
                    } catch (ParseException ex) {
                        return new BasicHeader(buffer.toString(), null);
                    }
                }
            };
            return new DefaultHttpResponseParser(buffer, lineParser, DefaultHttpResponseFactory.INSTANCE, constraints);
        }
    };
    NHttpMessageWriterFactory<HttpRequest> requestWriterFactory = new DefaultHttpRequestWriterFactory();
    // Use a custom connection factory to customize the process of
    // initialization of outgoing HTTP connections. Beside standard
    // connection
    // configuration parameters HTTP connection factory can define message
    // parser / writer routines to be employed by individual connections.
    NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory = new ManagedNHttpClientConnectionFactory(requestWriterFactory, responseParserFactory, HeapByteBufferAllocator.INSTANCE);
    // Client HTTP connection objects when fully initialized can be bound to
    // an arbitrary network socket. The process of network socket
    // initialization,
    // its connection to a remote address and binding to a local one is
    // controlled
    // by a connection socket factory.
    // SSL context for secure connections can be created either based on
    // system or application specific properties.
    // SSLContext sslcontext = org.apache.http.ssl.SSLContexts.createSystemDefault();
    // SSLContext sslcontext = org.apache.http.ssl.SSLContexts.createDefault();
    SSLContext sslcontext = null;
    try {
        sslcontext = this.createIgnoreVerifySSL();
    } catch (KeyManagementException | NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // Use custom hostname verifier to customize SSL hostname verification.
    HostnameVerifier hostnameVerifier = new DefaultHostnameVerifier();
    // Create a registry of custom connection session strategies for
    // supported
    // protocol schemes.
    Registry<SchemeIOSessionStrategy> sessionStrategyRegistry = RegistryBuilder.<SchemeIOSessionStrategy>create().register("http", NoopIOSessionStrategy.INSTANCE).register("https", new SSLIOSessionStrategy(sslcontext)).build();
    // .register("https", SSLConnectionSocketFactory.getSystemSocketFactory()).build();
    // Use custom DNS resolver to override the system DNS resolution.
    DnsResolver dnsResolver = new SystemDefaultDnsResolver() {

        @Override
        public InetAddress[] resolve(final String host) throws UnknownHostException {
            if (host.equalsIgnoreCase("myhost")) {
                return new InetAddress[] { InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }) };
            } else {
                return super.resolve(host);
            }
        }
    };
    // Create I/O reactor configuration
    IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setIoThreadCount(Runtime.getRuntime().availableProcessors()).setConnectTimeout(30000).setSoTimeout(30000).build();
    // Create a custom I/O reactort
    ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(ioReactorConfig);
    // Create a connection manager with custom configuration.
    PoolingNHttpClientConnectionManager connManager = new PoolingNHttpClientConnectionManager(ioReactor, connFactory, sessionStrategyRegistry, dnsResolver);
    // Create message constraints
    MessageConstraints messageConstraints = MessageConstraints.custom().setMaxHeaderCount(200).setMaxLineLength(2000).build();
    // Create connection configuration
    ConnectionConfig connectionConfig = ConnectionConfig.custom().setMalformedInputAction(CodingErrorAction.IGNORE).setUnmappableInputAction(CodingErrorAction.IGNORE).setCharset(Consts.UTF_8).setMessageConstraints(messageConstraints).build();
    // Configure the connection manager to use connection configuration
    // either
    // by default or for a specific host.
    connManager.setDefaultConnectionConfig(connectionConfig);
    // connManager.setConnectionConfig(new HttpHost("somehost", 80),
    // ConnectionConfig.DEFAULT);
    // Configure total max or per route limits for persistent connections
    // that can be kept in the pool or leased by the connection manager.
    connManager.setMaxTotal(100);
    connManager.setDefaultMaxPerRoute(10);
    // connManager.setMaxPerRoute(new HttpRoute(new HttpHost("somehost",
    // 80)), 20);
    // Use custom cookie store if necessary.
    CookieStore cookieStore = new BasicCookieStore();
    // Use custom credentials provider if necessary.
    // CredentialsProvider credentialsProvider = new
    // BasicCredentialsProvider();
    // credentialsProvider.setCredentials(new AuthScope("localhost", 8889),
    // new UsernamePasswordCredentials("squid", "nopassword"));
    // Create global request configuration
    RequestConfig defaultRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).setExpectContinueEnabled(true).setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST)).setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)).build();
    // Create an HttpClient with the given custom dependencies and
    // configuration.
    // CloseableHttpAsyncClient
    httpAsyncClient = HttpAsyncClients.custom().setConnectionManager(connManager).build();
}
Also used : ConnectingIOReactor(org.apache.http.nio.reactor.ConnectingIOReactor) DefaultConnectingIOReactor(org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor) CharArrayBuffer(org.apache.http.util.CharArrayBuffer) SSLIOSessionStrategy(org.apache.http.nio.conn.ssl.SSLIOSessionStrategy) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SystemDefaultDnsResolver(org.apache.http.impl.conn.SystemDefaultDnsResolver) KeyManagementException(java.security.KeyManagementException) IOReactorConfig(org.apache.http.impl.nio.reactor.IOReactorConfig) BasicLineParser(org.apache.http.message.BasicLineParser) LineParser(org.apache.http.message.LineParser) DefaultHostnameVerifier(org.apache.http.conn.ssl.DefaultHostnameVerifier) DefaultHttpResponseParser(org.apache.http.impl.nio.codecs.DefaultHttpResponseParser) PoolingNHttpClientConnectionManager(org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager) ConnectionConfig(org.apache.http.config.ConnectionConfig) HttpRequest(org.apache.http.HttpRequest) SystemDefaultDnsResolver(org.apache.http.impl.conn.SystemDefaultDnsResolver) DnsResolver(org.apache.http.conn.DnsResolver) RequestConfig(org.apache.http.client.config.RequestConfig) SessionInputBuffer(org.apache.http.nio.reactor.SessionInputBuffer) SchemeIOSessionStrategy(org.apache.http.nio.conn.SchemeIOSessionStrategy) ManagedNHttpClientConnection(org.apache.http.nio.conn.ManagedNHttpClientConnection) HttpResponse(org.apache.http.HttpResponse) BasicLineParser(org.apache.http.message.BasicLineParser) SSLContext(javax.net.ssl.SSLContext) ManagedNHttpClientConnectionFactory(org.apache.http.impl.nio.conn.ManagedNHttpClientConnectionFactory) DefaultHttpRequestWriterFactory(org.apache.http.impl.nio.codecs.DefaultHttpRequestWriterFactory) HostnameVerifier(javax.net.ssl.HostnameVerifier) DefaultHostnameVerifier(org.apache.http.conn.ssl.DefaultHostnameVerifier) DefaultHttpResponseParserFactory(org.apache.http.impl.nio.codecs.DefaultHttpResponseParserFactory) DefaultConnectingIOReactor(org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor) CookieStore(org.apache.http.client.CookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) MessageConstraints(org.apache.http.config.MessageConstraints) ParseException(org.apache.http.ParseException) InetAddress(java.net.InetAddress) BasicHeader(org.apache.http.message.BasicHeader)

Example 8 with IOReactorConfig

use of org.apache.http.impl.nio.reactor.IOReactorConfig in project presto by prestodb.

the class ElasticsearchClient method createClient.

private static RestHighLevelClient createClient(ElasticsearchConfig config, Optional<AwsSecurityConfig> awsSecurityConfig) {
    RestClientBuilder builder = RestClient.builder(new HttpHost(config.getHost(), config.getPort(), config.isTlsEnabled() ? "https" : "http")).setMaxRetryTimeoutMillis((int) config.getMaxRetryTime().toMillis());
    builder.setHttpClientConfigCallback(ignored -> {
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(toIntExact(config.getConnectTimeout().toMillis())).setSocketTimeout(toIntExact(config.getRequestTimeout().toMillis())).build();
        IOReactorConfig reactorConfig = IOReactorConfig.custom().setIoThreadCount(config.getHttpThreadCount()).build();
        // the client builder passed to the call-back is configured to use system properties, which makes it
        // impossible to configure concurrency settings, so we need to build a new one from scratch
        HttpAsyncClientBuilder clientBuilder = HttpAsyncClientBuilder.create().setDefaultRequestConfig(requestConfig).setDefaultIOReactorConfig(reactorConfig).setMaxConnPerRoute(config.getMaxHttpConnections()).setMaxConnTotal(config.getMaxHttpConnections());
        if (config.isTlsEnabled()) {
            buildSslContext(config.getKeystorePath(), config.getKeystorePassword(), config.getTrustStorePath(), config.getTruststorePassword()).ifPresent(clientBuilder::setSSLContext);
            if (config.isVerifyHostnames()) {
                clientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);
            }
        }
        awsSecurityConfig.ifPresent(securityConfig -> clientBuilder.addInterceptorLast(new AwsRequestSigner(securityConfig.getRegion(), getAwsCredentialsProvider(securityConfig))));
        return clientBuilder;
    });
    return new RestHighLevelClient(builder);
}
Also used : IOReactorConfig(org.apache.http.impl.nio.reactor.IOReactorConfig) RequestConfig(org.apache.http.client.config.RequestConfig) HttpHost(org.apache.http.HttpHost) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder)

Example 9 with IOReactorConfig

use of org.apache.http.impl.nio.reactor.IOReactorConfig in project wso2-synapse by wso2.

the class HttpCoreNIOListener method init.

/**
 * Initialize the transport listener, and execute reactor in new separate thread
 * @param "cfgCtx" the Axis2 configuration context
 * @param transportIn the description of the http/s transport from Axis2 configuration
 * @throws AxisFault on error
 */
public void init(ConfigurationContext ctx, TransportInDescription transportIn) throws AxisFault {
    cfgCtx = ctx;
    Map<String, String> o = (Map<String, String>) cfgCtx.getProperty(NhttpConstants.EPR_TO_SERVICE_NAME_MAP);
    if (o != null) {
        this.eprToServiceNameMap = o;
    } else {
        eprToServiceNameMap = new HashMap<String, String>();
        cfgCtx.setProperty(NhttpConstants.EPR_TO_SERVICE_NAME_MAP, eprToServiceNameMap);
    }
    NHttpConfiguration cfg = NHttpConfiguration.getInstance();
    // Initialize connection factory
    params = new BasicHttpParams();
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, cfg.getProperty(NhttpConstants.SO_TIMEOUT_RECEIVER, 60000)).setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, cfg.getProperty(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)).setParameter(CoreProtocolPNames.ORIGIN_SERVER, "Synapse-HttpComponents-NIO");
    // .setParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET,
    // cfg.getStringValue(CoreProtocolPNames.HTTP_ELEMENT_CHARSET, HTTP.DEFAULT_PROTOCOL_CHARSET)); //TODO:This does not works with HTTPCore 4.3
    name = transportIn.getName().toUpperCase(Locale.US) + " Listener";
    scheme = initScheme();
    // Setup listener context
    listenerContext = new ListenerContextBuilder(transportIn).parse().build();
    System.setProperty(transportIn.getName() + ".nio.port", String.valueOf(listenerContext.getPort()));
    // Setup connection factory
    HttpHost host = new HttpHost(listenerContext.getHostname(), listenerContext.getPort(), scheme.getName());
    connFactory = initConnFactoryBuilder(transportIn, host, ctx).build(params);
    // configure the IO reactor on the specified port
    try {
        String prefix = name + " I/O dispatcher";
        IOReactorConfig ioReactorConfig = new IOReactorConfig();
        ioReactorConfig.setIoThreadCount(cfg.getServerIOWorkers());
        ioReactorConfig.setSoTimeout(cfg.getProperty(NhttpConstants.SO_TIMEOUT_RECEIVER, 60000));
        ioReactorConfig.setTcpNoDelay(cfg.getProperty(CoreConnectionPNames.TCP_NODELAY, 1) == 1);
        if (cfg.getBooleanValue("http.nio.interest-ops-queueing", false)) {
            ioReactorConfig.setInterestOpQueued(true);
        }
        ioReactorConfig.setSoReuseAddress(cfg.getBooleanValue(CoreConnectionPNames.SO_REUSEADDR, false));
        ioReactor = new DefaultListeningIOReactor(ioReactorConfig, new NativeThreadFactory(new ThreadGroup(prefix + " thread group"), prefix));
        ioReactor.setExceptionHandler(new IOReactorExceptionHandler() {

            public boolean handle(IOException ioException) {
                log.warn("System may be unstable: IOReactor encountered a checked exception : " + ioException.getMessage(), ioException);
                return true;
            }

            public boolean handle(RuntimeException runtimeException) {
                log.warn("System may be unstable: IOReactor encountered a runtime exception : " + runtimeException.getMessage(), runtimeException);
                return true;
            }
        });
    } catch (IOException e) {
        handleException("Error creating IOReactor", e);
    }
    metrics = new NhttpMetricsCollector(true, transportIn.getName());
    handler = new ServerHandler(cfgCtx, scheme, listenerContext, metrics);
    iodispatch = new ServerIODispatch(handler, connFactory);
    Parameter param = transportIn.getParameter(NhttpConstants.WSDL_EPR_PREFIX);
    if (param != null) {
        serviceEPRPrefix = getServiceEPRPrefix(cfgCtx, (String) param.getValue());
        customEPRPrefix = (String) param.getValue();
        EPRPrefixCheck = false;
    } else {
        serviceEPRPrefix = getServiceEPRPrefix(cfgCtx, listenerContext.getHostname(), listenerContext.getPort());
        customEPRPrefix = scheme.getName() + "://" + listenerContext.getHostname() + ":" + (listenerContext.getPort() == scheme.getDefaultPort() ? "" : listenerContext.getPort()) + "/";
    }
    // register to receive updates on services for lifetime management
    cfgCtx.getAxisConfiguration().addObservers(axisObserver);
    // register with JMX
    mbeanSupport = new TransportMBeanSupport(this, "nio-" + transportIn.getName());
    mbeanSupport.register();
}
Also used : IOReactorExceptionHandler(org.apache.http.nio.reactor.IOReactorExceptionHandler) NhttpMetricsCollector(org.apache.synapse.transport.nhttp.util.NhttpMetricsCollector) TransportMBeanSupport(org.apache.axis2.transport.base.TransportMBeanSupport) DefaultListeningIOReactor(org.apache.http.impl.nio.reactor.DefaultListeningIOReactor) NativeThreadFactory(org.apache.axis2.transport.base.threads.NativeThreadFactory) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) IOReactorConfig(org.apache.http.impl.nio.reactor.IOReactorConfig) HttpHost(org.apache.http.HttpHost) Parameter(org.apache.axis2.description.Parameter) BasicHttpParams(org.apache.http.params.BasicHttpParams) Map(java.util.Map) HashMap(java.util.HashMap)

Example 10 with IOReactorConfig

use of org.apache.http.impl.nio.reactor.IOReactorConfig in project wso2-synapse by wso2.

the class BaseConfiguration method buildIOReactorConfig.

protected IOReactorConfig buildIOReactorConfig() {
    IOReactorConfig config = new IOReactorConfig();
    config.setIoThreadCount(conf.getIOThreadsPerReactor());
    config.setSoTimeout(getSocketTimeout());
    config.setConnectTimeout(getConnectionTimeout());
    config.setTcpNoDelay(conf.getBooleanProperty(HttpConnectionParams.TCP_NODELAY, true));
    config.setSoLinger(conf.getIntProperty(HttpConnectionParams.SO_LINGER, -1));
    config.setSoReuseAddress(conf.getBooleanProperty(HttpConnectionParams.SO_REUSEADDR, false));
    config.setInterestOpQueued(conf.getBooleanProperty("http.nio.interest-ops-queueing", false));
    config.setSelectInterval(conf.getIntProperty("http.nio.select-interval", 1000));
    return config;
}
Also used : IOReactorConfig(org.apache.http.impl.nio.reactor.IOReactorConfig)

Aggregations

IOReactorConfig (org.apache.http.impl.nio.reactor.IOReactorConfig)10 DefaultConnectingIOReactor (org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor)5 PoolingNHttpClientConnectionManager (org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager)4 HttpHost (org.apache.http.HttpHost)3 RequestConfig (org.apache.http.client.config.RequestConfig)3 ConnectionConfig (org.apache.http.config.ConnectionConfig)3 BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)3 SchemeIOSessionStrategy (org.apache.http.nio.conn.SchemeIOSessionStrategy)3 ConnectingIOReactor (org.apache.http.nio.reactor.ConnectingIOReactor)3 IOException (java.io.IOException)2 InterruptedIOException (java.io.InterruptedIOException)2 SSLContext (javax.net.ssl.SSLContext)2 Parameter (org.apache.axis2.description.Parameter)2 TransportMBeanSupport (org.apache.axis2.transport.base.TransportMBeanSupport)2 NativeThreadFactory (org.apache.axis2.transport.base.threads.NativeThreadFactory)2 HttpRequest (org.apache.http.HttpRequest)2 HttpResponse (org.apache.http.HttpResponse)2 HttpAsyncClientBuilder (org.apache.http.impl.nio.client.HttpAsyncClientBuilder)2 ManagedNHttpClientConnectionFactory (org.apache.http.impl.nio.conn.ManagedNHttpClientConnectionFactory)2 SSLIOSessionStrategy (org.apache.http.nio.conn.ssl.SSLIOSessionStrategy)2