Introduce JedisClientConfigBuilderCustomizer to support client config customization.
We now support customization of Jedis' JedisClientConfig that is used for various client configurations for setting extended properties that Spring Data Redis doesn't configure itself. Closes: #3007 Original Pull Request: #3014
This commit is contained in:
committed by
Christoph Strobl
parent
cf42be1c42
commit
feb534f3e8
@@ -34,6 +34,7 @@ import org.springframework.lang.Nullable;
|
||||
*/
|
||||
class DefaultJedisClientConfiguration implements JedisClientConfiguration {
|
||||
|
||||
private final Optional<JedisClientConfigBuilderCustomizer> customizer;
|
||||
private final boolean useSsl;
|
||||
private final Optional<SSLSocketFactory> sslSocketFactory;
|
||||
private final Optional<SSLParameters> sslParameters;
|
||||
@@ -44,11 +45,13 @@ class DefaultJedisClientConfiguration implements JedisClientConfiguration {
|
||||
private final Duration readTimeout;
|
||||
private final Duration connectTimeout;
|
||||
|
||||
DefaultJedisClientConfiguration(boolean useSsl, @Nullable SSLSocketFactory sslSocketFactory,
|
||||
DefaultJedisClientConfiguration(@Nullable JedisClientConfigBuilderCustomizer customizer, boolean useSsl,
|
||||
@Nullable SSLSocketFactory sslSocketFactory,
|
||||
@Nullable SSLParameters sslParameters, @Nullable HostnameVerifier hostnameVerifier, boolean usePooling,
|
||||
@Nullable GenericObjectPoolConfig poolConfig, @Nullable String clientName, Duration readTimeout,
|
||||
Duration connectTimeout) {
|
||||
|
||||
this.customizer = Optional.ofNullable(customizer);
|
||||
this.useSsl = useSsl;
|
||||
this.sslSocketFactory = Optional.ofNullable(sslSocketFactory);
|
||||
this.sslParameters = Optional.ofNullable(sslParameters);
|
||||
@@ -60,6 +63,11 @@ class DefaultJedisClientConfiguration implements JedisClientConfiguration {
|
||||
this.connectTimeout = connectTimeout;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<JedisClientConfigBuilderCustomizer> getCustomizer() {
|
||||
return customizer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isUseSsl() {
|
||||
return useSsl;
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2024 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.data.redis.connection.jedis;
|
||||
|
||||
import redis.clients.jedis.DefaultJedisClientConfig;
|
||||
|
||||
/**
|
||||
* Strategy interface for customizing {@link DefaultJedisClientConfig.Builder JedisClientConfig}. Any ClientConfig will
|
||||
* be used to call this interface implementation so you can set the protocol, client name, etc. after Spring has applies
|
||||
* its defaults.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @since 3.4
|
||||
* @see redis.clients.jedis.DefaultJedisClientConfig.Builder
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface JedisClientConfigBuilderCustomizer {
|
||||
|
||||
/**
|
||||
* Customize the {@link DefaultJedisClientConfig.Builder}.
|
||||
*
|
||||
* @param builder the builder to customize.
|
||||
*/
|
||||
void customize(DefaultJedisClientConfig.Builder builder);
|
||||
|
||||
}
|
||||
@@ -58,6 +58,12 @@ import org.springframework.util.Assert;
|
||||
*/
|
||||
public interface JedisClientConfiguration {
|
||||
|
||||
/**
|
||||
* @return the optional {@link JedisClientConfigBuilderCustomizer}.
|
||||
* @since 3.4
|
||||
*/
|
||||
Optional<JedisClientConfigBuilderCustomizer> getCustomizer();
|
||||
|
||||
/**
|
||||
* @return {@literal true} to use SSL, {@literal false} to use unencrypted connections.
|
||||
*/
|
||||
@@ -119,6 +125,8 @@ public interface JedisClientConfiguration {
|
||||
/**
|
||||
* Creates a default {@link JedisClientConfiguration}.
|
||||
* <dl>
|
||||
* <dt>Customizer</dt>
|
||||
* <dd>none</dd>
|
||||
* <dt>SSL enabled</dt>
|
||||
* <dd>no</dd>
|
||||
* <dt>Pooling enabled</dt>
|
||||
@@ -142,6 +150,15 @@ public interface JedisClientConfiguration {
|
||||
*/
|
||||
interface JedisClientConfigurationBuilder {
|
||||
|
||||
/**
|
||||
* Configure a {@link JedisClientConfigBuilderCustomizer} to configure
|
||||
* {@link redis.clients.jedis.JedisClientConfig}.
|
||||
*
|
||||
* @return {@link JedisClientConfigurationBuilder}.
|
||||
* @since 3.4
|
||||
*/
|
||||
JedisClientConfigurationBuilder customize(JedisClientConfigBuilderCustomizer customizer);
|
||||
|
||||
/**
|
||||
* Enable SSL connections.
|
||||
*
|
||||
@@ -269,6 +286,7 @@ public interface JedisClientConfiguration {
|
||||
class DefaultJedisClientConfigurationBuilder implements JedisClientConfigurationBuilder,
|
||||
JedisPoolingClientConfigurationBuilder, JedisSslClientConfigurationBuilder {
|
||||
|
||||
private @Nullable JedisClientConfigBuilderCustomizer customizer;
|
||||
private boolean useSsl;
|
||||
private @Nullable SSLSocketFactory sslSocketFactory;
|
||||
private @Nullable SSLParameters sslParameters;
|
||||
@@ -281,6 +299,15 @@ public interface JedisClientConfiguration {
|
||||
|
||||
private DefaultJedisClientConfigurationBuilder() {}
|
||||
|
||||
@Override
|
||||
public JedisClientConfigurationBuilder customize(JedisClientConfigBuilderCustomizer customizer) {
|
||||
|
||||
Assert.notNull(customizer, "JedisClientConfigBuilderCustomizer must not be null");
|
||||
|
||||
this.customizer = customizer;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JedisSslClientConfigurationBuilder useSsl() {
|
||||
|
||||
@@ -366,8 +393,8 @@ public interface JedisClientConfiguration {
|
||||
@Override
|
||||
public JedisClientConfiguration build() {
|
||||
|
||||
return new DefaultJedisClientConfiguration(useSsl, sslSocketFactory, sslParameters, hostnameVerifier, usePooling,
|
||||
poolConfig, clientName, readTimeout, connectTimeout);
|
||||
return new DefaultJedisClientConfiguration(customizer, useSsl, sslSocketFactory, sslParameters, hostnameVerifier,
|
||||
usePooling, poolConfig, clientName, readTimeout, connectTimeout);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -711,6 +711,8 @@ public class JedisConnectionFactory
|
||||
this.clientConfiguration.getSslParameters().ifPresent(builder::sslParameters);
|
||||
}
|
||||
|
||||
this.clientConfiguration.getCustomizer().ifPresent(customizer -> customizer.customize(builder));
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@@ -1087,6 +1089,11 @@ public class JedisConnectionFactory
|
||||
return configuration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<JedisClientConfigBuilderCustomizer> getCustomizer() {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isUseSsl() {
|
||||
return useSsl;
|
||||
|
||||
Reference in New Issue
Block a user