Search in sources :

Example 11 with XnioWorker

use of org.xnio.XnioWorker in project undertow by undertow-io.

the class AbstractLoadBalancingProxyTestCase method teardown.

@AfterClass
public static void teardown() {
    XnioWorker worker1 = null, worker2 = null;
    int countDown = 0;
    try {
        if (server1 != null) {
            final XnioWorker worker = server1.getWorker();
            server1.stop();
            // if stop did not shutdown the worker, we need to run the latch to prevent a Address already in use (UNDERTOW-1960)
            if (worker != null && !worker.isShutdown()) {
                countDown++;
                worker1 = worker;
            }
        }
    } finally {
        try {
            if (server2 != null) {
                final XnioWorker worker = server2.getWorker();
                server2.stop();
                // if stop did not shutdown the worker, we need to run the latch to prevent a Address already in use (UNDERTOW-1960)
                if (worker != null && !worker.isShutdown() && worker != worker1) {
                    worker2 = worker;
                    countDown++;
                }
            }
        } finally {
            if (countDown != 0) {
                // TODO this is needed solely for ssl servers; replace this by the mechanism described in UNDERTOW-1648 once it is implemented
                final CountDownLatch latch = new CountDownLatch(countDown);
                if (worker1 != null)
                    worker1.getIoThread().execute(latch::countDown);
                if (worker2 != null)
                    worker2.getIoThread().execute(latch::countDown);
                try {
                    latch.await();
                    // double protection, we need to guarantee that the servers have stopped, and some environments seem to need a small delay to re-bind the socket
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                // ignore
                }
            }
        }
    }
}
Also used : XnioWorker(org.xnio.XnioWorker) CountDownLatch(java.util.concurrent.CountDownLatch) AfterClass(org.junit.AfterClass)

Example 12 with XnioWorker

use of org.xnio.XnioWorker in project undertow by undertow-io.

the class ProgramaticAutobahnServer method run.

public void run() {
    Xnio xnio = Xnio.getInstance();
    try {
        XnioWorker worker = xnio.createWorker(OptionMap.builder().set(Options.CONNECTION_HIGH_WATER, 1000000).set(Options.CONNECTION_LOW_WATER, 1000000).set(Options.WORKER_TASK_CORE_THREADS, 10).set(Options.WORKER_TASK_MAX_THREADS, 12).set(Options.TCP_NODELAY, true).set(Options.CORK, true).getMap());
        OptionMap serverOptions = OptionMap.builder().set(Options.TCP_NODELAY, true).set(Options.REUSE_ADDRESSES, true).getMap();
        DefaultByteBufferPool pool = new DefaultByteBufferPool(true, 8192);
        HttpOpenListener openListener = new HttpOpenListener(pool);
        ChannelListener acceptListener = ChannelListeners.openListenerAdapter(openListener);
        AcceptingChannel<StreamConnection> server = worker.createStreamConnectionServer(new InetSocketAddress(port), acceptListener, serverOptions);
        server.resumeAccepts();
        final ServletContainer container = ServletContainer.Factory.newInstance();
        DeploymentInfo builder = new DeploymentInfo().setClassLoader(ProgramaticAutobahnServer.class.getClassLoader()).setContextPath("/").setClassIntrospecter(TestClassIntrospector.INSTANCE).setDeploymentName("servletContext.war").addFilter(new FilterInfo("filter", JsrWebSocketFilter.class)).addFilterUrlMapping("filter", "/*", DispatcherType.REQUEST).addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME, new WebSocketDeploymentInfo().setBuffers(pool).setWorker(worker).setDispatchToWorkerThread(true).addEndpoint(new ServerEndpointConfigImpl(ProgramaticAutobahnEndpoint.class, "/")).addExtension(new PerMessageDeflateHandshake()));
        deploymentManager = container.addDeployment(builder);
        deploymentManager.deploy();
        openListener.setRootHandler(deploymentManager.start());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ChannelListener(org.xnio.ChannelListener) DefaultByteBufferPool(io.undertow.server.DefaultByteBufferPool) XnioWorker(org.xnio.XnioWorker) InetSocketAddress(java.net.InetSocketAddress) ServerEndpointConfigImpl(io.undertow.websockets.jsr.ServerEndpointConfigImpl) StreamConnection(org.xnio.StreamConnection) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) ServletException(javax.servlet.ServletException) Xnio(org.xnio.Xnio) JsrWebSocketFilter(io.undertow.websockets.jsr.JsrWebSocketFilter) OptionMap(org.xnio.OptionMap) ServletContainer(io.undertow.servlet.api.ServletContainer) PerMessageDeflateHandshake(io.undertow.websockets.extensions.PerMessageDeflateHandshake) HttpOpenListener(io.undertow.server.protocol.http.HttpOpenListener) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) FilterInfo(io.undertow.servlet.api.FilterInfo)

Example 13 with XnioWorker

use of org.xnio.XnioWorker in project undertow by undertow-io.

the class AnnotatedAutobahnServer method run.

public void run() {
    Xnio xnio = Xnio.getInstance();
    DeploymentManager deploymentManager = null;
    try {
        XnioWorker worker = xnio.createWorker(OptionMap.builder().set(Options.WORKER_WRITE_THREADS, 4).set(Options.WORKER_READ_THREADS, 4).set(Options.CONNECTION_HIGH_WATER, 1000000).set(Options.CONNECTION_LOW_WATER, 1000000).set(Options.WORKER_TASK_CORE_THREADS, 10).set(Options.WORKER_TASK_MAX_THREADS, 12).set(Options.TCP_NODELAY, true).set(Options.CORK, true).getMap());
        OptionMap serverOptions = OptionMap.builder().set(Options.TCP_NODELAY, true).set(Options.REUSE_ADDRESSES, true).getMap();
        DefaultByteBufferPool pool = new DefaultByteBufferPool(true, 8024);
        HttpOpenListener openListener = new HttpOpenListener(pool);
        ChannelListener acceptListener = ChannelListeners.openListenerAdapter(openListener);
        AcceptingChannel<StreamConnection> server = worker.createStreamConnectionServer(new InetSocketAddress(port), acceptListener, serverOptions);
        server.resumeAccepts();
        final ServletContainer container = ServletContainer.Factory.newInstance();
        DeploymentInfo builder = new DeploymentInfo().setClassLoader(AnnotatedAutobahnServer.class.getClassLoader()).setContextPath("/").setClassIntrospecter(TestClassIntrospector.INSTANCE).setDeploymentName("servletContext.war").addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME, new WebSocketDeploymentInfo().setBuffers(pool).setWorker(worker).addEndpoint(AutobahnAnnotatedEndpoint.class).setDispatchToWorkerThread(true).addExtension(new PerMessageDeflateHandshake())).addFilter(new FilterInfo("filter", JsrWebSocketFilter.class)).addFilterUrlMapping("filter", "/*", DispatcherType.REQUEST);
        deploymentManager = container.addDeployment(builder);
        deploymentManager.deploy();
        openListener.setRootHandler(deploymentManager.start());
    } catch (Exception e) {
        log.error("failed to start server", e);
    } finally {
        if (deploymentManager != null) {
            deploymentManager.undeploy();
            try {
                deploymentManager.stop();
            } catch (ServletException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
Also used : ChannelListener(org.xnio.ChannelListener) DefaultByteBufferPool(io.undertow.server.DefaultByteBufferPool) DeploymentManager(io.undertow.servlet.api.DeploymentManager) XnioWorker(org.xnio.XnioWorker) InetSocketAddress(java.net.InetSocketAddress) StreamConnection(org.xnio.StreamConnection) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) ServletException(javax.servlet.ServletException) ServletException(javax.servlet.ServletException) Xnio(org.xnio.Xnio) OptionMap(org.xnio.OptionMap) ServletContainer(io.undertow.servlet.api.ServletContainer) PerMessageDeflateHandshake(io.undertow.websockets.extensions.PerMessageDeflateHandshake) HttpOpenListener(io.undertow.server.protocol.http.HttpOpenListener) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) FilterInfo(io.undertow.servlet.api.FilterInfo)

Example 14 with XnioWorker

use of org.xnio.XnioWorker in project spring-boot by spring-projects.

the class AccessLogHttpHandlerFactory method getHandler.

@Override
public HttpHandler getHandler(HttpHandler next) {
    try {
        createAccessLogDirectoryIfNecessary();
        XnioWorker worker = createWorker();
        String baseName = (this.prefix != null) ? this.prefix : "access_log.";
        String formatString = (this.pattern != null) ? this.pattern : "common";
        return new ClosableAccessLogHandler(next, worker, new DefaultAccessLogReceiver(worker, this.directory, baseName, this.suffix, this.rotate), formatString);
    } catch (IOException ex) {
        throw new IllegalStateException("Failed to create AccessLogHandler", ex);
    }
}
Also used : XnioWorker(org.xnio.XnioWorker) DefaultAccessLogReceiver(io.undertow.server.handlers.accesslog.DefaultAccessLogReceiver) IOException(java.io.IOException)

Example 15 with XnioWorker

use of org.xnio.XnioWorker in project wildfly by wildfly.

the class ModClusterService method start.

@Override
public synchronized void start(StartContext context) throws StartException {
    super.start(context);
    SSLContext sslContext = this.sslContext != null ? this.sslContext.get() : null;
    // TODO: SSL support for the client
    final ModCluster.Builder modClusterBuilder;
    final XnioWorker worker = this.worker.get();
    if (sslContext == null) {
        modClusterBuilder = ModCluster.builder(worker);
    } else {
        OptionMap.Builder builder = OptionMap.builder();
        builder.set(Options.USE_DIRECT_BUFFERS, true);
        OptionMap combined = builder.getMap();
        XnioSsl xnioSsl = new UndertowXnioSsl(worker.getXnio(), combined, sslContext);
        modClusterBuilder = ModCluster.builder(worker, UndertowClient.getInstance(), xnioSsl);
    }
    modClusterBuilder.setMaxRetries(maxRetries).setClientOptions(clientOptions).setHealthCheckInterval(healthCheckInterval).setMaxRequestTime(maxRequestTime).setCacheConnections(cachedConnections).setQueueNewRequests(requestQueueSize > 0).setRequestQueueSize(requestQueueSize).setRemoveBrokenNodes(removeBrokenNodes).setTtl(connectionIdleTimeout).setMaxConnections(connectionsPerThread).setUseAlias(useAlias).setRouteParsingStrategy(routeParsingStrategy).setRankedAffinityDelimiter(routeDelimiter);
    if (FailoverStrategy.DETERMINISTIC.equals(failoverStrategy)) {
        modClusterBuilder.setDeterministicFailover(true);
    }
    modCluster = modClusterBuilder.build();
    MCMPConfig.Builder builder = MCMPConfig.builder();
    final SocketBinding advertiseBinding = advertiseSocketBinding != null ? advertiseSocketBinding.get() : null;
    if (advertiseBinding != null) {
        InetAddress multicastAddress = advertiseBinding.getMulticastAddress();
        if (multicastAddress == null) {
            throw UndertowLogger.ROOT_LOGGER.advertiseSocketBindingRequiresMulticastAddress();
        }
        if (advertiseFrequency > 0) {
            builder.enableAdvertise().setAdvertiseAddress(advertiseBinding.getSocketAddress().getAddress().getHostAddress()).setAdvertiseGroup(multicastAddress.getHostAddress()).setAdvertisePort(advertiseBinding.getMulticastPort()).setAdvertiseFrequency(advertiseFrequency).setPath(advertisePath).setProtocol(advertiseProtocol).setSecurityKey(securityKey);
        }
    }
    builder.setManagementHost(managementSocketBinding.get().getSocketAddress().getHostString());
    builder.setManagementPort(managementSocketBinding.get().getSocketAddress().getPort());
    config = builder.build();
    if (advertiseBinding != null && advertiseFrequency > 0) {
        try {
            modCluster.advertise(config);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    modCluster.start();
}
Also used : SocketBinding(org.jboss.as.network.SocketBinding) XnioSsl(org.xnio.ssl.XnioSsl) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) XnioWorker(org.xnio.XnioWorker) MCMPConfig(io.undertow.server.handlers.proxy.mod_cluster.MCMPConfig) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) ModCluster(io.undertow.server.handlers.proxy.mod_cluster.ModCluster) OptionMap(org.xnio.OptionMap) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) InetAddress(java.net.InetAddress)

Aggregations

XnioWorker (org.xnio.XnioWorker)27 IOException (java.io.IOException)15 Xnio (org.xnio.Xnio)13 HttpHandler (io.undertow.server.HttpHandler)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 HttpServerExchange (io.undertow.server.HttpServerExchange)5 PathHandler (io.undertow.server.handlers.PathHandler)5 BeforeClass (org.junit.BeforeClass)5 OptionMap (org.xnio.OptionMap)5 DefaultByteBufferPool (io.undertow.server.DefaultByteBufferPool)4 InetSocketAddress (java.net.InetSocketAddress)4 URI (java.net.URI)4 Undertow (io.undertow.Undertow)3 ByteBufferPool (io.undertow.connector.ByteBufferPool)3 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)3 ServletContainer (io.undertow.servlet.api.ServletContainer)3 Test (org.junit.Test)3 ChannelListener (org.xnio.ChannelListener)3 Predicate (io.undertow.predicate.Predicate)2 UndertowXnioSsl (io.undertow.protocols.ssl.UndertowXnioSsl)2