Skip to content

Commit 54a8c34

Browse files
committed
Add order to client props customizer
Sets the `@Order` attribute on the ClientPropertiesChannelBuilderCustomizer which allows users to order their customizers before or after the client props customizer. Also adds order to the de/compressor channel builder customizers. Fixes #359 Signed-off-by: onobc <chris.bono@gmail.com>
1 parent ff1044a commit 54a8c34

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

spring-grpc-client-spring-boot-autoconfigure/src/main/java/org/springframework/boot/grpc/client/autoconfigure/GrpcClientAutoConfiguration.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.context.annotation.Bean;
2929
import org.springframework.context.annotation.Configuration;
3030
import org.springframework.context.annotation.Import;
31+
import org.springframework.core.annotation.Order;
3132
import org.springframework.grpc.client.ChannelCredentialsProvider;
3233
import org.springframework.grpc.client.ClientInterceptorsConfigurer;
3334
import org.springframework.grpc.client.CoroutineStubFactory;
@@ -46,6 +47,26 @@
4647
GrpcChannelFactoryConfigurations.InProcessChannelFactoryConfiguration.class, ClientScanConfiguration.class })
4748
public final class GrpcClientAutoConfiguration {
4849

50+
/**
51+
* Order applied to the {@link ClientPropertiesChannelBuilderCustomizer} used to apply
52+
* {@link GrpcClientProperties} to channel builders.
53+
*/
54+
public static final int CLIENT_PROPS_CHANNEL_BUILDER_CUSTOMIZER_ORDER = 0;
55+
56+
/**
57+
* Order applied to the {@link GrpcChannelBuilderCustomizer
58+
* compressionClientCustomizer} used to set the compressor registry on the channel
59+
* builder.
60+
*/
61+
public static final int COMPRESSION_CHANNEL_BUILDER_CUSTOMIZER_ORDER = 1;
62+
63+
/**
64+
* Order applied to the {@link GrpcChannelBuilderCustomizer
65+
* decompressionClientCustomizer} used to set the decompressor registry on the channel
66+
* builder.
67+
*/
68+
public static final int DECOMPRESSION_CHANNEL_BUILDER_CUSTOMIZER_ORDER = 2;
69+
4970
@Bean
5071
@ConditionalOnMissingBean
5172
ClientInterceptorsConfigurer clientInterceptorsConfigurer(ApplicationContext applicationContext) {
@@ -59,20 +80,23 @@ NamedChannelCredentialsProvider channelCredentialsProvider(SslBundles bundles, G
5980
}
6081

6182
@Bean
83+
@Order(CLIENT_PROPS_CHANNEL_BUILDER_CUSTOMIZER_ORDER)
6284
<T extends ManagedChannelBuilder<T>> GrpcChannelBuilderCustomizer<T> clientPropertiesChannelCustomizer(
6385
GrpcClientProperties properties) {
6486
return new ClientPropertiesChannelBuilderCustomizer<>(properties);
6587
}
6688

6789
@ConditionalOnBean(CompressorRegistry.class)
6890
@Bean
91+
@Order(COMPRESSION_CHANNEL_BUILDER_CUSTOMIZER_ORDER)
6992
<T extends ManagedChannelBuilder<T>> GrpcChannelBuilderCustomizer<T> compressionClientCustomizer(
7093
CompressorRegistry registry) {
7194
return (name, builder) -> builder.compressorRegistry(registry);
7295
}
7396

7497
@ConditionalOnBean(DecompressorRegistry.class)
7598
@Bean
99+
@Order(DECOMPRESSION_CHANNEL_BUILDER_CUSTOMIZER_ORDER)
76100
<T extends ManagedChannelBuilder<T>> GrpcChannelBuilderCustomizer<T> decompressionClientCustomizer(
77101
DecompressorRegistry registry) {
78102
return (name, builder) -> builder.decompressorRegistry(registry);

spring-grpc-client-spring-boot-autoconfigure/src/test/java/org/springframework/boot/grpc/client/autoconfigure/GrpcClientAutoConfigurationTests.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.springframework.grpc.client.InProcessGrpcChannelFactory;
4949
import org.springframework.grpc.client.NettyGrpcChannelFactory;
5050
import org.springframework.grpc.client.ShadedNettyGrpcChannelFactory;
51+
import org.springframework.test.util.ReflectionTestUtils;
5152

5253
import io.grpc.Codec;
5354
import io.grpc.CompressorRegistry;
@@ -230,6 +231,21 @@ void channelBuilderCustomizersAutoConfiguredAsExpected() {
230231
ChannelBuilderCustomizersConfig.CUSTOMIZER_FOO));
231232
}
232233

234+
@Test
235+
void userDefinedCustomizerCanRunBeforeAndAfterClientPropsCustomizer() {
236+
this.contextRunner().withUserConfiguration(UserClientPropsCustomizerConfig.class).run((context) -> {
237+
// NOTE: AssertJ "extract list + satisfies" balks about generic types
238+
// so we have to do this the old fashion way.
239+
var clientPropsCustomizer = context.getBean(ClientPropertiesChannelBuilderCustomizer.class);
240+
var channelBuilderCustomizers = context.getBean(ChannelBuilderCustomizers.class);
241+
List<GrpcChannelBuilderCustomizer<?>> customizers = (List<GrpcChannelBuilderCustomizer<?>>) ReflectionTestUtils
242+
.getField(channelBuilderCustomizers, "customizers");
243+
assertThat(customizers).isNotNull();
244+
assertThat(customizers).containsSequence(UserClientPropsCustomizerConfig.CUSTOMIZER_PRE_CLIENT_PROPS,
245+
clientPropsCustomizer, UserClientPropsCustomizerConfig.CUSTOMIZER_POST_CLIENT_PROPS);
246+
});
247+
}
248+
233249
@Test
234250
void clientScanConfigurationAutoConfiguredAsExpected() {
235251
this.contextRunner().run((context) -> assertThat(context).hasSingleBean(ClientScanConfiguration.class));
@@ -447,4 +463,25 @@ GrpcChannelBuilderCustomizer<?> customizerBar() {
447463

448464
}
449465

466+
@Configuration(proxyBeanMethods = false)
467+
static class UserClientPropsCustomizerConfig {
468+
469+
static GrpcChannelBuilderCustomizer<?> CUSTOMIZER_PRE_CLIENT_PROPS = mock();
470+
471+
static GrpcChannelBuilderCustomizer<?> CUSTOMIZER_POST_CLIENT_PROPS = mock();
472+
473+
@Bean
474+
@Order(GrpcClientAutoConfiguration.CLIENT_PROPS_CHANNEL_BUILDER_CUSTOMIZER_ORDER - 1)
475+
GrpcChannelBuilderCustomizer<?> customizerFoo() {
476+
return CUSTOMIZER_PRE_CLIENT_PROPS;
477+
}
478+
479+
@Bean
480+
@Order(GrpcClientAutoConfiguration.CLIENT_PROPS_CHANNEL_BUILDER_CUSTOMIZER_ORDER + 1)
481+
GrpcChannelBuilderCustomizer<?> customizerBar() {
482+
return CUSTOMIZER_POST_CLIENT_PROPS;
483+
}
484+
485+
}
486+
450487
}

0 commit comments

Comments
 (0)