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