Skip to content

Instantly share code, notes, and snippets.

@rmbarnett-rice
Created July 25, 2018 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmbarnett-rice/1179c4ad1d3344bb247c8b8daed3e4fa to your computer and use it in GitHub Desktop.
Save rmbarnett-rice/1179c4ad1d3344bb247c8b8daed3e4fa 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 final Object _maxSeenOverlappingRequestsMutex = new Object();
private static 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") // 0.0.0.0 means listen on any ip addr
.setSocketOption(Options.BACKLOG, 1000)
.setIoThreads(IO_THREAD_COUNT)
.setHandler(new HttpHandler()
{
@Override
public void handleRequest(HttpServerExchange httpServerExchange) throws Exception
{
int overlappingRequestsCount = _numOverlappingRequests.incrementAndGet();
synchronized (_maxSeenOverlappingRequestsMutex)
{
if (overlappingRequestsCount > _maxSeenOverlappingRequests)
{
_maxSeenOverlappingRequests = overlappingRequestsCount;
System.out.println(_maxSeenOverlappingRequests);
}
}
Thread.sleep(1000);
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