Skip to content

Commit 74da2fb

Browse files
committed
fix(Optimizing Code Writing): Optimizing Code Writing
Optimizing Code Writing
1 parent d930db6 commit 74da2fb

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/support/DynamicThreadPoolPostProcessor.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,14 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) {
6161
@Override
6262
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
6363
if (bean instanceof DynamicThreadPoolExecutor || DynamicThreadPoolAdapterChoose.match(bean)) {
64-
DynamicThreadPool dynamicThreadPool;
6564
try {
66-
dynamicThreadPool = ApplicationContextHolder.findAnnotationOnBean(beanName, DynamicThreadPool.class);
65+
DynamicThreadPool dynamicThreadPool =
66+
Optional.ofNullable(ApplicationContextHolder.findAnnotationOnBean(beanName,
67+
DynamicThreadPool.class))
68+
.orElse(DynamicThreadPoolAnnotationUtil.findAnnotationOnBean(beanName,
69+
DynamicThreadPool.class));
6770
if (Objects.isNull(dynamicThreadPool)) {
68-
// Adapt to lower versions of SpringBoot.
69-
dynamicThreadPool = DynamicThreadPoolAnnotationUtil.findAnnotationOnBean(beanName, DynamicThreadPool.class);
70-
if (Objects.isNull(dynamicThreadPool)) {
71-
return bean;
72-
}
71+
return bean;
7372
}
7473
} catch (Exception ex) {
7574
log.error("Failed to create dynamic thread pool in annotation mode.", ex);
@@ -79,7 +78,8 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw
7978
if ((dynamicThreadPoolExecutor = DynamicThreadPoolAdapterChoose.unwrap(bean)) == null) {
8079
dynamicThreadPoolExecutor = (DynamicThreadPoolExecutor) bean;
8180
}
82-
DynamicThreadPoolWrapper wrap = new DynamicThreadPoolWrapper(dynamicThreadPoolExecutor.getThreadPoolId(), dynamicThreadPoolExecutor);
81+
DynamicThreadPoolWrapper wrap = new DynamicThreadPoolWrapper(dynamicThreadPoolExecutor.getThreadPoolId(),
82+
dynamicThreadPoolExecutor);
8383
ThreadPoolExecutor remoteThreadPoolExecutor = fillPoolAndRegister(wrap);
8484
DynamicThreadPoolAdapterChoose.replace(bean, remoteThreadPoolExecutor);
8585
return DynamicThreadPoolAdapterChoose.match(bean) ? bean : remoteThreadPoolExecutor;
@@ -168,7 +168,8 @@ private ExecutorProperties buildDefaultExecutorProperties(String threadPoolId, T
168168
* @param executorProperties executor properties
169169
*/
170170
private void threadPoolParamReplace(ThreadPoolExecutor executor, ExecutorProperties executorProperties) {
171-
BlockingQueue workQueue = BlockingQueueTypeEnum.createBlockingQueue(executorProperties.getBlockingQueue(), executorProperties.getQueueCapacity());
171+
BlockingQueue workQueue = BlockingQueueTypeEnum.createBlockingQueue(executorProperties.getBlockingQueue(),
172+
executorProperties.getQueueCapacity());
172173
ReflectUtil.setFieldValue(executor, "workQueue", workQueue);
173174
executor.setCorePoolSize(executorProperties.getCorePoolSize());
174175
executor.setMaximumPoolSize(executorProperties.getMaximumPoolSize());
@@ -205,7 +206,8 @@ private ExecutorProperties buildExecutorProperties(ExecutorProperties executorPr
205206
.orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(each -> each.getQueueCapacity()).get()))
206207
.rejectedHandler(Optional.ofNullable(executorProperties.getRejectedHandler())
207208
.orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(each -> each.getRejectedHandler()).get()))
208-
.threadNamePrefix(StringUtil.isBlank(executorProperties.getThreadNamePrefix()) ? executorProperties.getThreadPoolId() : executorProperties.getThreadNamePrefix())
209+
.threadNamePrefix(StringUtil.isBlank(executorProperties.getThreadNamePrefix()) ?
210+
executorProperties.getThreadPoolId() : executorProperties.getThreadNamePrefix())
209211
.threadPoolId(executorProperties.getThreadPoolId())
210212
.build();
211213
return newExecutorProperties;
@@ -218,7 +220,8 @@ private ExecutorProperties buildExecutorProperties(ExecutorProperties executorPr
218220
* @return thread-pool notify alarm
219221
*/
220222
private ThreadPoolNotifyAlarm buildThreadPoolNotifyAlarm(ExecutorProperties executorProperties) {
221-
DynamicThreadPoolNotifyProperties notify = Optional.ofNullable(executorProperties).map(ExecutorProperties::getNotify).orElse(null);
223+
DynamicThreadPoolNotifyProperties notify =
224+
Optional.ofNullable(executorProperties).map(ExecutorProperties::getNotify).orElse(null);
222225
boolean isAlarm = Optional.ofNullable(executorProperties.getAlarm())
223226
.orElseGet(() -> Optional.ofNullable(configProperties.getDefaultExecutor()).map(ExecutorProperties::getAlarm).orElse(true));
224227
int activeAlarm = Optional.ofNullable(executorProperties.getActiveAlarm())

hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/support/DynamicThreadPoolPostProcessor.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,14 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) {
7878
@Override
7979
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
8080
if (bean instanceof DynamicThreadPoolExecutor || DynamicThreadPoolAdapterChoose.match(bean)) {
81-
DynamicThreadPool dynamicThreadPool;
8281
try {
83-
dynamicThreadPool = ApplicationContextHolder.findAnnotationOnBean(beanName, DynamicThreadPool.class);
82+
DynamicThreadPool dynamicThreadPool =
83+
Optional.ofNullable(ApplicationContextHolder.findAnnotationOnBean(beanName,
84+
DynamicThreadPool.class))
85+
.orElse(DynamicThreadPoolAnnotationUtil.findAnnotationOnBean(beanName,
86+
DynamicThreadPool.class));
8487
if (Objects.isNull(dynamicThreadPool)) {
85-
// Adapt to lower versions of SpringBoot.
86-
dynamicThreadPool = DynamicThreadPoolAnnotationUtil.findAnnotationOnBean(beanName, DynamicThreadPool.class);
87-
if (Objects.isNull(dynamicThreadPool)) {
88-
return bean;
89-
}
88+
return bean;
9089
}
9190
} catch (Exception ex) {
9291
log.error("Failed to create dynamic thread pool in annotation mode.", ex);
@@ -96,7 +95,9 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw
9695
if ((dynamicThreadPoolExecutor = DynamicThreadPoolAdapterChoose.unwrap(bean)) == null) {
9796
dynamicThreadPoolExecutor = (DynamicThreadPoolExecutor) bean;
9897
}
99-
DynamicThreadPoolWrapper dynamicThreadPoolWrapper = new DynamicThreadPoolWrapper(dynamicThreadPoolExecutor.getThreadPoolId(), dynamicThreadPoolExecutor);
98+
DynamicThreadPoolWrapper dynamicThreadPoolWrapper =
99+
new DynamicThreadPoolWrapper(dynamicThreadPoolExecutor.getThreadPoolId(),
100+
dynamicThreadPoolExecutor);
100101
ThreadPoolExecutor remoteThreadPoolExecutor = fillPoolAndRegister(dynamicThreadPoolWrapper);
101102
DynamicThreadPoolAdapterChoose.replace(bean, remoteThreadPoolExecutor);
102103
subscribeConfig(dynamicThreadPoolWrapper);
@@ -165,7 +166,8 @@ protected ThreadPoolExecutor fillPoolAndRegister(DynamicThreadPoolWrapper dynami
165166
} catch (Exception ex) {
166167
log.error("Failed to initialize thread pool configuration. error message: {}", ex.getMessage());
167168
}
168-
GlobalThreadPoolManage.register(dynamicThreadPoolWrapper.getThreadPoolId(), threadPoolParameterInfo, dynamicThreadPoolWrapper);
169+
GlobalThreadPoolManage.register(dynamicThreadPoolWrapper.getThreadPoolId(), threadPoolParameterInfo,
170+
dynamicThreadPoolWrapper);
169171
return executor;
170172
}
171173

@@ -176,7 +178,8 @@ protected ThreadPoolExecutor fillPoolAndRegister(DynamicThreadPoolWrapper dynami
176178
* @param threadPoolParameterInfo thread-pool parameter info
177179
*/
178180
private void threadPoolParamReplace(ThreadPoolExecutor executor, ThreadPoolParameterInfo threadPoolParameterInfo) {
179-
BlockingQueue workQueue = BlockingQueueTypeEnum.createBlockingQueue(threadPoolParameterInfo.getQueueType(), threadPoolParameterInfo.getCapacity());
181+
BlockingQueue workQueue = BlockingQueueTypeEnum.createBlockingQueue(threadPoolParameterInfo.getQueueType(),
182+
threadPoolParameterInfo.getCapacity());
180183
ReflectUtil.setFieldValue(executor, "workQueue", workQueue);
181184
executor.setCorePoolSize(threadPoolParameterInfo.corePoolSizeAdapt());
182185
executor.setMaximumPoolSize(threadPoolParameterInfo.maximumPoolSizeAdapt());

0 commit comments

Comments
 (0)