A multi-threaded (e.g. file-based) web server with thread-pooling implemented in Java.
Any class that implements WebApp
interface can be plugged as the handler application. In this example
I'm using FileServingApp
(serves static files from a document root) as my handler application. The single method
that must be implemented is HttpResponse handle(HttpRequest request)
.
- Server receives a request.
- Server creates a Handler (Runnable) for this request.
- Handler parses the request into a HttpRequest.
- Handler passes the HttpRequest to the handler application.
- Handler writes the response received from the handler application to the output stream.
- Handler closes the streams.
A compiled version webserver-1.0-SNAPSHOT.jar
can be found in the root directory.
WebServer
- listens for connections and delegates them to worker threads.
Handler
- handles a single request, by parsing it and sending it to the handler application.
ExecutorMonitor
- prints server status once every 10 seconds.
FileServingApp
- an application that serves static files from it's document root.
gradle build
java -jar webserver-<version> <port> <threads> <document root>