From a461daa371852fd193b5215ecc3abe8d12adf843 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=87=8C=E9=A3=8E?= <30565113+lingfengcoder@users.noreply.github.com> Date: Fri, 20 Jan 2023 11:23:14 +0800 Subject: [PATCH] feature:support zipkin pool and add a adapter (#1047) * feature:add zipkin pool adapter * feature:add zipkin pool adapter Co-authored-by: wangzhuo --- .../hippo4j/common/toolkit/ReflectUtil.java | 18 +++++ .../DynamicThreadPoolAdapterChoose.java | 1 + .../support/adpter/ZipkinExecutorAdapter.java | 80 +++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/adpter/ZipkinExecutorAdapter.java diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/ReflectUtil.java b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/ReflectUtil.java index d278ec55..1e18e84f 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/ReflectUtil.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/ReflectUtil.java @@ -264,4 +264,22 @@ public class ReflectUtil { throw new IllegalException(e); } } + + /** + * find field by fieldName and fieldType + * @param obj target obj + * @param filedName filedName + * @param fieldType fieldType + * @return target field or null + */ + public static Field findField(Object obj, String filedName, String fieldType) { + Field[] fields = ReflectUtil.getFields(obj.getClass()); + for (Field field : fields) { + if (field.getName().contains(filedName) && + (field.getType().getName().contains(fieldType))) { + return field; + } + } + return null; + } } diff --git a/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/adpter/DynamicThreadPoolAdapterChoose.java b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/adpter/DynamicThreadPoolAdapterChoose.java index 64ac174c..55541893 100644 --- a/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/adpter/DynamicThreadPoolAdapterChoose.java +++ b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/adpter/DynamicThreadPoolAdapterChoose.java @@ -35,6 +35,7 @@ public class DynamicThreadPoolAdapterChoose { DYNAMIC_THREAD_POOL_ADAPTERS.add(new TransmittableThreadLocalExecutorAdapter()); DYNAMIC_THREAD_POOL_ADAPTERS.add(new TransmittableThreadLocalExecutorServiceAdapter()); DYNAMIC_THREAD_POOL_ADAPTERS.add(new ThreadPoolTaskExecutorAdapter()); + DYNAMIC_THREAD_POOL_ADAPTERS.add(new ZipkinExecutorAdapter()); } /** diff --git a/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/adpter/ZipkinExecutorAdapter.java b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/adpter/ZipkinExecutorAdapter.java new file mode 100644 index 00000000..5af90f9f --- /dev/null +++ b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/adpter/ZipkinExecutorAdapter.java @@ -0,0 +1,80 @@ +/* + * 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.core.executor.support.adpter; + +import cn.hippo4j.common.toolkit.ReflectUtil; +import cn.hippo4j.core.executor.DynamicThreadPoolExecutor; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +import java.lang.reflect.Field; +import java.util.Objects; +import java.util.concurrent.Executor; + +/** + * Zipkin thread local executor adapter. + */ +public class ZipkinExecutorAdapter implements DynamicThreadPoolAdapter { + + private final static String MATCH_CLASS_NAME = "brave.internal.WrappingExecutorService"; + private final static String FIELD_NAME = "delegate"; + private final static String TYPE_NAME = "java.util.concurrent.ExecutorService"; + + @Override + public boolean match(Object executor) { + return matchSuper(executor); + } + + public boolean matchSuper(Object executor) { + if (Objects.equals(MATCH_CLASS_NAME, executor.getClass().getName())) { + return true; + } else { + return Objects.equals(MATCH_CLASS_NAME, executor.getClass().getSuperclass().getName()); + } + } + + @Override + public DynamicThreadPoolExecutor unwrap(Object executor) { + Object unwrap = doUnwrap(executor); + if (unwrap instanceof DynamicThreadPoolExecutor) { + return (DynamicThreadPoolExecutor) unwrap; + } + if (executor instanceof ThreadPoolTaskExecutor) { + return new ThreadPoolTaskExecutorAdapter().unwrap(executor); + } + return null; + } + + @Override + public void replace(Object executor, Executor dynamicThreadPoolExecutor) { + Field field = ReflectUtil.findField(executor, FIELD_NAME, TYPE_NAME); + ReflectUtil.setFieldValue(executor, field, dynamicThreadPoolExecutor); + } + + private Object doUnwrap(Object executor) { + Object unwrap = ReflectUtil.getFieldValue(executor, FIELD_NAME); + if (unwrap == null) { + Field field = ReflectUtil.findField(executor, FIELD_NAME, TYPE_NAME); + if (field != null) { + return ReflectUtil.getFieldValue(executor, field); + } + } + return null; + } + + +}