Add dubbo 2.x thread pool support

pull/234/head
iwangjie 3 years ago
parent 6bffd919c2
commit 23992432d1

@ -20,16 +20,27 @@ package cn.hippo4j.adapter.dubbo;
import cn.hippo4j.adapter.base.ThreadPoolAdapter;
import cn.hippo4j.adapter.base.ThreadPoolAdapterParameter;
import cn.hippo4j.adapter.base.ThreadPoolAdapterState;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.store.DataStore;
import org.apache.dubbo.remoting.Constants;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;
/**
* Dubbo thread-pool adapter.
*/
@Slf4j
public class DubboThreadPoolAdapter implements ThreadPoolAdapter, ApplicationListener<ApplicationStartedEvent> {
private final Map<String, ThreadPoolExecutor> DUBBO_PROTOCOL_EXECUTOR = Maps.newHashMap();
@Override
public String mark() {
return "Dubbo";
@ -37,16 +48,44 @@ public class DubboThreadPoolAdapter implements ThreadPoolAdapter, ApplicationLis
@Override
public ThreadPoolAdapterState getThreadPoolState(String identify) {
return null;
ThreadPoolAdapterState threadPoolAdapterState = new ThreadPoolAdapterState();
final ThreadPoolExecutor tp = DUBBO_PROTOCOL_EXECUTOR.get(identify);
if (tp == null) {
return threadPoolAdapterState;
}
threadPoolAdapterState.setThreadPoolKey(identify);
threadPoolAdapterState.setActive(tp.getActiveCount() + "");
threadPoolAdapterState.setCoreSize(tp.getCorePoolSize());
threadPoolAdapterState.setMaximumSize(tp.getMaximumPoolSize());
return threadPoolAdapterState;
}
@Override
public List<ThreadPoolAdapterState> getThreadPoolStates() {
List<ThreadPoolAdapterState> threadPoolAdapterStates = new ArrayList<>();
DUBBO_PROTOCOL_EXECUTOR.forEach((k, v) -> threadPoolAdapterStates.add(getThreadPoolState(k)));
return threadPoolAdapterStates;
}
@Override
public boolean updateThreadPool(ThreadPoolAdapterParameter threadPoolAdapterParameter) {
return false;
final ThreadPoolExecutor tp = DUBBO_PROTOCOL_EXECUTOR.get(threadPoolAdapterParameter.getThreadPoolKey());
if (tp == null) {
return false;
}
tp.setCorePoolSize(threadPoolAdapterParameter.getCoreSize());
tp.setMaximumPoolSize(threadPoolAdapterParameter.getMaximumSize());
return true;
}
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
try {
DataStore dataStore = ExtensionLoader.getExtensionLoader(DataStore.class).getDefaultExtension();
Map<String, Object> executors = dataStore.get(Constants.EXECUTOR_SERVICE_COMPONENT_KEY);
executors.forEach((key, value) -> DUBBO_PROTOCOL_EXECUTOR.put(key, (ThreadPoolExecutor) value));
} catch (Exception e) {
log.error("Failed to get Dubbo protocol thread pool", e);
}
}
}

Loading…
Cancel
Save