Commit a0cf1eed authored by Stephane Nicoll's avatar Stephane Nicoll

Add support for customizing the auto-configured ClientResources

Closes gh-26792
parent f8555b90
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.data.redis;
import io.lettuce.core.resource.ClientResources;
/**
* Callback interface that can be implemented by beans wishing to customize the
* {@link ClientResources} via a {@link ClientResources.Builder} whilst retaining default
* auto-configuration.
*
* @author Stephane Nicoll
* @since 2.6.0
*/
public interface ClientResourcesBuilderCustomizer {
/**
* Customize the {@link ClientResources.Builder}.
* @param clientResourcesBuilder the builder to customize
*/
void customize(ClientResources.Builder clientResourcesBuilder);
}
...@@ -65,8 +65,10 @@ class LettuceConnectionConfiguration extends RedisConnectionConfiguration { ...@@ -65,8 +65,10 @@ class LettuceConnectionConfiguration extends RedisConnectionConfiguration {
@Bean(destroyMethod = "shutdown") @Bean(destroyMethod = "shutdown")
@ConditionalOnMissingBean(ClientResources.class) @ConditionalOnMissingBean(ClientResources.class)
DefaultClientResources lettuceClientResources() { DefaultClientResources lettuceClientResources(ObjectProvider<ClientResourcesBuilderCustomizer> customizers) {
return DefaultClientResources.create(); DefaultClientResources.Builder builder = DefaultClientResources.builder();
customizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
return builder.build();
} }
@Bean @Bean
......
...@@ -27,6 +27,8 @@ import io.lettuce.core.ClientOptions; ...@@ -27,6 +27,8 @@ import io.lettuce.core.ClientOptions;
import io.lettuce.core.cluster.ClusterClientOptions; import io.lettuce.core.cluster.ClusterClientOptions;
import io.lettuce.core.cluster.ClusterTopologyRefreshOptions; import io.lettuce.core.cluster.ClusterTopologyRefreshOptions;
import io.lettuce.core.cluster.ClusterTopologyRefreshOptions.RefreshTrigger; import io.lettuce.core.cluster.ClusterTopologyRefreshOptions.RefreshTrigger;
import io.lettuce.core.resource.DefaultClientResources;
import io.lettuce.core.tracing.Tracing;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
...@@ -53,6 +55,7 @@ import org.springframework.util.StringUtils; ...@@ -53,6 +55,7 @@ import org.springframework.util.StringUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.Mockito.mock;
/** /**
* Tests for {@link RedisAutoConfiguration}. * Tests for {@link RedisAutoConfiguration}.
...@@ -97,6 +100,16 @@ class RedisAutoConfigurationTests { ...@@ -97,6 +100,16 @@ class RedisAutoConfigurationTests {
}); });
} }
@Test
void testCustomizeClientResources() {
Tracing tracing = mock(Tracing.class);
this.contextRunner.withBean(ClientResourcesBuilderCustomizer.class, () -> (builder) -> builder.tracing(tracing))
.run((context) -> {
DefaultClientResources clientResources = context.getBean(DefaultClientResources.class);
assertThat(clientResources.tracing()).isEqualTo(tracing);
});
}
@Test @Test
void testCustomizeRedisConfiguration() { void testCustomizeRedisConfiguration() {
this.contextRunner.withUserConfiguration(CustomConfiguration.class).run((context) -> { this.contextRunner.withUserConfiguration(CustomConfiguration.class).run((context) -> {
......
...@@ -42,6 +42,7 @@ include::{docs-java}/features/nosql/redis/connecting/MyBean.java[] ...@@ -42,6 +42,7 @@ include::{docs-java}/features/nosql/redis/connecting/MyBean.java[]
---- ----
TIP: You can also register an arbitrary number of beans that implement `LettuceClientConfigurationBuilderCustomizer` for more advanced customizations. TIP: You can also register an arbitrary number of beans that implement `LettuceClientConfigurationBuilderCustomizer` for more advanced customizations.
`ClientResources` can also be customized using `ClientResourcesBuilderCustomizer`.
If you use Jedis, `JedisClientConfigurationBuilderCustomizer` is also available. If you use Jedis, `JedisClientConfigurationBuilderCustomizer` is also available.
If you add your own `@Bean` of any of the auto-configured types, it replaces the default (except in the case of `RedisTemplate`, when the exclusion is based on the bean name, `redisTemplate`, not its type). If you add your own `@Bean` of any of the auto-configured types, it replaces the default (except in the case of `RedisTemplate`, when the exclusion is based on the bean name, `redisTemplate`, not its type).
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment