Skip to content

Commit d616ed7

Browse files
committed
Document StubFactory (Fixes #381)
1 parent 833058b commit d616ed7

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

docs/en/client/configuration.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ This section describes how you can configure your grpc-spring-boot-starter clien
1111
- [Configuration via Beans](#configuration-via-beans)
1212
- [GrpcChannelConfigurer](#grpcchannelconfigurer)
1313
- [ClientInterceptor](#clientinterceptor)
14+
- [StubFactory](#stubfactory)
1415
- [StubTransformer](#stubtransformer)
1516

1617
## Additional Topics <!-- omit in toc -->
@@ -132,6 +133,45 @@ There are three ways to add a `ClientInterceptor` to your channel.
132133
- Explicitly list them in the `@GrpcClient#interceptors` or `@GrpcClient#interceptorNames` field
133134
- Use a `StubTransformer` and call `stub.withInterceptors(ClientInterceptor... interceptors)`
134135

136+
### StubFactory
137+
138+
A `StubFactory` is used to create a `Stub` of a specific type. The registered stub factories will be checked in order
139+
and the first applicable factory will be used to create the stub.
140+
141+
This library has build in support for the `Stub` types defined in grpc-java:
142+
143+
- [`AsyncStubs`](https://grpc.github.io/grpc-java/javadoc/io/grpc/stub/AbstractAsyncStub.html)
144+
- [`BlockingStubs`](https://grpc.github.io/grpc-java/javadoc/io/grpc/stub/AbstractBlockingStub.html)
145+
- [`FutureStubs`](https://grpc.github.io/grpc-java/javadoc/io/grpc/stub/AbstractFutureStub.html)
146+
147+
But you can easily add support for other `Stub` types by adding a custom `StubFactory` to your application context.
148+
149+
````java
150+
@Component
151+
public class MyCustomStubFactory implements StubFactory {
152+
153+
@Override
154+
public MyCustomStub<?> createStub(Class<? extends AbstractStub<?>> stubType, Channel channel) {
155+
try {
156+
Class<?> enclosingClass = stubType.getEnclosingClass();
157+
Method factoryMethod = enclosingClass.getMethod("newMyBetterFutureStub", Channel.class);
158+
return stubType.cast(factoryMethod.invoke(null, channel));
159+
} catch (Exception e) {
160+
throw new BeanInstantiationException(stubType, "Failed to create gRPC stub", e);
161+
}
162+
}
163+
164+
@Override
165+
public boolean isApplicable(Class<? extends AbstractStub<?>> stubType) {
166+
return AbstractMyCustomStub.class.isAssignableFrom(stubType);
167+
}
168+
169+
}
170+
````
171+
172+
> **Note:** Please report missing stub types (and the corresponding library) in our issue tracker so that we can add
173+
> support if possible.
174+
135175
### StubTransformer
136176

137177
The stub transformer allows you to modify `Stub`s right before they are injected to your beans.

docs/en/client/getting-started.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ This section assumes that you have already defined and generated your [Protobuf
126126
automatically attach it to all `Stub`s (**NOT** `Channel`s). The
127127
[`CallCredentialsHelper`](https://javadoc.io/page/net.devh/grpc-client-spring-boot-autoconfigure/latest/net/devh/boot/grpc/client/security/CallCredentialsHelper.html)
128128
utility class helps you to create commonly used `CallCredentials` types and related `StubTransformer`.
129+
- [`StubFactory`](https://javadoc.io/page/net.devh/grpc-client-spring-boot-autoconfigure/latest/net/devh/boot/grpc/client/stubfactory/StubFactory.html):
130+
A factory that can be used to create a specfic `Stub` type from a `Channel`. Multiple `StubFactory`s can be registered to support different stub types.
131+
See also [Configuration -> StubFactory](configuration.md#stubfactory).
129132
- [`StubTransformer`](https://javadoc.io/page/net.devh/grpc-client-spring-boot-autoconfigure/latest/net/devh/boot/grpc/client/inject/StubTransformer.html):
130133
A transformer that will be applied to all client `Stub`s before they are injected.
131134
See also [Configuration -> StubTransformer](configuration.md#stubtransformer).

0 commit comments

Comments
 (0)