feature:support zipkin pool and add a adapter (#1047)

* feature:add zipkin pool adapter

* feature:add zipkin pool adapter

Co-authored-by: wangzhuo <wangzhuo@shuwen.com>
pull/1048/head
凌风 2 years ago committed by GitHub
parent 27baf87ec7
commit a461daa371
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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;
}
}

@ -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());
}
/**

@ -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;
}
}
Loading…
Cancel
Save