Example usage for org.apache.http.nio.reactor IOReactorExceptionHandler IOReactorExceptionHandler

List of usage examples for org.apache.http.nio.reactor IOReactorExceptionHandler IOReactorExceptionHandler

Introduction

In this page you can find the example usage for org.apache.http.nio.reactor IOReactorExceptionHandler IOReactorExceptionHandler.

Prototype

IOReactorExceptionHandler

Source Link

Usage

From source file:org.apache.synapse.transport.nhttp.HttpCoreNIOListener.java

/**
 * 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//  ww  w .  j  av a2  s  .co m
 */
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).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();
}

From source file:org.apache.synapse.transport.passthru.PassThroughHttpListener.java

public void start() throws AxisFault {
    serviceTracker.start();// w ww  . j  a v a 2 s. c  o m
    log.info("Starting Pass-through " + namePrefix + " Listener...");

    String prefix = namePrefix + "-Listener I/O dispatcher";

    passThroughListeningIOReactorManager.startIOReactor(ioReactor,
            passThroughListeningIOReactorManager.getServerIODispatch(operatingPort), prefix);

    ioReactor.setExceptionHandler(new IOReactorExceptionHandler() {

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

        public boolean handle(RuntimeException runtimeException) {
            log.warn("System may be unstable: " + namePrefix
                    + " ListeningIOReactor encountered a runtime exception : " + runtimeException.getMessage(),
                    runtimeException);
            return true;
        }
    });
    if (sourceConfiguration.getHttpGetRequestProcessor() != null) {
        sourceConfiguration.getHttpGetRequestProcessor().init(sourceConfiguration.getConfigurationContext(),
                handler);
    }

    startEndpoints();

    state = BaseConstants.STARTED;
}

From source file:com.google.acre.script.NHttpClient.java

public void make_reactor() {
    try {/*from   w w w . j  a  va2  s . c om*/
        _reactor = new DefaultConnectingIOReactor(2, DEFAULT_HTTP_PARAMS);
        _reactor.setExceptionHandler(new IOReactorExceptionHandler() {
            public boolean handle(IOException e) {
                return false;
            }

            public boolean handle(RuntimeException e) {
                return false;
            }
        });
    } catch (IOReactorException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.synapse.transport.nhttp.HttpCoreNIOSender.java

/**
 * 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/*from  w  w w. j a v a  2 s .co  m*/
 */
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().parse(transportOut).build();
    if (log.isInfoEnabled() && proxyConfig.getProxy() != null) {
        log.info("HTTP Sender using Proxy " + proxyConfig.getProxy() + " bypassing "
                + proxyConfig.getProxyBypass());
    }

    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.getCreds(), 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;
}

From source file:org.apache.synapse.transport.passthru.PassThroughHttpSender.java

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();/*from ww w .  j  a  v  a2 s  .c  om*/

    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().parse(transportOutDescription).build();
    if (log.isInfoEnabled() && proxyConfig.getProxy() != null) {
        log.info("HTTP Sender using Proxy " + proxyConfig.getProxy() + " bypassing "
                + proxyConfig.getProxyBypass());
    }

    targetConfiguration = new TargetConfiguration(configurationContext, transportOutDescription, workerPool,
            metrics, proxyConfig.getCreds() != null ? new ProxyAuthenticator(proxyConfig.getCreds()) : null);
    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...");
}