this is a somewhat simplified example that gets 75-80k req/s vs 85-90 for the more verbose version, but the use of the async api is the same
____my code is below___
import io.undertow.Handlers;
import io.undertow.Undertow;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.Headers;
import java.nio.ByteBuffer;
import java.util.Timer;
import java.util.TimerTask;
public final class UtowAsync implements HttpHandler {
public static void main(String[] args) throws Exception {
Undertow.builder()
.addHttpListener(9097,"0.0.0.0")
.setHandler(Handlers.path().addPrefixPath("/hello",new UtowAsync()))
.build()
.start();
}
int num = 0;
HttpServerExchange acv[] = new HttpServerExchange[10000];
byte [] bytes = "hello world".getBytes();
ByteBuffer buf = ByteBuffer.allocate(bytes.length).put(bytes);
{ buf.flip(); }
synchronized void store(HttpServerExchange async) {
if (async==null) while (num > 0) {
reply(acv[--num]);
acv[num] = null;
}
else acv[num++] = async;
}
void reply(HttpServerExchange exchange) {
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
exchange.getResponseSender().send(buf.duplicate());
}
public void handleRequest(final HttpServerExchange exchange) throws Exception {
store(exchange);
exchange.dispatch();
}
{
new Timer().schedule(new TimerTask() { public void run() {
UtowAsync.this.store(null);
} },10,10);
}
}