import akka.actor.ActorSystem;
import akka.http.javadsl.Http;
import akka.http.javadsl.ServerBinding;
import akka.http.javadsl.server.AllDirectives;
import akka.http.javadsl.server.Route;
import akka.stream.ActorMaterializer;
import akka.stream.Materializer;
public class AkkaHttpServer extends AllDirectives {
public static void main(String[] args) throws Exception {
ActorSystem actorSystem = ActorSystem.create();
Materializer materializer = ActorMaterializer.create(actorSystem);
AkkaHttpServer server = new AkkaHttpServer();
Route route = server.createRoute();
Http http = Http.get(actorSystem);
ServerBinding binding = http.bindAndHandle(route.flow(actorSystem, materializer),
ConnectHttp.toHost("localhost", 8080), materializer);
System.out.println("Server started on localhost:8080");
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
binding.unbind();
actorSystem.terminate();
System.out.println("Server stopped");
}));
}
private Route createRoute() {
return path("hello", () ->
get(() ->
complete("<h1>Hello, Akka HTTP!</h1>")
)
);
}
}
akka {
actor {
provider = "akka.actor.LocalActorRefProvider"
}
http.server {
preview.enable-http2 = off
backpressure {
buffer-size = 32
max-connections = 1024
}
pipelining-limit = 8
idle-timeout = 10 s
request-timeout = 20 s
bind-timeout = 20 s
parsing.illegal-header-warnings = off
errors {
include-stack-trace = on
}
}
}