dragon-zhang commented on a change in pull request #2983: URL: https://github.com/apache/rocketmq/pull/2983#discussion_r730397740
########## File path: common/src/main/java/org/apache/rocketmq/common/concurrent/ConcurrentEngine.java ########## @@ -0,0 +1,463 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.rocketmq.common.concurrent; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Queue; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.MapUtils; +import org.apache.rocketmq.common.UtilAll; +import org.apache.rocketmq.common.constant.LoggerName; +import org.apache.rocketmq.common.utils.ThreadUtils; +import org.apache.rocketmq.logging.InternalLogger; +import org.apache.rocketmq.logging.InternalLoggerFactory; + +public class ConcurrentEngine { + + protected static final InternalLogger log = InternalLoggerFactory.getLogger(LoggerName.COMMON_LOGGER_NAME); + + protected final ExecutorService enginePool; + + public ConcurrentEngine() { + this.enginePool = ForkJoinPool.commonPool(); + } + + public ConcurrentEngine(ExecutorService enginePool) { + this.enginePool = enginePool; + } + + public final void runAsync(Runnable... tasks) { + runAsync(UtilAll.newArrayList(tasks)); + } + + protected static <E> List<E> pollAllTask(Queue<E> tasks) { + //avoid list expansion + List<E> list = new LinkedList<>(); + while (tasks != null && !tasks.isEmpty()) { + E task = tasks.poll(); + list.add(task); + } + return list; + } + + protected static <T> void doCallback(CallableSupplier<T> supplier, T response) { + Collection<Callback<T>> callbacks = supplier.getCallbacks(); + if (CollectionUtils.isNotEmpty(callbacks)) { + for (Callback<T> callback : callbacks) { + callback.call(response); + } + } + } + + public final void runAsync(Queue<? extends Runnable> tasks) { + runAsync(pollAllTask(tasks)); + } + + public final void runAsync(Collection<? extends Runnable> tasks) { + if (CollectionUtils.isEmpty(tasks) || enginePool.isShutdown()) { + return; + } + List<CompletableFuture<Void>> list = new ArrayList<>(tasks.size()); + for (Runnable task : tasks) { + list.add(CompletableFuture.runAsync(task, enginePool)); + } + executeAsync(list); + } + + @SafeVarargs + public final <T> List<T> supplyAsync(Supplier<T>... tasks) { + return supplyAsync(UtilAll.newArrayList(tasks)); + } + + public final <T> List<T> supplyAsync(Queue<? extends Supplier<T>> tasks) { + return supplyAsync(pollAllTask(tasks)); + } + + public final <T> List<T> supplyAsync(Collection<? extends Supplier<T>> tasks) { + if (CollectionUtils.isEmpty(tasks) || enginePool.isShutdown()) { + return new ArrayList<>(); + } + List<CompletableFuture<T>> list = new ArrayList<>(tasks.size()); + for (Supplier<T> task : tasks) { + list.add(CompletableFuture.supplyAsync(task, enginePool)); + } + return executeAsync(list); + } + + @SafeVarargs + public final <T> List<T> supplyCallableAsync(CallableSupplier<T>... tasks) { + return supplyCallableAsync(UtilAll.newArrayList(tasks)); + } + + public final <T> List<T> supplyCallableAsync(Queue<? extends CallableSupplier<T>> tasks) { + return supplyCallableAsync(pollAllTask(tasks)); + } + + public final <T> List<T> supplyCallableAsync(Collection<? extends CallableSupplier<T>> tasks) { + if (CollectionUtils.isEmpty(tasks) || enginePool.isShutdown()) { + return new ArrayList<>(); + } + Map<CallableSupplier<T>, CompletableFuture<T>> map = new HashMap<>(tasks.size()); + for (CallableSupplier<T> task : tasks) { + map.put(task, CompletableFuture.supplyAsync(task, enginePool)); + } + Map<CallableSupplier<T>, T> result = executeKeyedAsync(map); + for (Map.Entry<CallableSupplier<T>, T> entry : result.entrySet()) { + doCallback(entry.getKey(), entry.getValue()); + } + return UtilAll.newArrayList(result.values()); + } + + @SafeVarargs + public final <K, V> Map<K, V> supplyKeyedCallableAsync(KeyedCallableSupplier<K, V>... tasks) { + return supplyKeyedCallableAsync(UtilAll.newArrayList(tasks)); + } + + public final <K, V> Map<K, V> supplyKeyedCallableAsync(Queue<? extends KeyedCallableSupplier<K, V>> tasks) { + return supplyKeyedCallableAsync(pollAllTask(tasks)); + } + + public final <K, V> Map<K, V> supplyKeyedCallableAsync(Collection<? extends KeyedCallableSupplier<K, V>> tasks) { + if (CollectionUtils.isEmpty(tasks) || enginePool.isShutdown()) { + return new HashMap<>(); + } + Map<K, CompletableFuture<V>> map = new HashMap<>(tasks.size()); + for (KeyedCallableSupplier<K, V> task : tasks) { + map.put(task.key(), CompletableFuture.supplyAsync(task, enginePool)); + } + Map<K, V> result = executeKeyedAsync(map); + for (KeyedCallableSupplier<K, V> task : tasks) { + K key = task.key(); + V response = result.get(key); + doCallback(task, response); + } + return result; + } + + @SafeVarargs + public final <T> List<T> executeAsync(CompletableFuture<T>... tasks) { + return executeAsync(UtilAll.newArrayList(tasks)); + } + + public final <T> List<T> executeAsync(Queue<CompletableFuture<T>> tasks) { + return executeAsync(pollAllTask(tasks)); + } + + public final <T> List<T> executeAsync(Collection<CompletableFuture<T>> tasks) { Review comment: What do you think is a more reasonable name ? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
