public void start() {
    final Xnio xnio;
    try {
      // Do what org.jboss.as.remoting.XnioUtil does
      xnio =
          Xnio.getInstance(
              null,
              Module.getModuleFromCallerModuleLoader(
                      ModuleIdentifier.fromString("org.jboss.xnio.nio"))
                  .getClassLoader());
    } catch (Exception e) {
      throw new IllegalStateException(e.getLocalizedMessage());
    }
    try {
      // TODO make this configurable
      worker =
          xnio.createWorker(
              OptionMap.builder()
                  .set(Options.WORKER_IO_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());

      Builder serverOptionsBuilder =
          OptionMap.builder().set(Options.TCP_NODELAY, true).set(Options.REUSE_ADDRESSES, true);
      ChannelListener acceptListener = ChannelListeners.openListenerAdapter(openListener);
      if (httpAddress != null) {
        normalServer =
            worker.createStreamConnectionServer(
                httpAddress, acceptListener, serverOptionsBuilder.getMap());
        normalServer.resumeAccepts();
      }
      if (secureAddress != null) {
        SSLContext sslContext = securityRealm.getSSLContext();
        Set<AuthMechanism> supportedMechanisms =
            securityRealm.getSupportedAuthenticationMechanisms();
        if (supportedMechanisms.contains(AuthMechanism.CLIENT_CERT)) {
          if (supportedMechanisms.contains(AuthMechanism.DIGEST)
              || supportedMechanisms.contains(AuthMechanism.PLAIN)) {
            // Username / Password auth is possible so don't mandate a client certificate.
            serverOptionsBuilder.set(SSL_CLIENT_AUTH_MODE, REQUESTED);
          } else {
            serverOptionsBuilder.set(SSL_CLIENT_AUTH_MODE, REQUIRED);
          }
        }
        OptionMap secureOptions = serverOptionsBuilder.getMap();
        XnioSsl xnioSsl = new JsseXnioSsl(worker.getXnio(), secureOptions, sslContext);
        secureServer =
            xnioSsl.createSslConnectionServer(worker, secureAddress, acceptListener, secureOptions);
        secureServer.resumeAccepts();
      }
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  public void start() {
    final Xnio xnio;
    try {
      // Do what org.jboss.as.remoting.XnioUtil does
      xnio =
          Xnio.getInstance(
              null,
              Module.getModuleFromCallerModuleLoader(
                      ModuleIdentifier.fromString("org.jboss.xnio.nio"))
                  .getClassLoader());
    } catch (Exception e) {
      throw new IllegalStateException(e.getLocalizedMessage());
    }
    try {
      // TODO make this configurable
      worker =
          xnio.createWorker(
              OptionMap.builder()
                  .set(Options.WORKER_IO_THREADS, 2)
                  .set(Options.WORKER_TASK_CORE_THREADS, 5)
                  .set(Options.WORKER_TASK_MAX_THREADS, 10)
                  .set(Options.TCP_NODELAY, true)
                  .set(Options.CORK, true)
                  .getMap());

      Builder serverOptionsBuilder =
          OptionMap.builder().set(Options.TCP_NODELAY, true).set(Options.REUSE_ADDRESSES, true);
      ChannelListener acceptListener = ChannelListeners.openListenerAdapter(openListener);
      if (httpAddress != null) {
        normalServer =
            worker.createStreamConnectionServer(
                httpAddress, acceptListener, serverOptionsBuilder.getMap());
        normalServer.resumeAccepts();
      }
      if (secureAddress != null) {
        if (sslClientAuthMode != null) {
          serverOptionsBuilder.set(SSL_CLIENT_AUTH_MODE, sslClientAuthMode);
        }
        OptionMap secureOptions = serverOptionsBuilder.getMap();
        XnioSsl xnioSsl = new UndertowXnioSsl(worker.getXnio(), secureOptions, sslContext);
        secureServer =
            xnioSsl.createSslConnectionServer(worker, secureAddress, acceptListener, secureOptions);
        secureServer.resumeAccepts();
      }
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
Example #3
0
  public synchronized void start() {
    xnio = Xnio.getInstance(Undertow.class.getClassLoader());
    channels = new ArrayList<>();
    try {
      worker =
          xnio.createWorker(
              OptionMap.builder()
                  .set(Options.WORKER_IO_THREADS, ioThreads)
                  .set(Options.CONNECTION_HIGH_WATER, 1000000)
                  .set(Options.CONNECTION_LOW_WATER, 1000000)
                  .set(Options.WORKER_TASK_CORE_THREADS, workerThreads)
                  .set(Options.WORKER_TASK_MAX_THREADS, workerThreads)
                  .set(Options.TCP_NODELAY, true)
                  .set(Options.CORK, true)
                  .addAll(workerOptions)
                  .getMap());

      OptionMap socketOptions =
          OptionMap.builder()
              .set(Options.WORKER_IO_THREADS, ioThreads)
              .set(Options.TCP_NODELAY, true)
              .set(Options.REUSE_ADDRESSES, true)
              .set(Options.BALANCING_TOKENS, 1)
              .set(Options.BALANCING_CONNECTIONS, 2)
              .set(Options.BACKLOG, 1000)
              .addAll(this.socketOptions)
              .getMap();

      Pool<ByteBuffer> buffers =
          new ByteBufferSlicePool(
              directBuffers
                  ? BufferAllocator.DIRECT_BYTE_BUFFER_ALLOCATOR
                  : BufferAllocator.BYTE_BUFFER_ALLOCATOR,
              bufferSize,
              bufferSize * buffersPerRegion);

      for (ListenerConfig listener : listeners) {
        final HttpHandler rootHandler =
            listener.rootHandler != null ? listener.rootHandler : this.rootHandler;
        if (listener.type == ListenerType.AJP) {
          AjpOpenListener openListener = new AjpOpenListener(buffers, serverOptions);
          openListener.setRootHandler(rootHandler);
          ChannelListener<AcceptingChannel<StreamConnection>> acceptListener =
              ChannelListeners.openListenerAdapter(openListener);
          AcceptingChannel<? extends StreamConnection> server =
              worker.createStreamConnectionServer(
                  new InetSocketAddress(Inet4Address.getByName(listener.host), listener.port),
                  acceptListener,
                  socketOptions);
          server.resumeAccepts();
          channels.add(server);
        } else {
          OptionMap undertowOptions =
              OptionMap.builder()
                  .set(UndertowOptions.BUFFER_PIPELINED_DATA, true)
                  .addAll(serverOptions)
                  .getMap();
          if (listener.type == ListenerType.HTTP) {
            HttpOpenListener openListener = new HttpOpenListener(buffers, undertowOptions);
            openListener.setRootHandler(rootHandler);
            ChannelListener<AcceptingChannel<StreamConnection>> acceptListener =
                ChannelListeners.openListenerAdapter(openListener);
            AcceptingChannel<? extends StreamConnection> server =
                worker.createStreamConnectionServer(
                    new InetSocketAddress(Inet4Address.getByName(listener.host), listener.port),
                    acceptListener,
                    socketOptions);
            server.resumeAccepts();
            channels.add(server);
          } else if (listener.type == ListenerType.HTTPS) {
            ChannelListener<StreamConnection> openListener;

            HttpOpenListener httpOpenListener = new HttpOpenListener(buffers, undertowOptions);
            httpOpenListener.setRootHandler(rootHandler);

            boolean spdy = serverOptions.get(UndertowOptions.ENABLE_SPDY, false);
            boolean http2 = serverOptions.get(UndertowOptions.ENABLE_HTTP2, false);
            if (spdy || http2) {
              AlpnOpenListener alpn =
                  new AlpnOpenListener(buffers, undertowOptions, httpOpenListener);
              if (spdy) {
                SpdyOpenListener spdyListener =
                    new SpdyOpenListener(
                        buffers,
                        new ByteBufferSlicePool(BufferAllocator.BYTE_BUFFER_ALLOCATOR, 1024, 1024),
                        undertowOptions);
                spdyListener.setRootHandler(rootHandler);
                alpn.addProtocol(SpdyOpenListener.SPDY_3_1, spdyListener, 5);
              }
              if (http2) {
                Http2OpenListener http2Listener = new Http2OpenListener(buffers, undertowOptions);
                http2Listener.setRootHandler(rootHandler);
                alpn.addProtocol(Http2OpenListener.HTTP2, http2Listener, 10);
                alpn.addProtocol(Http2OpenListener.HTTP2_14, http2Listener, 7);
              }
              openListener = alpn;
            } else {
              openListener = httpOpenListener;
            }
            ChannelListener<AcceptingChannel<StreamConnection>> acceptListener =
                ChannelListeners.openListenerAdapter(openListener);
            XnioSsl xnioSsl;
            if (listener.sslContext != null) {
              xnioSsl =
                  new UndertowXnioSsl(
                      xnio,
                      OptionMap.create(Options.USE_DIRECT_BUFFERS, true),
                      listener.sslContext);
            } else {
              xnioSsl =
                  xnio.getSslProvider(
                      listener.keyManagers,
                      listener.trustManagers,
                      OptionMap.create(Options.USE_DIRECT_BUFFERS, true));
            }
            AcceptingChannel<SslConnection> sslServer =
                xnioSsl.createSslConnectionServer(
                    worker,
                    new InetSocketAddress(Inet4Address.getByName(listener.host), listener.port),
                    (ChannelListener) acceptListener,
                    socketOptions);
            sslServer.resumeAccepts();
            channels.add(sslServer);
          }
        }
      }

    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }