3232import org .springframework .data .redis .TooManyClusterRedirectionsException ;
3333import org .springframework .data .redis .connection .util .ByteArraySet ;
3434import org .springframework .data .redis .connection .util .ByteArrayWrapper ;
35- import org .springframework .lang .NonNull ;
3635import org .springframework .lang .Nullable ;
37- import org .springframework .scheduling .concurrent .ThreadPoolTaskExecutor ;
3836import org .springframework .util .Assert ;
3937import org .springframework .util .CollectionUtils ;
4038import org .springframework .util .ObjectUtils ;
4139
4240/**
4341 * {@link ClusterCommandExecutor} takes care of running commands across the known cluster nodes. By providing an
44- * {@link AsyncTaskExecutor} the execution behavior can be influenced .
42+ * {@link AsyncTaskExecutor} the execution behavior can be configured .
4543 *
4644 * @author Christoph Strobl
4745 * @author Mark Paluch
4846 * @since 1.7
4947 */
5048public class ClusterCommandExecutor implements DisposableBean {
5149
52- protected static final AsyncTaskExecutor DEFAULT_TASK_EXECUTOR = new SimpleAsyncTaskExecutor ();
53-
5450 private int maxRedirects = 5 ;
5551
5652 private final AsyncTaskExecutor executor ;
@@ -71,14 +67,14 @@ public class ClusterCommandExecutor implements DisposableBean {
7167 public ClusterCommandExecutor (ClusterTopologyProvider topologyProvider , ClusterNodeResourceProvider resourceProvider ,
7268 ExceptionTranslationStrategy exceptionTranslation ) {
7369
74- this (topologyProvider , resourceProvider , exceptionTranslation , DEFAULT_TASK_EXECUTOR );
70+ this (topologyProvider , resourceProvider , exceptionTranslation , new SimpleAsyncTaskExecutor () );
7571 }
7672
7773 /**
7874 * @param topologyProvider must not be {@literal null}.
7975 * @param resourceProvider must not be {@literal null}.
8076 * @param exceptionTranslation must not be {@literal null}.
81- * @param executor can be {@literal null}. Defaulted to {@link ThreadPoolTaskExecutor }.
77+ * @param executor the task executor to null, defaults to {@link SimpleAsyncTaskExecutor} if {@literal null }.
8278 */
8379 public ClusterCommandExecutor (ClusterTopologyProvider topologyProvider , ClusterNodeResourceProvider resourceProvider ,
8480 ExceptionTranslationStrategy exceptionTranslation , @ Nullable AsyncTaskExecutor executor ) {
@@ -90,11 +86,7 @@ public ClusterCommandExecutor(ClusterTopologyProvider topologyProvider, ClusterN
9086 this .topologyProvider = topologyProvider ;
9187 this .resourceProvider = resourceProvider ;
9288 this .exceptionTranslationStrategy = exceptionTranslation ;
93- this .executor = resolveTaskExecutor (executor );
94- }
95-
96- private @ NonNull AsyncTaskExecutor resolveTaskExecutor (@ Nullable AsyncTaskExecutor taskExecutor ) {
97- return taskExecutor != null ? taskExecutor : DEFAULT_TASK_EXECUTOR ;
89+ this .executor = executor != null ? executor : new SimpleAsyncTaskExecutor ();
9890 }
9991
10092 /**
@@ -149,9 +141,8 @@ private <S, T> NodeResult<T> executeCommandOnSingleNode(ClusterCommandCallback<S
149141 RuntimeException translatedException = convertToDataAccessException (cause );
150142
151143 if (translatedException instanceof ClusterRedirectException clusterRedirectException ) {
152- return executeCommandOnSingleNode (cmd , topologyProvider .getTopology ()
153- .lookup (clusterRedirectException .getTargetHost (), clusterRedirectException .getTargetPort ()),
154- redirectCount + 1 );
144+ return executeCommandOnSingleNode (cmd , topologyProvider .getTopology ().lookup (
145+ clusterRedirectException .getTargetHost (), clusterRedirectException .getTargetPort ()), redirectCount + 1 );
155146 } else {
156147 throw translatedException != null ? translatedException : cause ;
157148 }
@@ -182,7 +173,7 @@ private RedisClusterNode lookupNode(RedisClusterNode node) {
182173 * @param cmd must not be {@literal null}.
183174 * @return never {@literal null}.
184175 * @throws ClusterCommandExecutionFailureException if a failure occurs while executing the given
185- * {@link ClusterCommandCallback command} on any given {@link RedisClusterNode node}.
176+ * {@link ClusterCommandCallback command} on any given {@link RedisClusterNode node}.
186177 */
187178 public <S , T > MultiNodeResult <T > executeCommandOnAllNodes (final ClusterCommandCallback <S , T > cmd ) {
188179 return executeCommandAsyncOnNodes (cmd , getClusterTopology ().getActiveMasterNodes ());
@@ -193,7 +184,7 @@ public <S, T> MultiNodeResult<T> executeCommandOnAllNodes(final ClusterCommandCa
193184 * @param nodes must not be {@literal null}.
194185 * @return never {@literal null}.
195186 * @throws ClusterCommandExecutionFailureException if a failure occurs while executing the given
196- * {@link ClusterCommandCallback command} on any given {@link RedisClusterNode node}.
187+ * {@link ClusterCommandCallback command} on any given {@link RedisClusterNode node}.
197188 * @throws IllegalArgumentException in case the node could not be resolved to a topology-known node
198189 */
199190 public <S , T > MultiNodeResult <T > executeCommandAsyncOnNodes (ClusterCommandCallback <S , T > callback ,
@@ -295,7 +286,7 @@ private <T> MultiNodeResult<T> collectResults(Map<NodeExecution, Future<NodeResu
295286 * @param commandCallback must not be {@literal null}.
296287 * @return never {@literal null}.
297288 * @throws ClusterCommandExecutionFailureException if a failure occurs while executing the given
298- * {@link MultiKeyClusterCommandCallback command}.
289+ * {@link MultiKeyClusterCommandCallback command}.
299290 */
300291 public <S , T > MultiNodeResult <T > executeMultiKeyCommand (MultiKeyClusterCommandCallback <S , T > commandCallback ,
301292 Iterable <byte []> keys ) {
@@ -315,8 +306,8 @@ public <S, T> MultiNodeResult<T> executeMultiKeyCommand(MultiKeyClusterCommandCa
315306
316307 if (entry .getKey ().isMaster ()) {
317308 for (PositionalKey key : entry .getValue ()) {
318- futures .put (new NodeExecution (entry .getKey (), key ), this .executor . submit (() ->
319- executeMultiKeyCommandOnSingleNode (commandCallback , entry .getKey (), key .getBytes ())));
309+ futures .put (new NodeExecution (entry .getKey (), key ), this .executor
310+ . submit (() -> executeMultiKeyCommandOnSingleNode (commandCallback , entry .getKey (), key .getBytes ())));
320311 }
321312 }
322313 }
@@ -367,10 +358,6 @@ public void setMaxRedirects(int maxRedirects) {
367358 @ Override
368359 public void destroy () throws Exception {
369360
370- if (this .executor instanceof DisposableBean disposableBean ) {
371- disposableBean .destroy ();
372- }
373-
374361 if (this .resourceProvider instanceof DisposableBean disposableBean ) {
375362 disposableBean .destroy ();
376363 }
0 commit comments