public UndertowXhrTransport(OptionMap optionMap) throws IOException { Assert.notNull(optionMap, "OptionMap is required"); this.optionMap = optionMap; this.httpClient = UndertowClient.getInstance(); this.worker = Xnio.getInstance().createWorker(optionMap); this.undertowBufferSupport = (undertow13Present ? new Undertow13BufferSupport() : new UndertowXnioBufferSupport()); }
@BeforeClass public static void setup() throws URISyntaxException { final SessionCookieConfig sessionConfig = new SessionCookieConfig(); int port = DefaultServer.getHostPort("default"); server1 = Undertow.builder() .addHttpListener(port + 1, DefaultServer.getHostAddress("default")) .setSocketOption(Options.REUSE_ADDRESSES, true) .setHandler( jvmRoute( "JSESSIONID", "s1", path() .addPrefixPath( "/session", new SessionAttachmentHandler( new AbstractLoadBalancingProxyTestCase.SessionTestHandler( sessionConfig), new InMemorySessionManager(""), sessionConfig)) .addPrefixPath( "/name", new AbstractLoadBalancingProxyTestCase.StringSendHandler("server1")))) .build(); server2 = Undertow.builder() .addHttpListener(port + 2, DefaultServer.getHostAddress("default")) .setSocketOption(Options.REUSE_ADDRESSES, true) .setHandler( jvmRoute( "JSESSIONID", "s2", path() .addPrefixPath( "/session", new SessionAttachmentHandler( new AbstractLoadBalancingProxyTestCase.SessionTestHandler( sessionConfig), new InMemorySessionManager(""), sessionConfig)) .addPrefixPath( "/name", new AbstractLoadBalancingProxyTestCase.StringSendHandler("server2")))) .build(); server1.start(); server2.start(); LoadBalancingProxyClient.HostSelector hostSelector = new LoadBalancingProxyClient.HostSelector() { @Override public int selectHost(LoadBalancingProxyClient.Host[] availableHosts) { return 0; } }; DefaultServer.setRootHandler( new ProxyHandler( new LoadBalancingProxyClient(UndertowClient.getInstance(), null, hostSelector) .setConnectionsPerThread(1) .addHost( new URI( "http", null, DefaultServer.getHostAddress("default"), port + 1, null, null, null), "s1") .addHost( new URI( "http", null, DefaultServer.getHostAddress("default"), port + 2, null, null, null), "s2"), 10000, ResponseCodeHandler.HANDLE_404)); }
@Override public synchronized void start(StartContext context) throws StartException { super.start(context); SecurityRealm realm = securityRealm.getOptionalValue(); // TODO: SSL support for the client // TODO: wire up idle timeout when new version of undertow arrives final ModCluster.Builder modClusterBuilder; final XnioWorker worker = workerInjectedValue.getValue(); if (realm == null) { modClusterBuilder = ModCluster.builder(worker); } else { SSLContext sslContext = realm.getSSLContext(); 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 .setHealthCheckInterval(healthCheckInterval) .setMaxRequestTime(maxRequestTime) .setCacheConnections(cachedConnections) .setQueueNewRequests(requestQueueSize > 0) .setRequestQueueSize(requestQueueSize) .setRemoveBrokenNodes(removeBrokenNodes) .setTtl(connectionIdleTimeout) .setMaxConnections(connectionsPerThread) .setUseAlias(useAlias); modCluster = modClusterBuilder.build(); MCMPConfig.Builder builder = MCMPConfig.builder(); InetAddress multicastAddress = advertiseSocketBinding.getValue().getMulticastAddress(); if (multicastAddress == null) { throw UndertowLogger.ROOT_LOGGER.advertiseSocketBindingRequiresMulticastAddress(); } if (advertiseFrequency > 0) { builder .enableAdvertise() .setAdvertiseAddress( advertiseSocketBinding.getValue().getSocketAddress().getAddress().getHostAddress()) .setAdvertiseGroup(multicastAddress.getHostAddress()) .setAdvertisePort(advertiseSocketBinding.getValue().getPort()) .setAdvertiseFrequency(advertiseFrequency) .setPath(advertisePath) .setProtocol(advertiseProtocol) .setSecurityKey(securityKey); } builder.setManagementHost(managementSocketBinding.getValue().getSocketAddress().getHostName()); builder.setManagementPort(managementSocketBinding.getValue().getSocketAddress().getPort()); config = builder.build(); if (advertiseFrequency > 0) { try { modCluster.advertise(config); } catch (IOException e) { throw new RuntimeException(e); } } modCluster.start(); }