diff --git a/docs/src/main/asciidoc/mp/grpc/client.adoc b/docs/src/main/asciidoc/mp/grpc/client.adoc index 2ce1e87b7e2..bf2b196de1d 100644 --- a/docs/src/main/asciidoc/mp/grpc/client.adoc +++ b/docs/src/main/asciidoc/mp/grpc/client.adoc @@ -35,6 +35,7 @@ include::{rootdir}/includes/mp.adoc[] - <> ** <> ** <> +** <> - <> == Overview @@ -133,7 +134,7 @@ include::{sourcedir}/mp/grpc/GrpcSnippets.java[tag=snippet_5, indent=0] service when it differs from the interface name, as it is the case in this example. <2> The `@Grpc.GrpcChannel` annotation is the qualifier that supplies the channel name. This is the same name as used in the channel configuration in the examples provided in -the <>. +the <>. There is no need to write any code to implement the client. The Helidon MP gRPC API will create a dynamic proxy for the interface using the information from the annotations and @@ -162,14 +163,33 @@ include::{sourcedir}/mp/grpc/GrpcSnippets.java[tag=snippet_7, indent=0] <1> The `@Inject` annotation tells CDI to inject the client implementation. <2> The `@Grpc.GrpcProxy` annotation is used by the CDI container to match the injection point to -the gRPC MP APIs provider. +the gRPC MP API provider. When the CDI container instantiates `MyAppBean`, it will inject a dynamic proxy into the `stringServiceClient` field, and then provide the necessary logic for the proxy methods to convert a method call into a gRPC call. In the example above, there is no need to use a channel directly. The correct channel is added to -the dynamic client proxy internally by the Helidon MP gRPC APIs. +the dynamic client proxy internally by the Helidon MP gRPC API. + +=== Injecting Channels + +Channels can also be directly injected into application bean instances. +The Helidon gRPC client API has CDI producers to inject `io.grpc.Channel` instances. + +For example, a class might have an injectable `io.grpc.Channel` field as follows: + +[source,java] +---- +include::{sourcedir}/mp/grpc/GrpcSnippets.java[tag=snippet_8, indent=4] +---- + +<1> The `@Inject` annotation tells CDI to inject the channel. +<2> The `@Grpc.GrpcChannel` annotation supplies the channel name. +This is the same name as used in the channel configuration in the examples provided in +the <>. + +An injected channel can be used, for example, when directly instantiating `protoc` generated stubs. == Examples diff --git a/docs/src/main/java/io/helidon/docs/mp/grpc/GrpcSnippets.java b/docs/src/main/java/io/helidon/docs/mp/grpc/GrpcSnippets.java index 54f15767343..6e83cb4fa25 100644 --- a/docs/src/main/java/io/helidon/docs/mp/grpc/GrpcSnippets.java +++ b/docs/src/main/java/io/helidon/docs/mp/grpc/GrpcSnippets.java @@ -136,4 +136,13 @@ public class MyAppBean { } // end::snippet_7[] } + + class Snippet8 { + + // tag::snippet_8[] + @Inject // <1> + @Grpc.GrpcChannel("string-channel") // <2> + private Channel channel; + // end::snippet_8[] + } } diff --git a/microprofile/grpc/client/src/test/java/io/helidon/microprofile/grpc/client/ChannelInjectionTest.java b/microprofile/grpc/client/src/test/java/io/helidon/microprofile/grpc/client/ChannelInjectionTest.java index 3a427d2f42d..5c17ff64123 100644 --- a/microprofile/grpc/client/src/test/java/io/helidon/microprofile/grpc/client/ChannelInjectionTest.java +++ b/microprofile/grpc/client/src/test/java/io/helidon/microprofile/grpc/client/ChannelInjectionTest.java @@ -22,9 +22,9 @@ import jakarta.inject.Inject; import org.junit.jupiter.api.Test; +import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; @HelidonTest class ChannelInjectionTest { @@ -44,7 +44,7 @@ class ChannelInjectionTest { void testInjection() { assertThat(echoChannel1, notNullValue()); assertThat(echoChannel2, notNullValue()); - assertEquals(echoChannel1.getClass(), echoChannel2.getClass()); - assertEquals(echoChannel1.authority(), echoChannel2.authority()); + assertThat(echoChannel1.getClass(), equalTo(echoChannel2.getClass())); + assertThat(echoChannel1.authority(), equalTo(echoChannel2.authority())); } }