|
|
|
@ -25,17 +25,15 @@ import com.google.common.collect.Lists;
|
|
|
|
|
import com.google.common.collect.Maps;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.amqp.rabbit.config.AbstractRabbitListenerContainerFactory;
|
|
|
|
|
import org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer;
|
|
|
|
|
import org.springframework.amqp.rabbit.connection.AbstractConnectionFactory;
|
|
|
|
|
import org.springframework.boot.context.event.ApplicationStartedEvent;
|
|
|
|
|
import org.springframework.context.ApplicationListener;
|
|
|
|
|
import org.springframework.core.task.SimpleAsyncTaskExecutor;
|
|
|
|
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.concurrent.Executor;
|
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
|
|
|
|
|
|
import static cn.hippo4j.common.constant.ChangeThreadPoolConstants.CHANGE_DELIMITER;
|
|
|
|
|
|
|
|
|
@ -48,15 +46,17 @@ public class RabbitMQThreadPoolAdapter implements ThreadPoolAdapter, Application
|
|
|
|
|
|
|
|
|
|
private static final String RABBITMQ = "RabbitMQ";
|
|
|
|
|
|
|
|
|
|
private static final String FiledName = "taskExecutor";
|
|
|
|
|
private static final String FiledName = "executorService";
|
|
|
|
|
|
|
|
|
|
private static final String BEAN_NAME_FILED = "beanName";
|
|
|
|
|
|
|
|
|
|
private final List<AbstractRabbitListenerContainerFactory<?>> abstractRabbitListenerContainerFactories;
|
|
|
|
|
// todo: Configurable name
|
|
|
|
|
private static final String RABBITMQ_EXECUTOR_SERVICE = "Rabbitmq_Executor_Service";
|
|
|
|
|
|
|
|
|
|
private final Map<String, SimpleAsyncTaskExecutor> RABBITMQ_EXECUTOR = Maps.newHashMap();
|
|
|
|
|
|
|
|
|
|
private final Map<String, ThreadPoolTaskExecutor> RABBITMQ_THREAD_POOL_TASK_EXECUTOR = Maps.newHashMap();
|
|
|
|
|
private final AbstractConnectionFactory abstractConnectionFactory;
|
|
|
|
|
|
|
|
|
|
private final Map<String, ThreadPoolExecutor> RABBITMQ_THREAD_POOL_TASK_EXECUTOR = Maps.newHashMap();
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String mark() {
|
|
|
|
@ -66,23 +66,17 @@ public class RabbitMQThreadPoolAdapter implements ThreadPoolAdapter, Application
|
|
|
|
|
@Override
|
|
|
|
|
public ThreadPoolAdapterState getThreadPoolState(String identify) {
|
|
|
|
|
ThreadPoolAdapterState threadPoolAdapterState = new ThreadPoolAdapterState();
|
|
|
|
|
SimpleAsyncTaskExecutor simpleAsyncTaskExecutor = RABBITMQ_EXECUTOR.get(identify);
|
|
|
|
|
ThreadPoolTaskExecutor threadPoolTaskExecutor = RABBITMQ_THREAD_POOL_TASK_EXECUTOR.get(identify);
|
|
|
|
|
ThreadPoolExecutor threadPoolTaskExecutor = RABBITMQ_THREAD_POOL_TASK_EXECUTOR.get(identify);
|
|
|
|
|
threadPoolAdapterState.setThreadPoolKey(identify);
|
|
|
|
|
if (Objects.nonNull(simpleAsyncTaskExecutor)) {
|
|
|
|
|
threadPoolAdapterState.setCoreSize(simpleAsyncTaskExecutor.getConcurrencyLimit());
|
|
|
|
|
}
|
|
|
|
|
if (Objects.nonNull(threadPoolTaskExecutor)) {
|
|
|
|
|
threadPoolAdapterState.setCoreSize(threadPoolTaskExecutor.getCorePoolSize());
|
|
|
|
|
threadPoolAdapterState.setMaximumSize(threadPoolTaskExecutor.getMaxPoolSize());
|
|
|
|
|
threadPoolAdapterState.setMaximumSize(threadPoolTaskExecutor.getMaximumPoolSize());
|
|
|
|
|
}
|
|
|
|
|
return threadPoolAdapterState;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ThreadPoolAdapterState> getThreadPoolStates() {
|
|
|
|
|
List<ThreadPoolAdapterState> adapterStateList = Lists.newArrayList();
|
|
|
|
|
RABBITMQ_EXECUTOR.forEach(
|
|
|
|
|
(key, val) -> adapterStateList.add(getThreadPoolState(key)));
|
|
|
|
|
RABBITMQ_THREAD_POOL_TASK_EXECUTOR.forEach(
|
|
|
|
|
(key, val) -> adapterStateList.add(getThreadPoolState(key)));
|
|
|
|
|
return adapterStateList;
|
|
|
|
@ -91,53 +85,35 @@ public class RabbitMQThreadPoolAdapter implements ThreadPoolAdapter, Application
|
|
|
|
|
@Override
|
|
|
|
|
public boolean updateThreadPool(ThreadPoolAdapterParameter threadPoolAdapterParameter) {
|
|
|
|
|
String threadPoolKey = threadPoolAdapterParameter.getThreadPoolKey();
|
|
|
|
|
SimpleAsyncTaskExecutor simpleAsyncTaskExecutor = RABBITMQ_EXECUTOR.get(threadPoolKey);
|
|
|
|
|
ThreadPoolTaskExecutor threadPoolTaskExecutor = RABBITMQ_THREAD_POOL_TASK_EXECUTOR.get(threadPoolKey);
|
|
|
|
|
ThreadPoolExecutor threadPoolTaskExecutor = RABBITMQ_THREAD_POOL_TASK_EXECUTOR.get(threadPoolKey);
|
|
|
|
|
if (Objects.nonNull(threadPoolTaskExecutor)) {
|
|
|
|
|
int originalCoreSize = threadPoolTaskExecutor.getCorePoolSize();
|
|
|
|
|
int originalMaximumPoolSize = threadPoolTaskExecutor.getMaxPoolSize();
|
|
|
|
|
int originalMaximumPoolSize = threadPoolTaskExecutor.getMaximumPoolSize();
|
|
|
|
|
threadPoolTaskExecutor.setMaximumPoolSize(threadPoolAdapterParameter.getMaximumPoolSize());
|
|
|
|
|
threadPoolTaskExecutor.setCorePoolSize(threadPoolAdapterParameter.getCorePoolSize());
|
|
|
|
|
threadPoolTaskExecutor.setMaxPoolSize(threadPoolAdapterParameter.getMaximumPoolSize());
|
|
|
|
|
log.info("[{}] rabbitmq consumption thread pool parameter change. coreSize :: {}, maximumSize :: {}",
|
|
|
|
|
threadPoolKey,
|
|
|
|
|
String.format(CHANGE_DELIMITER, originalCoreSize, threadPoolAdapterParameter.getCorePoolSize()),
|
|
|
|
|
String.format(CHANGE_DELIMITER, originalMaximumPoolSize, threadPoolAdapterParameter.getMaximumPoolSize()));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (Objects.nonNull(simpleAsyncTaskExecutor)) {
|
|
|
|
|
int concurrencyLimit = simpleAsyncTaskExecutor.getConcurrencyLimit();
|
|
|
|
|
simpleAsyncTaskExecutor.setConcurrencyLimit(threadPoolAdapterParameter.getCorePoolSize());
|
|
|
|
|
log.info("[{}] rabbitmq consumption thread pool parameter change. coreSize :: {}",
|
|
|
|
|
threadPoolKey,
|
|
|
|
|
String.format(CHANGE_DELIMITER, concurrencyLimit, threadPoolAdapterParameter.getCorePoolSize()));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
log.warn("[{}] rabbitmq consuming thread pool not found.", threadPoolKey);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onApplicationEvent(ApplicationStartedEvent event) {
|
|
|
|
|
for (AbstractRabbitListenerContainerFactory<?> consumerWorkService : abstractRabbitListenerContainerFactories) {
|
|
|
|
|
// 是否为自定义线程池
|
|
|
|
|
Executor executor = (Executor) ReflectUtil.getFieldValue(consumerWorkService, FiledName);
|
|
|
|
|
if (Objects.isNull(executor)) {
|
|
|
|
|
// 获取默认线程池
|
|
|
|
|
// 优先获取用户配置的
|
|
|
|
|
AbstractMessageListenerContainer listenerContainer1 = consumerWorkService.createListenerContainer();
|
|
|
|
|
SimpleAsyncTaskExecutor fieldValue = (SimpleAsyncTaskExecutor) ReflectUtil.getFieldValue(listenerContainer1, FiledName);
|
|
|
|
|
log.info("rabbitmq executor name {}", FiledName);
|
|
|
|
|
RABBITMQ_EXECUTOR.put(FiledName, fieldValue);
|
|
|
|
|
ExecutorService executor = (ExecutorService) ReflectUtil.getFieldValue(abstractConnectionFactory, FiledName);
|
|
|
|
|
if (Objects.nonNull(executor)) {
|
|
|
|
|
if (executor instanceof ThreadPoolExecutor) {
|
|
|
|
|
ThreadPoolExecutor threadPoolTaskExecutor = (ThreadPoolExecutor) executor;
|
|
|
|
|
String beanName = (String) ReflectUtil.getFieldValue(threadPoolTaskExecutor, BEAN_NAME_FILED);
|
|
|
|
|
RABBITMQ_THREAD_POOL_TASK_EXECUTOR.put(RABBITMQ_EXECUTOR_SERVICE, threadPoolTaskExecutor);
|
|
|
|
|
log.info("rabbitmq executor name {}", RABBITMQ_EXECUTOR_SERVICE);
|
|
|
|
|
} else {
|
|
|
|
|
if (executor instanceof ThreadPoolTaskExecutor) {
|
|
|
|
|
ThreadPoolTaskExecutor threadPoolTaskExecutor = (ThreadPoolTaskExecutor) executor;
|
|
|
|
|
String beanName = (String) ReflectUtil.getFieldValue(threadPoolTaskExecutor, BEAN_NAME_FILED);
|
|
|
|
|
RABBITMQ_THREAD_POOL_TASK_EXECUTOR.put(beanName, threadPoolTaskExecutor);
|
|
|
|
|
log.info("rabbitmq executor name {}", beanName);
|
|
|
|
|
} else {
|
|
|
|
|
log.warn("Custom thread pools only support ThreadPoolTaskExecutor");
|
|
|
|
|
}
|
|
|
|
|
log.warn("Custom thread pools only support ThreadPoolExecutor");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|