use of io.undertow.util.StringReadChannelListener in project undertow by undertow-io.
the class LoadBalancingProxyHTTP2TestCase method testHttp2ClientMultipleStreamsThreadSafety.
@Test
public void testHttp2ClientMultipleStreamsThreadSafety() throws IOException, URISyntaxException, ExecutionException, InterruptedException, TimeoutException {
//not actually a proxy test
//but convent to put it here
UndertowXnioSsl ssl = new UndertowXnioSsl(DefaultServer.getWorker().getXnio(), OptionMap.EMPTY, DefaultServer.SSL_BUFFER_POOL, DefaultServer.createClientSslContext());
final UndertowClient client = UndertowClient.getInstance();
final ClientConnection connection = client.connect(new URI("https", null, DefaultServer.getHostAddress(), DefaultServer.getHostPort() + 1, "/", null, null), DefaultServer.getWorker(), ssl, DefaultServer.getBufferPool(), OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get();
final ExecutorService service = Executors.newFixedThreadPool(10);
try {
Deque<FutureResult<String>> futures = new ArrayDeque<>();
for (int i = 0; i < 100; ++i) {
final FutureResult<String> future = new FutureResult<>();
futures.add(future);
service.submit(new Callable<String>() {
@Override
public String call() throws Exception {
ClientRequest cr = new ClientRequest().setMethod(Methods.GET).setPath("/path").setProtocol(Protocols.HTTP_1_1);
connection.sendRequest(cr, new ClientCallback<ClientExchange>() {
@Override
public void completed(ClientExchange result) {
result.setResponseListener(new ClientCallback<ClientExchange>() {
@Override
public void completed(ClientExchange result) {
new StringReadChannelListener(DefaultServer.getBufferPool()) {
@Override
protected void stringDone(String string) {
future.setResult(string);
}
@Override
protected void error(IOException e) {
future.setException(e);
}
}.setup(result.getResponseChannel());
}
@Override
public void failed(IOException e) {
future.setException(e);
}
});
}
@Override
public void failed(IOException e) {
future.setException(e);
}
});
return null;
}
});
}
while (!futures.isEmpty()) {
FutureResult<String> future = futures.poll();
Assert.assertNotEquals(IoFuture.Status.WAITING, future.getIoFuture().awaitInterruptibly(10, TimeUnit.SECONDS));
Assert.assertEquals("/path", future.getIoFuture().get());
}
} finally {
service.shutdownNow();
}
}
use of io.undertow.util.StringReadChannelListener in project undertow by undertow-io.
the class ServerSentEventsServer method main.
public static void main(final String[] args) {
final ServerSentEventHandler sseHandler = serverSentEvents();
HttpHandler chatHandler = new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
new StringReadChannelListener(exchange.getConnection().getByteBufferPool()) {
@Override
protected void stringDone(String string) {
for (ServerSentEventConnection h : sseHandler.getConnections()) {
h.send(string);
}
}
@Override
protected void error(IOException e) {
}
}.setup(exchange.getRequestChannel());
}
};
Undertow server = Undertow.builder().addHttpListener(8080, "localhost").setHandler(path().addPrefixPath("/sse", sseHandler).addPrefixPath("/send", chatHandler).addPrefixPath("/", resource(new ClassPathResourceManager(ServerSentEventsServer.class.getClassLoader(), ServerSentEventsServer.class.getPackage())).addWelcomeFiles("index.html"))).build();
server.start();
}
Aggregations