Search in sources :

Example 1 with ClientConnFactoryBuilder

use of org.apache.synapse.transport.nhttp.config.ClientConnFactoryBuilder in project wso2-synapse by wso2.

the class PassThroughHttpSender method init.

public void init(ConfigurationContext configurationContext, TransportOutDescription transportOutDescription) throws AxisFault {
    log.info("Initializing Pass-through HTTP/S Sender...");
    namePrefix = transportOutDescription.getName().toUpperCase(Locale.US);
    scheme = getScheme();
    WorkerPool workerPool = null;
    Object obj = configurationContext.getProperty(PassThroughConstants.PASS_THROUGH_TRANSPORT_WORKER_POOL);
    if (obj != null) {
        workerPool = (WorkerPool) obj;
    }
    PassThroughTransportMetricsCollector metrics = new PassThroughTransportMetricsCollector(false, scheme.getName());
    TransportView view = new TransportView(null, this, metrics, null);
    MBeanRegistrar.getInstance().registerMBean(view, "Transport", "passthru-" + namePrefix.toLowerCase() + "-sender");
    proxyConfig = new ProxyConfigBuilder().build(transportOutDescription);
    log.info(proxyConfig.logProxyConfig());
    targetConfiguration = new TargetConfiguration(configurationContext, transportOutDescription, workerPool, metrics, proxyConfig.createProxyAuthenticator());
    targetConfiguration.build();
    PassThroughSenderManager.registerPassThroughHttpSender(this);
    configurationContext.setProperty(PassThroughConstants.PASS_THROUGH_TRANSPORT_WORKER_POOL, targetConfiguration.getWorkerPool());
    ClientConnFactoryBuilder connFactoryBuilder = initConnFactoryBuilder(transportOutDescription);
    connFactory = connFactoryBuilder.createConnFactory(targetConfiguration.getHttpParams());
    try {
        String prefix = namePrefix + "-Sender I/O dispatcher";
        ioReactor = new DefaultConnectingIOReactor(targetConfiguration.getIOReactorConfig(), new NativeThreadFactory(new ThreadGroup(prefix + " Thread Group"), prefix));
        ioReactor.setExceptionHandler(new IOReactorExceptionHandler() {

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

            public boolean handle(RuntimeException runtimeException) {
                log.warn("System may be unstable: " + namePrefix + " ConnectingIOReactor encountered a runtime exception : " + runtimeException.getMessage(), runtimeException);
                return true;
            }
        });
    } catch (IOReactorException e) {
        handleException("Error starting " + namePrefix + " ConnectingIOReactor", e);
    }
    ConnectCallback connectCallback = new ConnectCallback();
    targetConnections = new TargetConnections(ioReactor, targetConfiguration, connectCallback);
    targetConfiguration.setConnections(targetConnections);
    // create the delivery agent to hand over messages
    deliveryAgent = new DeliveryAgent(targetConfiguration, targetConnections, proxyConfig);
    // we need to set the delivery agent
    connectCallback.setDeliveryAgent(deliveryAgent);
    handler = new TargetHandler(deliveryAgent, connFactory, targetConfiguration);
    ioEventDispatch = new ClientIODispatch(handler, connFactory);
    // start the sender in a separate thread
    Thread t = new Thread(new Runnable() {

        public void run() {
            try {
                ioReactor.execute(ioEventDispatch);
            } catch (Exception ex) {
                log.fatal("Exception encountered in the " + namePrefix + " Sender. " + "No more connections will be initiated by this transport", ex);
            }
            log.info(namePrefix + " Sender shutdown");
        }
    }, "PassThrough" + namePrefix + "Sender");
    t.start();
    state = BaseConstants.STARTED;
    log.info("Pass-through " + namePrefix + " Sender started...");
}
Also used : IOReactorExceptionHandler(org.apache.http.nio.reactor.IOReactorExceptionHandler) ProxyConfigBuilder(org.apache.synapse.transport.nhttp.config.ProxyConfigBuilder) TargetConfiguration(org.apache.synapse.transport.passthru.config.TargetConfiguration) ClientConnFactoryBuilder(org.apache.synapse.transport.nhttp.config.ClientConnFactoryBuilder) PassThroughTransportMetricsCollector(org.apache.synapse.transport.passthru.jmx.PassThroughTransportMetricsCollector) NativeThreadFactory(org.apache.axis2.transport.base.threads.NativeThreadFactory) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException) RemoteException(java.rmi.RemoteException) HttpException(org.apache.http.HttpException) IOException(java.io.IOException) IOReactorException(org.apache.http.nio.reactor.IOReactorException) DefaultConnectingIOReactor(org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor) IOReactorException(org.apache.http.nio.reactor.IOReactorException) WorkerPool(org.apache.axis2.transport.base.threads.WorkerPool) TransportView(org.apache.synapse.transport.passthru.jmx.TransportView) TargetConnections(org.apache.synapse.transport.passthru.connections.TargetConnections)

Example 2 with ClientConnFactoryBuilder

use of org.apache.synapse.transport.nhttp.config.ClientConnFactoryBuilder 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);
    connFactory = contextBuilder.createConnFactory(params);
    connpool = new ConnectionPool();
    proxyConfig = new ProxyConfigBuilder().build(transportOut);
    log.info(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)

Example 3 with ClientConnFactoryBuilder

use of org.apache.synapse.transport.nhttp.config.ClientConnFactoryBuilder in project wso2-synapse by wso2.

the class PassThroughHttpSender method reloadDynamicSSLConfig.

/**
 * Reload SSL configurations from configurations, reset all connections and restart the thread
 *
 * @param transport TransportOutDescription of the configuration
 * @throws AxisFault
 */
public void reloadDynamicSSLConfig(TransportOutDescription transport) throws AxisFault {
    log.info("PassThroughHttpSender reloading SSL Config..");
    ClientConnFactoryBuilder connFactoryBuilder = initConnFactoryBuilder(transport);
    connFactory = connFactoryBuilder.createConnFactory(targetConfiguration.getHttpParams());
    // Set new configurations
    handler.setConnFactory(connFactory);
    ioEventDispatch.setConnFactory(connFactory);
    // close existing connections to apply new settings
    targetConnections.resetConnectionPool(connFactory.getHostList());
    log.info("Pass-through " + namePrefix + " Sender updated with Dynamic Configuration Updates ...");
}
Also used : ClientConnFactoryBuilder(org.apache.synapse.transport.nhttp.config.ClientConnFactoryBuilder)

Example 4 with ClientConnFactoryBuilder

use of org.apache.synapse.transport.nhttp.config.ClientConnFactoryBuilder in project wso2-synapse by wso2.

the class HttpCoreNIOSender method reload.

/**
 * Reload SSL configurations and reset all connections
 *
 * @param transportOut TransportOutDescriptin of the configuration
 * @throws AxisFault
 */
public void reload(TransportOutDescription transportOut) throws AxisFault {
    log.info("HttpCoreNIOSender reloading SSL Config..");
    // create new connection factory
    ClientConnFactoryBuilder contextBuilder = initConnFactoryBuilder(transportOut);
    connFactory = contextBuilder.createConnFactory(params);
    // set new connection factory
    handler.setConnFactory(connFactory);
    iodispatch.setConnFactory(connFactory);
    // close existing connections to apply new settings
    handler.resetConnectionPool(connFactory.getHostList());
    log.info("HttpCoreNIO " + name + " Sender updated with Dynamic Configuration Updates ...");
}
Also used : ClientConnFactoryBuilder(org.apache.synapse.transport.nhttp.config.ClientConnFactoryBuilder)

Aggregations

ClientConnFactoryBuilder (org.apache.synapse.transport.nhttp.config.ClientConnFactoryBuilder)4 IOException (java.io.IOException)2 NativeThreadFactory (org.apache.axis2.transport.base.threads.NativeThreadFactory)2 DefaultConnectingIOReactor (org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor)2 IOReactorExceptionHandler (org.apache.http.nio.reactor.IOReactorExceptionHandler)2 ProxyConfigBuilder (org.apache.synapse.transport.nhttp.config.ProxyConfigBuilder)2 InterruptedIOException (java.io.InterruptedIOException)1 RemoteException (java.rmi.RemoteException)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 AxisFault (org.apache.axis2.AxisFault)1 Parameter (org.apache.axis2.description.Parameter)1 TransportMBeanSupport (org.apache.axis2.transport.base.TransportMBeanSupport)1 WorkerPool (org.apache.axis2.transport.base.threads.WorkerPool)1 HttpException (org.apache.http.HttpException)1 IOReactorConfig (org.apache.http.impl.nio.reactor.IOReactorConfig)1 IOEventDispatch (org.apache.http.nio.reactor.IOEventDispatch)1 IOReactorException (org.apache.http.nio.reactor.IOReactorException)1 BasicHttpParams (org.apache.http.params.BasicHttpParams)1 NhttpMetricsCollector (org.apache.synapse.transport.nhttp.util.NhttpMetricsCollector)1 TargetConfiguration (org.apache.synapse.transport.passthru.config.TargetConfiguration)1