|
| 1 | +/* |
| 2 | + * Copyright 2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.integration.dispatcher; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Set; |
| 24 | +import java.util.concurrent.Executor; |
| 25 | +import java.util.concurrent.ExecutorService; |
| 26 | +import java.util.concurrent.Executors; |
| 27 | +import java.util.concurrent.ThreadFactory; |
| 28 | +import java.util.function.Function; |
| 29 | + |
| 30 | +import org.springframework.integration.util.ErrorHandlingTaskExecutor; |
| 31 | +import org.springframework.lang.Nullable; |
| 32 | +import org.springframework.messaging.Message; |
| 33 | +import org.springframework.messaging.MessageHandler; |
| 34 | +import org.springframework.scheduling.concurrent.CustomizableThreadFactory; |
| 35 | +import org.springframework.util.Assert; |
| 36 | +import org.springframework.util.ErrorHandler; |
| 37 | + |
| 38 | +/** |
| 39 | + * An {@link AbstractDispatcher} implementation for distributing messages to |
| 40 | + * dedicated threads according to the key determined by the provided function against |
| 41 | + * the message to dispatch. |
| 42 | + * <p> |
| 43 | + * Every partition, created by this class, is a {@link UnicastingDispatcher} |
| 44 | + * delegate based on a single thread {@link Executor}. |
| 45 | + * <p> |
| 46 | + * The number of partitions should be a reasonable value for the application environment |
| 47 | + * since every partition is based on a dedicated thread for message processing. |
| 48 | + * <p> |
| 49 | + * The rest of the logic is similar to {@link UnicastingDispatcher} behavior. |
| 50 | + * |
| 51 | + * @author Artem Bilan |
| 52 | + * |
| 53 | + * @since 6.1 |
| 54 | + */ |
| 55 | +public class PartitionedDispatcher extends AbstractDispatcher { |
| 56 | + |
| 57 | + private final Map<Integer, UnicastingDispatcher> partitions = new HashMap<>(); |
| 58 | + |
| 59 | + private final List<ExecutorService> executors = new ArrayList<>(); |
| 60 | + |
| 61 | + private final int partitionCount; |
| 62 | + |
| 63 | + private final Function<Message<?>, Object> partitionKeyFunction; |
| 64 | + |
| 65 | + private ThreadFactory threadFactory = new CustomizableThreadFactory("partition-thread-"); |
| 66 | + |
| 67 | + private boolean failover = true; |
| 68 | + |
| 69 | + @Nullable |
| 70 | + private LoadBalancingStrategy loadBalancingStrategy; |
| 71 | + |
| 72 | + private ErrorHandler errorHandler; |
| 73 | + |
| 74 | + private MessageHandlingTaskDecorator messageHandlingTaskDecorator = task -> task; |
| 75 | + |
| 76 | + /** |
| 77 | + * Instantiate based on a provided number of partitions and function for partition key against |
| 78 | + * the message to dispatch. |
| 79 | + * @param partitionCount the number of partitions in this channel. |
| 80 | + * @param partitionKeyFunction the function to resolve a partition key against the message |
| 81 | + * to dispatch. |
| 82 | + */ |
| 83 | + public PartitionedDispatcher(int partitionCount, Function<Message<?>, Object> partitionKeyFunction) { |
| 84 | + Assert.isTrue(partitionCount > 0, "'partitionCount' must be greater than 0"); |
| 85 | + Assert.notNull(partitionKeyFunction, "'partitionKeyFunction' must not be null"); |
| 86 | + this.partitionKeyFunction = partitionKeyFunction; |
| 87 | + this.partitionCount = partitionCount; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Set a {@link ThreadFactory} for executors per partitions. |
| 92 | + * Defaults to the {@link CustomizableThreadFactory} based on a {@code partition-thread-} prefix. |
| 93 | + * @param threadFactory the {@link ThreadFactory} to use. |
| 94 | + */ |
| 95 | + public void setThreadFactory(ThreadFactory threadFactory) { |
| 96 | + Assert.notNull(threadFactory, "'threadFactory' must not be null"); |
| 97 | + this.threadFactory = threadFactory; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Specify whether partition dispatchers should have failover enabled. |
| 102 | + * By default, it will. Set this value to 'false' to disable it. |
| 103 | + * @param failover The failover boolean. |
| 104 | + */ |
| 105 | + public void setFailover(boolean failover) { |
| 106 | + this.failover = failover; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Provide a {@link LoadBalancingStrategy} for partition dispatchers. |
| 111 | + * @param loadBalancingStrategy The load balancing strategy implementation. |
| 112 | + */ |
| 113 | + public void setLoadBalancingStrategy(@Nullable LoadBalancingStrategy loadBalancingStrategy) { |
| 114 | + this.loadBalancingStrategy = loadBalancingStrategy; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Provide a {@link ErrorHandler} for wrapping partition {@link Executor} |
| 119 | + * to the {@link ErrorHandlingTaskExecutor}. |
| 120 | + * @param errorHandler the {@link ErrorHandler} to use. |
| 121 | + */ |
| 122 | + public void setErrorHandler(ErrorHandler errorHandler) { |
| 123 | + this.errorHandler = errorHandler; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Set a {@link MessageHandlingTaskDecorator} to wrap a message handling task into some |
| 128 | + * addition logic, e.g. message channel may provide an interception for its operations. |
| 129 | + * @param messageHandlingTaskDecorator the {@link MessageHandlingTaskDecorator} to use. |
| 130 | + */ |
| 131 | + public void setMessageHandlingTaskDecorator(MessageHandlingTaskDecorator messageHandlingTaskDecorator) { |
| 132 | + Assert.notNull(messageHandlingTaskDecorator, "'messageHandlingTaskDecorator' must not be null."); |
| 133 | + this.messageHandlingTaskDecorator = messageHandlingTaskDecorator; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Shutdown this dispatcher on application close. |
| 138 | + * The partition executors are shutdown and internal state of this instance is cleared. |
| 139 | + */ |
| 140 | + public void shutdown() { |
| 141 | + this.executors.forEach(ExecutorService::shutdown); |
| 142 | + this.executors.clear(); |
| 143 | + this.partitions.clear(); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public boolean dispatch(Message<?> message) { |
| 148 | + populatedPartitions(); |
| 149 | + int partition = Math.abs(this.partitionKeyFunction.apply(message).hashCode()) % this.partitionCount; |
| 150 | + UnicastingDispatcher partitionDispatcher = this.partitions.get(partition); |
| 151 | + return partitionDispatcher.dispatch(message); |
| 152 | + } |
| 153 | + |
| 154 | + private synchronized void populatedPartitions() { |
| 155 | + if (this.partitions.isEmpty()) { |
| 156 | + for (int i = 0; i < this.partitionCount; i++) { |
| 157 | + this.partitions.put(i, newPartition()); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + private UnicastingDispatcher newPartition() { |
| 163 | + ExecutorService executor = Executors.newSingleThreadExecutor(this.threadFactory); |
| 164 | + this.executors.add(executor); |
| 165 | + DelegateDispatcher delegateDispatcher = |
| 166 | + new DelegateDispatcher(new ErrorHandlingTaskExecutor(executor, this.errorHandler)); |
| 167 | + delegateDispatcher.setFailover(this.failover); |
| 168 | + delegateDispatcher.setLoadBalancingStrategy(this.loadBalancingStrategy); |
| 169 | + delegateDispatcher.setMessageHandlingTaskDecorator(this.messageHandlingTaskDecorator); |
| 170 | + return delegateDispatcher; |
| 171 | + } |
| 172 | + |
| 173 | + private final class DelegateDispatcher extends UnicastingDispatcher { |
| 174 | + |
| 175 | + DelegateDispatcher(Executor executor) { |
| 176 | + super(executor); |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + protected Set<MessageHandler> getHandlers() { |
| 181 | + return PartitionedDispatcher.this.getHandlers(); |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + protected boolean tryOptimizedDispatch(Message<?> message) { |
| 186 | + return PartitionedDispatcher.this.tryOptimizedDispatch(message); |
| 187 | + } |
| 188 | + |
| 189 | + } |
| 190 | + |
| 191 | +} |
0 commit comments