Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Optimizing Code Writing): Optimizing Code Writing #1460

Open
wants to merge 2 commits into
base: dev_1.5.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class DynamicThreadPoolAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@Order(Ordered.HIGHEST_PRECEDENCE)
public ApplicationContextHolder hippo4JApplicationContextHolder() {
public ApplicationContextHolder hippo4jApplicationContextHolder() {
return new ApplicationContextHolder();
}

Expand All @@ -93,7 +93,7 @@ public ThreadPoolConfigChange defaultThreadPoolConfigChangeHandler(Hippo4jSendMe
}

@Bean
public DynamicThreadPoolPostProcessor dynamicThreadPoolPostProcessor(ApplicationContextHolder hippo4JApplicationContextHolder) {
public DynamicThreadPoolPostProcessor dynamicThreadPoolPostProcessor() {
return new DynamicThreadPoolPostProcessor(bootstrapConfigProperties);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,14 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof DynamicThreadPoolExecutor || DynamicThreadPoolAdapterChoose.match(bean)) {
DynamicThreadPool dynamicThreadPool;
try {
dynamicThreadPool = ApplicationContextHolder.findAnnotationOnBean(beanName, DynamicThreadPool.class);
DynamicThreadPool dynamicThreadPool =
Optional.ofNullable(ApplicationContextHolder.findAnnotationOnBean(beanName,
DynamicThreadPool.class))
.orElse(DynamicThreadPoolAnnotationUtil.findAnnotationOnBean(beanName,
DynamicThreadPool.class));
if (Objects.isNull(dynamicThreadPool)) {
// Adapt to lower versions of SpringBoot.
dynamicThreadPool = DynamicThreadPoolAnnotationUtil.findAnnotationOnBean(beanName, DynamicThreadPool.class);
if (Objects.isNull(dynamicThreadPool)) {
return bean;
}
return bean;
}
} catch (Exception ex) {
log.error("Failed to create dynamic thread pool in annotation mode.", ex);
Expand All @@ -79,7 +78,8 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw
if ((dynamicThreadPoolExecutor = DynamicThreadPoolAdapterChoose.unwrap(bean)) == null) {
dynamicThreadPoolExecutor = (DynamicThreadPoolExecutor) bean;
}
DynamicThreadPoolWrapper wrap = new DynamicThreadPoolWrapper(dynamicThreadPoolExecutor.getThreadPoolId(), dynamicThreadPoolExecutor);
DynamicThreadPoolWrapper wrap = new DynamicThreadPoolWrapper(dynamicThreadPoolExecutor.getThreadPoolId(),
dynamicThreadPoolExecutor);
ThreadPoolExecutor remoteThreadPoolExecutor = fillPoolAndRegister(wrap);
DynamicThreadPoolAdapterChoose.replace(bean, remoteThreadPoolExecutor);
return DynamicThreadPoolAdapterChoose.match(bean) ? bean : remoteThreadPoolExecutor;
Expand Down Expand Up @@ -168,7 +168,8 @@ private ExecutorProperties buildDefaultExecutorProperties(String threadPoolId, T
* @param executorProperties executor properties
*/
private void threadPoolParamReplace(ThreadPoolExecutor executor, ExecutorProperties executorProperties) {
BlockingQueue workQueue = BlockingQueueTypeEnum.createBlockingQueue(executorProperties.getBlockingQueue(), executorProperties.getQueueCapacity());
BlockingQueue workQueue = BlockingQueueTypeEnum.createBlockingQueue(executorProperties.getBlockingQueue(),
executorProperties.getQueueCapacity());
ReflectUtil.setFieldValue(executor, "workQueue", workQueue);
executor.setCorePoolSize(executorProperties.getCorePoolSize());
executor.setMaximumPoolSize(executorProperties.getMaximumPoolSize());
Expand Down Expand Up @@ -205,7 +206,8 @@ private ExecutorProperties buildExecutorProperties(ExecutorProperties executorPr
.orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(each -> each.getQueueCapacity()).get()))
.rejectedHandler(Optional.ofNullable(executorProperties.getRejectedHandler())
.orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(each -> each.getRejectedHandler()).get()))
.threadNamePrefix(StringUtil.isBlank(executorProperties.getThreadNamePrefix()) ? executorProperties.getThreadPoolId() : executorProperties.getThreadNamePrefix())
.threadNamePrefix(StringUtil.isBlank(executorProperties.getThreadNamePrefix()) ?
executorProperties.getThreadPoolId() : executorProperties.getThreadNamePrefix())
.threadPoolId(executorProperties.getThreadPoolId())
.build();
return newExecutorProperties;
Expand All @@ -218,7 +220,8 @@ private ExecutorProperties buildExecutorProperties(ExecutorProperties executorPr
* @return thread-pool notify alarm
*/
private ThreadPoolNotifyAlarm buildThreadPoolNotifyAlarm(ExecutorProperties executorProperties) {
DynamicThreadPoolNotifyProperties notify = Optional.ofNullable(executorProperties).map(ExecutorProperties::getNotify).orElse(null);
DynamicThreadPoolNotifyProperties notify =
Optional.ofNullable(executorProperties).map(ExecutorProperties::getNotify).orElse(null);
boolean isAlarm = Optional.ofNullable(executorProperties.getAlarm())
.orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(ExecutorProperties::getAlarm).orElse(true));
int activeAlarm = Optional.ofNullable(executorProperties.getActiveAlarm())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public DynamicThreadPoolBannerHandler threadPoolBannerHandler(ObjectProvider<Bui
@Bean
@ConditionalOnMissingBean
@Order(Ordered.HIGHEST_PRECEDENCE)
public ApplicationContextHolder hippo4JApplicationContextHolder() {
public ApplicationContextHolder hippo4jApplicationContextHolder() {
return new ApplicationContextHolder();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,14 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof DynamicThreadPoolExecutor || DynamicThreadPoolAdapterChoose.match(bean)) {
DynamicThreadPool dynamicThreadPool;
try {
dynamicThreadPool = ApplicationContextHolder.findAnnotationOnBean(beanName, DynamicThreadPool.class);
DynamicThreadPool dynamicThreadPool =
Optional.ofNullable(ApplicationContextHolder.findAnnotationOnBean(beanName,
DynamicThreadPool.class))
.orElse(DynamicThreadPoolAnnotationUtil.findAnnotationOnBean(beanName,
DynamicThreadPool.class));
if (Objects.isNull(dynamicThreadPool)) {
// Adapt to lower versions of SpringBoot.
dynamicThreadPool = DynamicThreadPoolAnnotationUtil.findAnnotationOnBean(beanName, DynamicThreadPool.class);
if (Objects.isNull(dynamicThreadPool)) {
return bean;
}
return bean;
}
} catch (Exception ex) {
log.error("Failed to create dynamic thread pool in annotation mode.", ex);
Expand All @@ -96,7 +95,9 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw
if ((dynamicThreadPoolExecutor = DynamicThreadPoolAdapterChoose.unwrap(bean)) == null) {
dynamicThreadPoolExecutor = (DynamicThreadPoolExecutor) bean;
}
DynamicThreadPoolWrapper dynamicThreadPoolWrapper = new DynamicThreadPoolWrapper(dynamicThreadPoolExecutor.getThreadPoolId(), dynamicThreadPoolExecutor);
DynamicThreadPoolWrapper dynamicThreadPoolWrapper =
new DynamicThreadPoolWrapper(dynamicThreadPoolExecutor.getThreadPoolId(),
dynamicThreadPoolExecutor);
ThreadPoolExecutor remoteThreadPoolExecutor = fillPoolAndRegister(dynamicThreadPoolWrapper);
DynamicThreadPoolAdapterChoose.replace(bean, remoteThreadPoolExecutor);
subscribeConfig(dynamicThreadPoolWrapper);
Expand Down Expand Up @@ -165,7 +166,8 @@ protected ThreadPoolExecutor fillPoolAndRegister(DynamicThreadPoolWrapper dynami
} catch (Exception ex) {
log.error("Failed to initialize thread pool configuration. error message: {}", ex.getMessage());
}
GlobalThreadPoolManage.register(dynamicThreadPoolWrapper.getThreadPoolId(), threadPoolParameterInfo, dynamicThreadPoolWrapper);
GlobalThreadPoolManage.register(dynamicThreadPoolWrapper.getThreadPoolId(), threadPoolParameterInfo,
dynamicThreadPoolWrapper);
return executor;
}

Expand All @@ -176,7 +178,8 @@ protected ThreadPoolExecutor fillPoolAndRegister(DynamicThreadPoolWrapper dynami
* @param threadPoolParameterInfo thread-pool parameter info
*/
private void threadPoolParamReplace(ThreadPoolExecutor executor, ThreadPoolParameterInfo threadPoolParameterInfo) {
BlockingQueue workQueue = BlockingQueueTypeEnum.createBlockingQueue(threadPoolParameterInfo.getQueueType(), threadPoolParameterInfo.getCapacity());
BlockingQueue workQueue = BlockingQueueTypeEnum.createBlockingQueue(threadPoolParameterInfo.getQueueType(),
threadPoolParameterInfo.getCapacity());
ReflectUtil.setFieldValue(executor, "workQueue", workQueue);
executor.setCorePoolSize(threadPoolParameterInfo.corePoolSizeAdapt());
executor.setMaximumPoolSize(threadPoolParameterInfo.maximumPoolSizeAdapt());
Expand Down