Run server:
./gradlew clean build && java -jar build/libs/http-server.jar --port=8080 --threadsNumber=2 --keepAliveTime=30000
Configuration example:
HttpServer.create()
.port(propertyResolver.getInt("port", 8080))
.threadsNumber(propertyResolver.getInt("threadsNumber", 20))
.keepAliveTime(propertyResolver.getInt("keepAliveTime", 30000))
.endpoint(Endpoint
.create()
.method("GET")
.path("/hello")
.handler((request, response) -> response.writeAsString("Hello!"))
.build())
.endpoint(Endpoint
.create()
.method("POST")
.path("/login")
.handler((request, response) -> response.writeAsString("Hello, " + request.getRequestBody()))
.build())
.endpoint(Endpoint
.create()
.method("GET")
.path("/favicon.ico")
.handler((request, response) -> {
writeFileFromResource("static/favicon.ico", response.getOutputStream());
response.addHeader("Content-Type", "image/x-icon");
response.addHeader("Content-Disposition", "attachment; filename=favicon.ico");
})
.build())
.endpoint(Endpoint
.create()
.method("GET")
.path("/")
.handler((request, response) -> {
writeFileFromResource("static/index.html", response.getOutputStream());
response.addHeader("Content-Type", "text/html;charset=utf-8");
response.addHeader("encoding", "utf-8");
})
.build())
.build()
.run();
Upload to S3:
./gradlew upload -Pbucket=your-bucket
Deploy to CloudFormation:
aws cloudformation create-stack \
--stack-name <stack-name> \
--template-body file://aws/ec2-java-server.yaml \
--parameters \
ParameterKey=KeyName,ParameterValue=<key-name> \
ParameterKey=JarUrl,ParameterValue=<jar-url> \
ParameterKey=JarParams,ParameterValue=<jar-params> \
--capabilities CAPABILITY_NAMED_IAM
- Sun http server implementation: sun/net/httpserver/ServerImpl.java:386
- Java NIO: Non-blocking Server
- Java NIO ServerSocketChannel
- HTTP/1.0 specification
- HTTP/1.1 specification
- Connection reuse only works since HTTP 1.1 (in curl at least). See Persistence section
- It seems that it is better to build a connection pool based on Java NIO (ServerSocketChannel, SocketChannel), where we can register a selector that listens on channels in one thread. When using Java standard IO, we basically have a thread for each socket that is listening on it. This method may be ineffective with a large number of connections.