/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
- /*
- * Copyright (C) 2018-2020 Lightbend Inc. <https://www.lightbend.com>
- */
- //#full-server
- package example.myapp.helloworld;
- import akka.actor.ActorSystem;
- import akka.http.javadsl.*;
- import akka.stream.ActorMaterializer;
- import akka.stream.Materializer;
- import com.typesafe.config.Config;
- import com.typesafe.config.ConfigFactory;
- import example.myapp.helloworld.grpc.*;
- import java.util.concurrent.CompletionStage;
- class GreeterServer {
- public static void main(String[] args) throws Exception {
- // important to enable HTTP/2 in ActorSystem's config
- Config conf = ConfigFactory.parseString("akka.http.server.preview.enable-http2 = on")
- .withFallback(ConfigFactory.defaultApplication());
- // Akka ActorSystem Boot
- ActorSystem sys = ActorSystem.create("HelloWorld", conf);
- run(sys).thenAccept(binding -> {
- System.out.println("gRPC server bound to: " + binding.localAddress());
- });
- // ActorSystem threads will keep the app alive until `system.terminate()` is called
- }
- public static CompletionStage<ServerBinding> run(ActorSystem sys) throws Exception {
- Materializer mat = ActorMaterializer.create(sys);
- // Instantiate implementation
- GreeterService impl = new GreeterServiceImpl(mat);
- return Http.get(sys).bindAndHandleAsync(
- GreeterServiceHandlerFactory.create(impl, sys),
- ConnectHttp.toHost("127.0.0.1", 8090),
- mat);
- }
- }
- //#full-server