Search in sources :

Example 1 with DefaultListeningIOReactor

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

the class PassThroughListeningIOReactorManager method initIOReactor.

/**
 * Create IOReactor with given configuration
 *
 * @param port                                   Port of the Endpoint for axis2 Listener
 * @param nHttpServerEventHandler                Server Handler responsible for handle events of port
 * @param passThroughSharedListenerConfiguration configuration related to create and start IOReactor
 * @return IOReactor
 */
public ListeningIOReactor initIOReactor(int port, NHttpServerEventHandler nHttpServerEventHandler, PassThroughSharedListenerConfiguration passThroughSharedListenerConfiguration) throws IOReactorException {
    ListeningIOReactor defaultListeningIOReactor;
    try {
        ServerIODispatch serverIODispatch;
        // PassThroughListenerConfiguration to be used by Shared IO Reactor
        synchronized (this) {
            if (this.passThroughListenerConfiguration == null && !passThroughSharedListenerConfiguration.getSourceConfiguration().getScheme().isSSL()) {
                this.passThroughListenerConfiguration = passThroughSharedListenerConfiguration;
            }
            if (this.sslPassThroughListenerConfiguration == null && passThroughSharedListenerConfiguration.getSourceConfiguration().getScheme().isSSL()) {
                this.sslPassThroughListenerConfiguration = passThroughSharedListenerConfiguration;
            }
        }
        // If IOReactor is in shared mode and if it is  not initialized, initialize it for HTTP Protocol
        if (ioReactorSharingMode == IOReactorSharingMode.SHARED && !isSharedIOReactorInitiated.get() && !passThroughSharedListenerConfiguration.getSourceConfiguration().getScheme().isSSL()) {
            synchronized (this) {
                portServerHandlerMapper.put(port, nHttpServerEventHandler);
                serverIODispatch = new MultiListenerServerIODispatch(portServerHandlerMapper, nHttpServerEventHandler, passThroughSharedListenerConfiguration.getServerConnFactory());
                // Create IOReactor for Listener make it shareable with Inbounds
                defaultListeningIOReactor = createListeningIOReactor(passThroughSharedListenerConfiguration);
                log.info("IO Reactor for port " + port + " initiated on shared mode which will be used by non axis2 " + "Transport Listeners ");
                sharedListeningIOReactor = defaultListeningIOReactor;
                isSharedIOReactorInitiated.compareAndSet(false, true);
            }
        } else if (ioReactorSharingMode == IOReactorSharingMode.SHARED && !isSharedSSLIOReactorInitiated.get() && passThroughSharedListenerConfiguration.getSourceConfiguration().getScheme().isSSL()) {
            synchronized (this) {
                serverConnectionFactoryMapper.put(port, passThroughSharedListenerConfiguration.getServerConnFactory());
                portServerHandlerMapper.put(port, nHttpServerEventHandler);
                serverIODispatch = new MultiListenerSSLServerIODispatch(portServerHandlerMapper, nHttpServerEventHandler, serverConnectionFactoryMapper);
                // Create IOReactor for Listener make it shareable with Inbounds
                defaultListeningIOReactor = createListeningIOReactor(passThroughSharedListenerConfiguration);
                log.info("IO Reactor for port " + port + " initiated on shared mode which will be used by non axis2 " + "Transport SSL Listeners ");
                sharedSSLListeningIOReactor = defaultListeningIOReactor;
                isSharedSSLIOReactorInitiated.compareAndSet(false, true);
            }
        } else {
            // Create un shareable IOReactors for axis2 Listeners and assign IOReactor Config for later
            // create IOReactor for Inbounds
            serverIODispatch = new ServerIODispatch(nHttpServerEventHandler, passThroughSharedListenerConfiguration.getServerConnFactory());
            defaultListeningIOReactor = createListeningIOReactor(passThroughSharedListenerConfiguration);
        }
        passThroughListenerServerIODispatchMapper.put(port, serverIODispatch);
        passThroughListenerIOReactorMapper.put(port, defaultListeningIOReactor);
    } catch (IOReactorException e) {
        throw new IOReactorException("Error occurred when trying to init IO Reactor", e);
    }
    return defaultListeningIOReactor;
}
Also used : IOReactorException(org.apache.http.nio.reactor.IOReactorException) ServerIODispatch(org.apache.synapse.transport.passthru.ServerIODispatch) MultiListenerSSLServerIODispatch(org.apache.synapse.transport.passthru.core.ssl.MultiListenerSSLServerIODispatch) ListeningIOReactor(org.apache.http.nio.reactor.ListeningIOReactor) DefaultListeningIOReactor(org.apache.http.impl.nio.reactor.DefaultListeningIOReactor) MultiListenerSSLServerIODispatch(org.apache.synapse.transport.passthru.core.ssl.MultiListenerSSLServerIODispatch)

Example 2 with DefaultListeningIOReactor

use of org.apache.http.impl.nio.reactor.DefaultListeningIOReactor 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).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)

Aggregations

DefaultListeningIOReactor (org.apache.http.impl.nio.reactor.DefaultListeningIOReactor)2 IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Parameter (org.apache.axis2.description.Parameter)1 TransportMBeanSupport (org.apache.axis2.transport.base.TransportMBeanSupport)1 NativeThreadFactory (org.apache.axis2.transport.base.threads.NativeThreadFactory)1 HttpHost (org.apache.http.HttpHost)1 IOReactorConfig (org.apache.http.impl.nio.reactor.IOReactorConfig)1 IOReactorException (org.apache.http.nio.reactor.IOReactorException)1 IOReactorExceptionHandler (org.apache.http.nio.reactor.IOReactorExceptionHandler)1 ListeningIOReactor (org.apache.http.nio.reactor.ListeningIOReactor)1 BasicHttpParams (org.apache.http.params.BasicHttpParams)1 NhttpMetricsCollector (org.apache.synapse.transport.nhttp.util.NhttpMetricsCollector)1 ServerIODispatch (org.apache.synapse.transport.passthru.ServerIODispatch)1 MultiListenerSSLServerIODispatch (org.apache.synapse.transport.passthru.core.ssl.MultiListenerSSLServerIODispatch)1