Optimize the logic of the adaptation module

pull/289/head
lukuan 3 years ago
parent a57945dd0d
commit 2437723d3f

@ -1,5 +1,21 @@
package cn.hippo4j.adapter.base;
/*
* 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 cn.hippo4j.adapter.base;
import java.util.Map;

@ -104,15 +104,16 @@ public class HystrixThreadPoolAdapter implements ThreadPoolAdapter, ApplicationL
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
//Periodically update the Hystrix thread pool
// Periodically update the Hystrix thread pool
HystrixThreadPoolRefresh();
//Periodically refresh registration
// Periodically refresh registration
ThreadPoolAdapterRegisterAction threadPoolAdapterRegisterAction = ApplicationContextHolder.getBean(ThreadPoolAdapterRegisterAction.class);
Map<String, ThreadPoolAdapter> map = (Map<String, ThreadPoolAdapter>)ApplicationContextHolder.getBeansOfType(this.getClass());
Map<String, ? extends HystrixThreadPoolAdapter> beansOfType = ApplicationContextHolder.getBeansOfType(this.getClass());
Map<String, ThreadPoolAdapter> map = Maps.newHashMap(beansOfType);
threadPoolAdapterRegisterAction.adapterRegister(map);
}
public void HystrixThreadPoolRefresh(){
public void HystrixThreadPoolRefresh() {
ScheduledExecutorService scheduler = threadPoolAdapterScheduler.getScheduler();
int taskIntervalSeconds = threadPoolAdapterScheduler.getTaskIntervalSeconds();
HystrixThreadPoolRefreshTask hystrixThreadPoolRefreshTask = new HystrixThreadPoolRefreshTask(scheduler, taskIntervalSeconds);

@ -48,7 +48,6 @@ import static cn.hippo4j.common.constant.Constants.REGISTER_ADAPTER_PATH;
*/
@Slf4j
@AllArgsConstructor
@RequiredArgsConstructor
public class ThreadPoolAdapterRegister implements ApplicationRunner, ThreadPoolAdapterRegisterAction {
private final HttpAgent httpAgent;
@ -61,8 +60,6 @@ public class ThreadPoolAdapterRegister implements ApplicationRunner, ThreadPoolA
private final ThreadPoolAdapterScheduler threadPoolAdapterScheduler;
@Override
public void run(ApplicationArguments args) throws Exception {
register();
@ -70,10 +67,11 @@ public class ThreadPoolAdapterRegister implements ApplicationRunner, ThreadPoolA
public List<ThreadPoolAdapterCacheConfig> getThreadPoolAdapterCacheConfigs(Map<String, ThreadPoolAdapter> threadPoolAdapterMap) {
List<ThreadPoolAdapterCacheConfig> cacheConfigList = Lists.newArrayList();
threadPoolAdapterMap.forEach((key, val) -> {
for (Map.Entry<String, ThreadPoolAdapter> threadPoolAdapterEntry : threadPoolAdapterMap.entrySet()) {
ThreadPoolAdapter val = threadPoolAdapterEntry.getValue();
List<ThreadPoolAdapterState> threadPoolStates = val.getThreadPoolStates();
if (CollectionUtil.isEmpty(threadPoolStates)) {
return;
if (CollectionUtil.isEmpty(threadPoolStates) || threadPoolStates.size() == 0) {
continue;
}
ThreadPoolAdapterCacheConfig cacheConfig = new ThreadPoolAdapterCacheConfig();
cacheConfig.setMark(val.mark());
@ -84,12 +82,12 @@ public class ThreadPoolAdapterRegister implements ApplicationRunner, ThreadPoolA
cacheConfig.setClientAddress(clientAddress);
cacheConfig.setThreadPoolAdapterStates(threadPoolStates);
cacheConfigList.add(cacheConfig);
});
}
return cacheConfigList;
}
public void doRegister(List<ThreadPoolAdapterCacheConfig> cacheConfigList) {
if (CollectionUtil.isNotEmpty(cacheConfigList)) {
if (CollectionUtil.isNotEmpty(cacheConfigList) && cacheConfigList.size() > 0) {
try {
Result result = httpAgent.httpPost(REGISTER_ADAPTER_PATH, cacheConfigList);
if (!result.isSuccess()) {

Loading…
Cancel
Save