|
|
@ -18,13 +18,11 @@
|
|
|
|
package cn.hippo4j.adapter.hystrix;
|
|
|
|
package cn.hippo4j.adapter.hystrix;
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hippo4j.adapter.base.ThreadPoolAdapter;
|
|
|
|
import cn.hippo4j.adapter.base.ThreadPoolAdapter;
|
|
|
|
import cn.hippo4j.adapter.base.ThreadPoolAdapterExtra;
|
|
|
|
import cn.hippo4j.adapter.base.ThreadPoolAdapterScheduler;
|
|
|
|
import cn.hippo4j.adapter.base.ThreadPoolAdapterParameter;
|
|
|
|
import cn.hippo4j.adapter.base.ThreadPoolAdapterParameter;
|
|
|
|
import cn.hippo4j.adapter.base.ThreadPoolAdapterState;
|
|
|
|
import cn.hippo4j.adapter.base.ThreadPoolAdapterState;
|
|
|
|
import cn.hippo4j.common.config.ApplicationContextHolder;
|
|
|
|
|
|
|
|
import cn.hippo4j.common.toolkit.CollectionUtil;
|
|
|
|
import cn.hippo4j.common.toolkit.CollectionUtil;
|
|
|
|
import com.google.common.collect.Maps;
|
|
|
|
import com.google.common.collect.Maps;
|
|
|
|
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
|
|
|
|
|
|
|
import com.netflix.hystrix.HystrixThreadPool;
|
|
|
|
import com.netflix.hystrix.HystrixThreadPool;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.springframework.boot.context.event.ApplicationStartedEvent;
|
|
|
|
import org.springframework.boot.context.event.ApplicationStartedEvent;
|
|
|
@ -32,13 +30,10 @@ import org.springframework.context.ApplicationListener;
|
|
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
|
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
|
|
|
|
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
|
@ -54,15 +49,13 @@ public class HystrixThreadPoolAdapter implements ThreadPoolAdapter, ApplicationL
|
|
|
|
|
|
|
|
|
|
|
|
private static final String THREAD_POOLS_FIELD = "threadPools";
|
|
|
|
private static final String THREAD_POOLS_FIELD = "threadPools";
|
|
|
|
|
|
|
|
|
|
|
|
private static final int TASK_INTERVAL_SECONDS = 2;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final Map<String, ThreadPoolExecutor> HYSTRIX_CONSUME_EXECUTOR = Maps.newHashMap();
|
|
|
|
private final Map<String, ThreadPoolExecutor> HYSTRIX_CONSUME_EXECUTOR = Maps.newHashMap();
|
|
|
|
|
|
|
|
|
|
|
|
private ThreadPoolAdapterExtra threadPoolAdapterExtra;
|
|
|
|
private ThreadPoolAdapterScheduler threadPoolAdapterScheduler;
|
|
|
|
|
|
|
|
|
|
|
|
public HystrixThreadPoolAdapter(ThreadPoolAdapterExtra threadPoolAdapterExtra) {
|
|
|
|
public HystrixThreadPoolAdapter(ThreadPoolAdapterScheduler threadPoolAdapterScheduler) {
|
|
|
|
|
|
|
|
|
|
|
|
this.threadPoolAdapterExtra = threadPoolAdapterExtra;
|
|
|
|
this.threadPoolAdapterScheduler = threadPoolAdapterScheduler;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -113,9 +106,10 @@ public class HystrixThreadPoolAdapter implements ThreadPoolAdapter, ApplicationL
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onApplicationEvent(ApplicationStartedEvent event) {
|
|
|
|
public void onApplicationEvent(ApplicationStartedEvent event) {
|
|
|
|
ScheduledExecutorService scheduler = threadPoolAdapterExtra.getScheduler();
|
|
|
|
ScheduledExecutorService scheduler = threadPoolAdapterScheduler.getScheduler();
|
|
|
|
HystrixThreadPoolRefreshTask hystrixThreadPoolRefreshTask = new HystrixThreadPoolRefreshTask(scheduler);
|
|
|
|
int taskIntervalSeconds = threadPoolAdapterScheduler.getTaskIntervalSeconds();
|
|
|
|
scheduler.schedule(hystrixThreadPoolRefreshTask, TASK_INTERVAL_SECONDS, TimeUnit.SECONDS);
|
|
|
|
HystrixThreadPoolRefreshTask hystrixThreadPoolRefreshTask = new HystrixThreadPoolRefreshTask(scheduler,taskIntervalSeconds);
|
|
|
|
|
|
|
|
scheduler.schedule(hystrixThreadPoolRefreshTask, taskIntervalSeconds, TimeUnit.SECONDS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void hystrixThreadPoolRefresh() {
|
|
|
|
public void hystrixThreadPoolRefresh() {
|
|
|
@ -151,8 +145,11 @@ public class HystrixThreadPoolAdapter implements ThreadPoolAdapter, ApplicationL
|
|
|
|
|
|
|
|
|
|
|
|
private ScheduledExecutorService scheduler;
|
|
|
|
private ScheduledExecutorService scheduler;
|
|
|
|
|
|
|
|
|
|
|
|
public HystrixThreadPoolRefreshTask(ScheduledExecutorService scheduler) {
|
|
|
|
private int taskIntervalSeconds;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public HystrixThreadPoolRefreshTask(ScheduledExecutorService scheduler, int taskIntervalSeconds) {
|
|
|
|
this.scheduler = scheduler;
|
|
|
|
this.scheduler = scheduler;
|
|
|
|
|
|
|
|
this.taskIntervalSeconds = taskIntervalSeconds;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -161,7 +158,7 @@ public class HystrixThreadPoolAdapter implements ThreadPoolAdapter, ApplicationL
|
|
|
|
hystrixThreadPoolRefresh();
|
|
|
|
hystrixThreadPoolRefresh();
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
if (!scheduler.isShutdown()) {
|
|
|
|
if (!scheduler.isShutdown()) {
|
|
|
|
scheduler.schedule(this, TASK_INTERVAL_SECONDS, TimeUnit.MILLISECONDS);
|
|
|
|
scheduler.schedule(this, taskIntervalSeconds, TimeUnit.MILLISECONDS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|