-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
87a87df
commit 4eb8068
Showing
13 changed files
with
196 additions
and
8 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
...essor-tests/rest-endpoints-descriptors/src/main/java/com/example/UserGrpcServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Copyright (C) 2021-2024 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package com.example; | ||
|
||
import akka.javasdk.annotations.GrpcEndpoint; | ||
|
||
@GrpcEndpoint | ||
public class UserGrpcServiceImpl { | ||
// would extend generated service stub and implement grpc methods but that's not important for this test | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
akka-javasdk-tests/src/test/java/akkajavasdk/components/grpc/TestGrpcServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright (C) 2021-2024 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package akkajavasdk.components.grpc; | ||
|
||
import akka.javasdk.annotations.GrpcEndpoint; | ||
import akkajavasdk.protocol.*; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionStage; | ||
|
||
@GrpcEndpoint | ||
public class TestGrpcServiceImpl implements TestGrpcService { | ||
@Override | ||
public CompletionStage<TestGrpcServiceOuterClass.Out> simple(TestGrpcServiceOuterClass.In in) { | ||
return CompletableFuture.completedFuture( | ||
TestGrpcServiceOuterClass.Out.newBuilder().setData(in.getData()).build() | ||
); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
akka-javasdk-tests/src/test/protobuf/akkajavasdk/test_grpc_service.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (C) 2021-2024 Lightbend Inc. <https://www.lightbend.com> | ||
|
||
// gRPC interface for a gRPC endpoint component | ||
|
||
syntax = "proto3"; | ||
|
||
package akkajavasdk; | ||
|
||
option java_package = "akkajavasdk.protocol"; | ||
|
||
message In { | ||
string data = 1; | ||
} | ||
|
||
message Out { | ||
string data = 1; | ||
} | ||
|
||
service TestGrpcService { | ||
rpc Simple(In) returns (Out) {}; | ||
} |
27 changes: 27 additions & 0 deletions
27
akka-javasdk/src/main/java/akka/javasdk/annotations/GrpcEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright (C) 2021-2024 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package akka.javasdk.annotations; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* Mark a class to be made available as a gRPC endpoint. The annotated class should extend a gRPC service interface | ||
* generated using Akka gRPC, be public and have a public constructor. | ||
* <p> | ||
* Annotated classes can accept the following types to the constructor: | ||
* <ul> | ||
* <li>{@link akka.javasdk.client.ComponentClient}</li> | ||
* <li>{@link akka.javasdk.http.HttpClientProvider}</li> | ||
* <li>{@link akka.javasdk.timer.TimerScheduler}</li> | ||
* <li>{@link akka.stream.Materializer}</li> | ||
* <li>{@link com.typesafe.config.Config}</li> | ||
* <li>Custom types provided by a {@link akka.javasdk.DependencyProvider} from the service setup</li> | ||
* </ul> | ||
*/ | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
public @interface GrpcEndpoint { | ||
} |
61 changes: 61 additions & 0 deletions
61
akka-javasdk/src/main/scala/akka/javasdk/impl/GrpcEndpointDescriptorFactory.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright (C) 2021-2024 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package akka.javasdk.impl | ||
|
||
import akka.actor.typed.ActorSystem | ||
import akka.grpc.AkkaGrpcGenerated | ||
import akka.grpc.scaladsl.InstancePerRequestFactory | ||
import akka.http.scaladsl.model.HttpRequest | ||
import akka.http.scaladsl.model.HttpResponse | ||
import akka.runtime.sdk.spi.GrpcEndpointDescriptor | ||
import akka.runtime.sdk.spi.GrpcEndpointRequestConstructionContext | ||
|
||
import scala.concurrent.Future | ||
|
||
object GrpcEndpointDescriptorFactory { | ||
|
||
def apply[T](grpcEndpointClass: Class[T], factory: () => T)(implicit | ||
system: ActorSystem[_]): GrpcEndpointDescriptor[T] = { | ||
val serviceDefinitionClass: Class[_] = grpcEndpointClass.getSuperclass | ||
|
||
// Validate that it is a grpc service (no supertype so this is the best we can do) | ||
if (serviceDefinitionClass.getAnnotation(classOf[AkkaGrpcGenerated]) == null) { | ||
throw new IllegalArgumentException( | ||
s"Class [${grpcEndpointClass.getName}] is with @GrpcEndpoint but the direct supertype [${serviceDefinitionClass.getName}] generated by Akka gRPC. " + | ||
"This is not supported.") | ||
} | ||
|
||
val instanceFactory = { (_: GrpcEndpointRequestConstructionContext) => | ||
factory() | ||
} | ||
|
||
// FIXME creating router is concrete method, not any interface, would be better to have an interface and not do reflectively | ||
// FIXME service lifecycle - one instance vs per request for HTTP endpoint, should we align? | ||
val handlerFactory = | ||
system.dynamicAccess | ||
.createInstanceFor[InstancePerRequestFactory[T]](serviceDefinitionClass.getName + "ScalaHandlerFactory", Nil) | ||
.get | ||
|
||
val routeFactory: (HttpRequest => T) => PartialFunction[HttpRequest, Future[HttpResponse]] = { serviceFactory => | ||
handlerFactory.partialInstancePerRequest( | ||
serviceFactory, | ||
"", | ||
// FIXME default error handler, is it fine to leave like this, should runtime define? | ||
PartialFunction.empty, | ||
system) | ||
} | ||
|
||
// Pick up generated companion object for file descriptor (for reflection) and creating router | ||
val companion = system.dynamicAccess.getObjectFor[akka.grpc.ServiceDescription](grpcEndpointClass.getName).get | ||
|
||
new GrpcEndpointDescriptor[T]( | ||
grpcEndpointClass.getName, | ||
companion.name, | ||
companion.descriptor, | ||
instanceFactory, | ||
routeFactory) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters