Skip to content

Instantly share code, notes, and snippets.

@rmbarnett-rice
Created July 24, 2018 23:39
Show Gist options
  • Save rmbarnett-rice/668db6b4e9f8f8da7093a3659b6ae2b5 to your computer and use it in GitHub Desktop.
Save rmbarnett-rice/668db6b4e9f8f8da7093a3659b6ae2b5 to your computer and use it in GitHub Desktop.
import io.undertow.Undertow;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.HttpString;
import org.xnio.Options;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicInteger;
public class Server
{
private static final AtomicInteger _numOverlappingRequests = new AtomicInteger(0);
private static volatile int _maxSeenOverlappingRequests = 0;
private static final int IO_THREAD_COUNT = 8; // we hope to see this number printed on the screen!
public static void main(String[] args)
{
int listenPort = 8080;
Undertow server = Undertow.builder()
.addHttpListener(listenPort, "0.0.0.0")
.setSocketOption(Options.BACKLOG, 1000)
.setIoThreads(IO_THREAD_COUNT)
.setHandler(new HttpHandler()
{
@Override
public void handleRequest(HttpServerExchange httpServerExchange) throws Exception
{
int overlappingRequestsCount = _numOverlappingRequests.incrementAndGet();
if(overlappingRequestsCount > _maxSeenOverlappingRequests)
{
_maxSeenOverlappingRequests = overlappingRequestsCount;
System.out.println(_maxSeenOverlappingRequests);
}
long sleepUntil = System.currentTimeMillis() + 1000;
while(System.currentTimeMillis() < sleepUntil)
{
}
httpServerExchange.getResponseHeaders().put(new HttpString("Content-Type"), "text/plain");
httpServerExchange.setStatusCode(200);
httpServerExchange.getResponseSender().send(ByteBuffer.wrap("Hello".getBytes()));
httpServerExchange.endExchange();
_numOverlappingRequests.decrementAndGet();
}
}).build();
server.start();
System.out.println("\n\nServer started on port " + listenPort);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment