/plugin-tester-java/src/main/java/example/myapp/helloworld/GreeterServer.java

https://github.com/akka/akka-grpc · Java · 47 lines · 27 code · 11 blank · 9 comment · 0 complexity · 38a62a72f5d0976e62dc75eea9a4253f MD5 · raw file

  1. /*
  2. * Copyright (C) 2018-2020 Lightbend Inc. <https://www.lightbend.com>
  3. */
  4. //#full-server
  5. package example.myapp.helloworld;
  6. import akka.actor.ActorSystem;
  7. import akka.http.javadsl.*;
  8. import akka.stream.ActorMaterializer;
  9. import akka.stream.Materializer;
  10. import com.typesafe.config.Config;
  11. import com.typesafe.config.ConfigFactory;
  12. import example.myapp.helloworld.grpc.*;
  13. import java.util.concurrent.CompletionStage;
  14. class GreeterServer {
  15. public static void main(String[] args) throws Exception {
  16. // important to enable HTTP/2 in ActorSystem's config
  17. Config conf = ConfigFactory.parseString("akka.http.server.preview.enable-http2 = on")
  18. .withFallback(ConfigFactory.defaultApplication());
  19. // Akka ActorSystem Boot
  20. ActorSystem sys = ActorSystem.create("HelloWorld", conf);
  21. run(sys).thenAccept(binding -> {
  22. System.out.println("gRPC server bound to: " + binding.localAddress());
  23. });
  24. // ActorSystem threads will keep the app alive until `system.terminate()` is called
  25. }
  26. public static CompletionStage<ServerBinding> run(ActorSystem sys) throws Exception {
  27. Materializer mat = ActorMaterializer.create(sys);
  28. // Instantiate implementation
  29. GreeterService impl = new GreeterServiceImpl(mat);
  30. return Http.get(sys).bindAndHandleAsync(
  31. GreeterServiceHandlerFactory.create(impl, sys),
  32. ConnectHttp.toHost("127.0.0.1", 8090),
  33. mat);
  34. }
  35. }
  36. //#full-server