@@ -11,6 +11,7 @@ This section describes how you can configure your grpc-spring-boot-starter clien
11
11
- [ Configuration via Beans] ( #configuration-via-beans )
12
12
- [ GrpcChannelConfigurer] ( #grpcchannelconfigurer )
13
13
- [ ClientInterceptor] ( #clientinterceptor )
14
+ - [ StubFactory] ( #stubfactory )
14
15
- [ StubTransformer] ( #stubtransformer )
15
16
16
17
## Additional Topics <!-- omit in toc -->
@@ -132,6 +133,45 @@ There are three ways to add a `ClientInterceptor` to your channel.
132
133
- Explicitly list them in the ` @GrpcClient#interceptors ` or ` @GrpcClient#interceptorNames ` field
133
134
- Use a ` StubTransformer ` and call ` stub.withInterceptors(ClientInterceptor... interceptors) `
134
135
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
+
135
175
### StubTransformer
136
176
137
177
The stub transformer allows you to modify ` Stub ` s right before they are injected to your beans.
0 commit comments