Search in sources :

Example 1 with IOReactorConfig

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

the class HttpMetricsSender method setup.

/**
 * The HTTP API is the primary means of writing data into InfluxDB, by
 * sending POST requests to the /write endpoint. Initiate the HttpClient
 * client with a HttpPost request from influxdb url
 *
 * @param influxdbUrl   example : http://localhost:8086/write?db=myd&rp=one_week
 * @param influxDBToken example: my-token
 * @see InfluxdbMetricsSender#setup(String, String)
 */
@Override
public void setup(String influxdbUrl, String influxDBToken) throws Exception {
    // Create I/O reactor configuration
    IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setIoThreadCount(1).setConnectTimeout(JMeterUtils.getPropDefault("backend_influxdb.connection_timeout", 1000)).setSoTimeout(JMeterUtils.getPropDefault("backend_influxdb.socket_timeout", 3000)).build();
    // Create a custom I/O reactor
    ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(ioReactorConfig);
    // Create a connection manager with custom configuration.
    PoolingNHttpClientConnectionManager connManager = new PoolingNHttpClientConnectionManager(ioReactor);
    httpClient = HttpAsyncClientBuilder.create().setConnectionManager(connManager).setMaxConnPerRoute(2).setMaxConnTotal(2).setUserAgent("ApacheJMeter" + JMeterUtils.getJMeterVersion()).disableCookieManagement().disableConnectionState().build();
    url = new URL(influxdbUrl);
    token = influxDBToken;
    httpRequest = createRequest(url, token);
    httpClient.start();
}
Also used : IOReactorConfig(org.apache.http.impl.nio.reactor.IOReactorConfig) ConnectingIOReactor(org.apache.http.nio.reactor.ConnectingIOReactor) DefaultConnectingIOReactor(org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor) DefaultConnectingIOReactor(org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor) PoolingNHttpClientConnectionManager(org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager) URL(java.net.URL)

Example 2 with IOReactorConfig

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

the class AsyncHTTPConduitFactory method setupNIOClient.

public synchronized void setupNIOClient(HTTPClientPolicy clientPolicy) throws IOReactorException {
    if (client != null) {
        return;
    }
    IOReactorConfig config = IOReactorConfig.custom().setIoThreadCount(ioThreadCount).setSelectInterval(selectInterval).setInterestOpQueued(interestOpQueued).setSoLinger(soLinger).setSoTimeout(soTimeout).setSoKeepAlive(soKeepalive).setTcpNoDelay(tcpNoDelay).build();
    Registry<SchemeIOSessionStrategy> ioSessionFactoryRegistry = RegistryBuilder.<SchemeIOSessionStrategy>create().register("http", NoopIOSessionStrategy.INSTANCE).register("https", SSLIOSessionStrategy.getSystemDefaultStrategy()).build();
    ManagedNHttpClientConnectionFactory connectionFactory = new ManagedNHttpClientConnectionFactory();
    DefaultConnectingIOReactor ioreactor = new DefaultConnectingIOReactor(config);
    connectionManager = new PoolingNHttpClientConnectionManager(ioreactor, connectionFactory, ioSessionFactoryRegistry, DefaultSchemePortResolver.INSTANCE, SystemDefaultDnsResolver.INSTANCE, connectionTTL, TimeUnit.MILLISECONDS);
    connectionManager.setDefaultMaxPerRoute(maxPerRoute);
    connectionManager.setMaxTotal(maxConnections);
    ConnectionConfig connectionConfig = ConnectionConfig.custom().setBufferSize(clientPolicy.getChunkLength() > 0 ? clientPolicy.getChunkLength() : 16332).build();
    connectionManager.setDefaultConnectionConfig(connectionConfig);
    RedirectStrategy redirectStrategy = new RedirectStrategy() {

        public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
            return false;
        }

        public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
            return null;
        }
    };
    HttpAsyncClientBuilder httpAsyncClientBuilder = HttpAsyncClients.custom().setConnectionManager(connectionManager).setRedirectStrategy(redirectStrategy).setDefaultCookieStore(new BasicCookieStore() {

        private static final long serialVersionUID = 1L;

        public void addCookie(Cookie cookie) {
        }
    });
    adaptClientBuilder(httpAsyncClientBuilder);
    client = httpAsyncClientBuilder.build();
    // Start the client thread
    client.start();
    // Always start the idle checker thread to validate pending requests and
    // use the ConnectionMaxIdle to close the idle connection
    new CloseIdleConnectionThread(connectionManager, client).start();
}
Also used : HttpRequest(org.apache.http.HttpRequest) Cookie(org.apache.http.cookie.Cookie) SchemeIOSessionStrategy(org.apache.http.nio.conn.SchemeIOSessionStrategy) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) ManagedNHttpClientConnectionFactory(org.apache.http.impl.nio.conn.ManagedNHttpClientConnectionFactory) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder) IOReactorConfig(org.apache.http.impl.nio.reactor.IOReactorConfig) DefaultConnectingIOReactor(org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) RedirectStrategy(org.apache.http.client.RedirectStrategy) PoolingNHttpClientConnectionManager(org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager) ConnectionConfig(org.apache.http.config.ConnectionConfig)

Example 3 with IOReactorConfig

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

the class BaseConfigurationTest method testBuildIOReactorConfig.

@Test
public void testBuildIOReactorConfig() throws Exception {
    IOReactorConfig config = baseConfiguration.buildIOReactorConfig();
    int expectedIOThreadCount = Runtime.getRuntime().availableProcessors();
    Assert.assertNotNull("I/O Reactor hasn't been initialized.", config);
    Assert.assertEquals("I/O reactor thread count isn't correct.", expectedIOThreadCount, config.getIoThreadCount());
}
Also used : IOReactorConfig(org.apache.http.impl.nio.reactor.IOReactorConfig) Test(org.junit.Test)

Example 4 with IOReactorConfig

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

the class SourceConfigurationTest method testGetIOReactorConfig.

@Test
public void testGetIOReactorConfig() throws Exception {
    IOReactorConfig config = sourceConfiguration.getIOReactorConfig();
    int expectedIOThreadCount = Runtime.getRuntime().availableProcessors();
    Assert.assertNotNull("I/O Reactor hasn't been initialized.", config);
    Assert.assertEquals("I/O reactor thread count isn't correct.", expectedIOThreadCount, config.getIoThreadCount());
}
Also used : IOReactorConfig(org.apache.http.impl.nio.reactor.IOReactorConfig) Test(org.junit.Test)

Example 5 with IOReactorConfig

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

the class HttpCoreNIOSender method init.

/**
 * Initialize the transport sender, and execute reactor in new separate thread
 * @param cfgCtx the Axis2 configuration context
 * @param transportOut the description of the http/s transport from Axis2 configuration
 * @throws AxisFault thrown on an error
 */
public void init(ConfigurationContext cfgCtx, TransportOutDescription transportOut) throws AxisFault {
    this.configurationContext = cfgCtx;
    cfg = NHttpConfiguration.getInstance();
    params = new BasicHttpParams();
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, cfg.getProperty(NhttpConstants.SO_TIMEOUT_SENDER, 60000)).setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, cfg.getProperty(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000)).setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, cfg.getProperty(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)).setParameter(CoreProtocolPNames.USER_AGENT, "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 = transportOut.getName().toUpperCase(Locale.US) + " Sender";
    ClientConnFactoryBuilder contextBuilder = initConnFactoryBuilder(transportOut, cfgCtx);
    connFactory = contextBuilder.createConnFactory(params);
    connpool = new ConnectionPool();
    proxyConfig = new ProxyConfigBuilder().build(transportOut);
    if (log.isDebugEnabled()) {
        log.debug(proxyConfig.logProxyConfig());
    }
    Parameter param = transportOut.getParameter("warnOnHTTP500");
    if (param != null) {
        String[] warnOnHttp500 = ((String) param.getValue()).split("\\|");
        cfgCtx.setNonReplicableProperty("warnOnHTTP500", warnOnHttp500);
    }
    IOReactorConfig ioReactorConfig = new IOReactorConfig();
    ioReactorConfig.setIoThreadCount(NHttpConfiguration.getInstance().getClientIOWorkers());
    ioReactorConfig.setSoTimeout(cfg.getProperty(NhttpConstants.SO_TIMEOUT_RECEIVER, 60000));
    ioReactorConfig.setConnectTimeout(cfg.getProperty(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000));
    ioReactorConfig.setTcpNoDelay(cfg.getProperty(CoreConnectionPNames.TCP_NODELAY, 1) == 1);
    if (cfg.getBooleanValue("http.nio.interest-ops-queueing", false)) {
        ioReactorConfig.setInterestOpQueued(true);
    }
    try {
        String prefix = name + " I/O dispatcher";
        ioReactor = new DefaultConnectingIOReactor(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) {
        log.error("Error starting the IOReactor", e);
        throw new AxisFault(e.getMessage(), e);
    }
    metrics = new NhttpMetricsCollector(false, transportOut.getName());
    handler = new ClientHandler(connpool, connFactory, proxyConfig, cfgCtx, params, metrics);
    iodispatch = new ClientIODispatch(handler, connFactory);
    final IOEventDispatch ioEventDispatch = iodispatch;
    // start the Sender in a new seperate thread
    Thread t = new Thread(new Runnable() {

        public void run() {
            try {
                ioReactor.execute(ioEventDispatch);
            } catch (InterruptedIOException ex) {
                log.fatal("Reactor Interrupted");
            } catch (IOException e) {
                log.fatal("Encountered an I/O error: " + e.getMessage(), e);
            }
            log.info(name + " Shutdown");
        }
    }, "HttpCoreNIOSender");
    t.start();
    log.info(name + " starting");
    // register with JMX
    mbeanSupport = new TransportMBeanSupport(this, "nio-" + transportOut.getName());
    mbeanSupport.register();
    state = BaseConstants.STARTED;
}
Also used : IOReactorExceptionHandler(org.apache.http.nio.reactor.IOReactorExceptionHandler) AxisFault(org.apache.axis2.AxisFault) InterruptedIOException(java.io.InterruptedIOException) ProxyConfigBuilder(org.apache.synapse.transport.nhttp.config.ProxyConfigBuilder) ClientConnFactoryBuilder(org.apache.synapse.transport.nhttp.config.ClientConnFactoryBuilder) NhttpMetricsCollector(org.apache.synapse.transport.nhttp.util.NhttpMetricsCollector) TransportMBeanSupport(org.apache.axis2.transport.base.TransportMBeanSupport) NativeThreadFactory(org.apache.axis2.transport.base.threads.NativeThreadFactory) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) IOReactorConfig(org.apache.http.impl.nio.reactor.IOReactorConfig) DefaultConnectingIOReactor(org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor) Parameter(org.apache.axis2.description.Parameter) IOEventDispatch(org.apache.http.nio.reactor.IOEventDispatch) BasicHttpParams(org.apache.http.params.BasicHttpParams)

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