Skip to content

Commit efa6909

Browse files
authored
feat: Access to gRPC clients in through workflow context (#2281)
1 parent ce1da39 commit efa6909

File tree

6 files changed

+49
-3
lines changed

6 files changed

+49
-3
lines changed

project/Dependencies.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ object Dependencies {
7676

7777
val scalapbCompilerPlugin = "com.thesamet.scalapb" %% "compilerplugin" % scalapb.compiler.Version.scalapbVersion
7878
val scalaPbValidateCore = "com.thesamet.scalapb" %% "scalapb-validate-core" % "0.3.4"
79-
val sbtProtoc = "com.thesamet" % "sbt-protoc" % "1.0.0"
79+
// keep aligned with the sbt-protoc plugin version in plugins.sbt
80+
val sbtProtoc = "com.thesamet" % "sbt-protoc" % "1.0.3"
8081

8182
val akkaGrpc = "com.lightbend.akka.grpc" % "sbt-akka-grpc" % akka.grpc.gen.BuildInfo.version
8283
val scalaCollectionCompat = "org.scala-lang.modules" %% "scala-collection-compat" % "2.10.0"

project/plugins.sbt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.12")
1212
addSbtPlugin("net.aichler" % "sbt-jupiter-interface" % "0.11.0")
1313
addSbtPlugin("com.thesamet" % "sbt-protoc" % "1.0.3")
1414
addSbtPlugin("com.thoughtworks.sbt-api-mappings" % "sbt-api-mappings" % "3.0.2")
15+
16+
// force bumped because of CI not finding 1.0.0 which was the transitive version
17+
addSbtPlugin("org.portable-scala" % "sbt-platform-deps" % "1.0.2")

sdk/java-sdk-protobuf/src/main/java/kalix/javasdk/workflow/WorkflowContext.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,17 @@ public interface WorkflowContext extends Context {
1313
* @return The workflow id.
1414
*/
1515
String workflowId();
16+
17+
/**
18+
* Get an Akka gRPC client for the given service name. The same client instance is shared across
19+
* components in the application. The lifecycle of the client is managed by the SDK and it should
20+
* not be stopped by user code.
21+
*
22+
* @param <T> The "service" interface generated for the service by Akka gRPC
23+
* @param clientClass The class of a gRPC service generated by Akka gRPC
24+
* @param service The name of the service to connect to, either a name of another Kalix service or
25+
* an external service where connection details are configured under
26+
* `akka.grpc.client.[service-name]` in `application.conf`.
27+
*/
28+
<T> T getGrpcClient(Class<T> clientClass, String service);
1629
}

sdk/java-sdk-protobuf/src/main/scala/kalix/javasdk/impl/workflow/WorkflowImpl.scala

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,16 @@ private[kalix] final class CommandContextImpl(
361361
system: ActorSystem)
362362
extends AbstractContext(system)
363363
with CommandContext
364-
with ActivatableContext
364+
with ActivatableContext {
365+
override def getGrpcClient[T](clientClass: Class[T], service: String): T =
366+
GrpcClients(system).getGrpcClient(clientClass, service)
367+
368+
}
365369

366370
private[kalix] final class WorkflowContextImpl(override val workflowId: String, system: ActorSystem)
367371
extends AbstractContext(system)
368-
with WorkflowContext
372+
with WorkflowContext {
373+
override def getGrpcClient[T](clientClass: Class[T], service: String): T =
374+
GrpcClients(system).getGrpcClient(clientClass, service)
375+
376+
}

sdk/scala-sdk-protobuf/src/main/scala/kalix/scalasdk/impl/workflow/WorkflowAdapters.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ private[scalasdk] final class ScalaCommandContextAdapter(val javaSdkContext: jav
203203
case ctx: javasdk.impl.AbstractContext => ctx.getComponentGrpcClient(serviceClass)
204204
}
205205

206+
override def getGrpcClient[T](clientClass: Class[T], service: String): T =
207+
javaSdkContext.getGrpcClient(clientClass, service)
208+
206209
override def materializer(): Materializer = javaSdkContext.materializer()
207210

208211
override def workflowId: String = javaSdkContext.workflowId()
@@ -211,6 +214,9 @@ private[scalasdk] final class ScalaCommandContextAdapter(val javaSdkContext: jav
211214
private[scalasdk] final class ScalaWorkflowContextAdapter(javaSdkContext: javasdk.workflow.WorkflowContext)
212215
extends WorkflowContext {
213216

217+
override def getGrpcClient[T](clientClass: Class[T], service: String): T =
218+
javaSdkContext.getGrpcClient(clientClass, service)
219+
214220
override def materializer(): Materializer = javaSdkContext.materializer()
215221

216222
override def workflowId: String = javaSdkContext.workflowId()

sdk/scala-sdk-protobuf/src/main/scala/kalix/scalasdk/workflow/WorkflowContext.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,19 @@ trait WorkflowContext extends Context {
1515
* The workflow id.
1616
*/
1717
def workflowId: String
18+
19+
/**
20+
* Get an Akka gRPC client for the given service name. The same client instance is shared across components in the
21+
* application. The lifecycle of the client is managed by the SDK and it should not be stopped by user code.
22+
*
23+
* @tparam T
24+
* The "service" interface generated for the service by Akka gRPC
25+
* @param clientClass
26+
* The class of a gRPC service generated by Akka gRPC
27+
* @param service
28+
* The name of the service to connect to, either a name of another Kalix service or an external service where
29+
* connection details are configured under `akka.grpc.client.[service-name]` in `application.conf`.
30+
*/
31+
def getGrpcClient[T](clientClass: Class[T], service: String): T
32+
1833
}

0 commit comments

Comments
 (0)