Skip to content

Commit

Permalink
Add support for customizing the auto-configured ClientResources
Browse files Browse the repository at this point in the history
Closes gh-26792
  • Loading branch information
snicoll committed Jun 14, 2021
1 parent f8555b9 commit a0cf1ee
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ class LettuceConnectionConfiguration extends RedisConnectionConfiguration {

@Bean(destroyMethod = "shutdown")
@ConditionalOnMissingBean(ClientResources.class)
DefaultClientResources lettuceClientResources() {
return DefaultClientResources.create();
DefaultClientResources lettuceClientResources(ObjectProvider<ClientResourcesBuilderCustomizer> customizers) {
DefaultClientResources.Builder builder = DefaultClientResources.builder();
customizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
return builder.build();
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import io.lettuce.core.cluster.ClusterClientOptions;
import io.lettuce.core.cluster.ClusterTopologyRefreshOptions;
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.junit.jupiter.api.Test;

Expand All @@ -53,6 +55,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.Mockito.mock;

/**
* Tests for {@link RedisAutoConfiguration}.
Expand Down Expand Up @@ -97,6 +100,16 @@ void testOverrideRedisConfiguration() {
});
}

@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
void testCustomizeRedisConfiguration() {
this.contextRunner.withUserConfiguration(CustomConfiguration.class).run((context) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
`ClientResources` can also be customized using `ClientResourcesBuilderCustomizer`.
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).
Expand Down

0 comments on commit a0cf1ee

Please sign in to comment.