mirror of https://github.com/longtai-cn/hippo4j
Merge pull request #507 from mabaiwan/develop
Ali TTL framework thread pool adaptation (#506)pull/511/head
commit
e489651ab2
@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
* 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.core.executor.DynamicThreadPoolExecutor;
|
||||||
|
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dynamic thread pool adapter.
|
||||||
|
*/
|
||||||
|
public interface DynamicThreadPoolAdapter {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Match.
|
||||||
|
*
|
||||||
|
* @param executor
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
boolean match(Object executor);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unwrap.
|
||||||
|
*
|
||||||
|
* @param executor
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
DynamicThreadPoolExecutor unwrap(Object executor);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replace.
|
||||||
|
*
|
||||||
|
* @param executor
|
||||||
|
* @param dynamicThreadPoolExecutor
|
||||||
|
*/
|
||||||
|
void replace(Object executor, Executor dynamicThreadPoolExecutor);
|
||||||
|
}
|
@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
* 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.core.executor.DynamicThreadPoolExecutor;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dynamic thread pool adapter choose.
|
||||||
|
*/
|
||||||
|
public class DynamicThreadPoolAdapterChoose {
|
||||||
|
|
||||||
|
private static List<DynamicThreadPoolAdapter> DYNAMIC_THREAD_POOL_ADAPTERS = new ArrayList<>();
|
||||||
|
|
||||||
|
static {
|
||||||
|
DYNAMIC_THREAD_POOL_ADAPTERS.add(new TransmittableThreadLocalExecutorAdapter());
|
||||||
|
DYNAMIC_THREAD_POOL_ADAPTERS.add(new TransmittableThreadLocalExecutorServiceAdapter());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Match.
|
||||||
|
*
|
||||||
|
* @param executor
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static boolean match(Object executor) {
|
||||||
|
return DYNAMIC_THREAD_POOL_ADAPTERS.stream().anyMatch(each -> each.match(executor));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unwrap.
|
||||||
|
*
|
||||||
|
* @param executor
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static DynamicThreadPoolExecutor unwrap(Object executor) {
|
||||||
|
Optional<DynamicThreadPoolAdapter> dynamicThreadPoolAdapterOptional = DYNAMIC_THREAD_POOL_ADAPTERS.stream().filter(each -> each.match(executor)).findFirst();
|
||||||
|
return dynamicThreadPoolAdapterOptional.map(each -> each.unwrap(executor)).orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replace.
|
||||||
|
*
|
||||||
|
* @param executor
|
||||||
|
*/
|
||||||
|
public static void replace(Object executor, Executor dynamicThreadPoolExecutor) {
|
||||||
|
Optional<DynamicThreadPoolAdapter> dynamicThreadPoolAdapterOptional = DYNAMIC_THREAD_POOL_ADAPTERS.stream().filter(each -> each.match(executor)).findFirst();
|
||||||
|
if (dynamicThreadPoolAdapterOptional.isPresent()) {
|
||||||
|
dynamicThreadPoolAdapterOptional.get().replace(executor, dynamicThreadPoolExecutor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* 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 java.util.Objects;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transmittable thread local executor adapter.
|
||||||
|
*/
|
||||||
|
public class TransmittableThreadLocalExecutorAdapter implements DynamicThreadPoolAdapter {
|
||||||
|
|
||||||
|
private static String MATCH_CLASS_NAME = "ExecutorTtlWrapper";
|
||||||
|
|
||||||
|
private static String FIELD_NAME = "executor";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean match(Object executor) {
|
||||||
|
return Objects.equals(MATCH_CLASS_NAME, executor.getClass().getSimpleName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DynamicThreadPoolExecutor unwrap(Object executor) {
|
||||||
|
Object unwrap = ReflectUtil.getFieldValue(executor, FIELD_NAME);
|
||||||
|
if (unwrap != null && unwrap instanceof DynamicThreadPoolExecutor) {
|
||||||
|
return (DynamicThreadPoolExecutor) unwrap;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void replace(Object executor, Executor dynamicThreadPoolExecutor) {
|
||||||
|
ReflectUtil.setFieldValue(executor, FIELD_NAME, dynamicThreadPoolExecutor);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* 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 java.util.Objects;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transmittable thread local executor service adapter.
|
||||||
|
*/
|
||||||
|
public class TransmittableThreadLocalExecutorServiceAdapter implements DynamicThreadPoolAdapter {
|
||||||
|
|
||||||
|
private static String MATCH_CLASS_NAME = "ExecutorServiceTtlWrapper";
|
||||||
|
|
||||||
|
private static String FIELD_NAME = "executorService";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean match(Object executor) {
|
||||||
|
return Objects.equals(MATCH_CLASS_NAME, executor.getClass().getSimpleName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DynamicThreadPoolExecutor unwrap(Object executor) {
|
||||||
|
Object unwrap = ReflectUtil.getFieldValue(executor, FIELD_NAME);
|
||||||
|
if (unwrap != null && unwrap instanceof DynamicThreadPoolExecutor) {
|
||||||
|
return (DynamicThreadPoolExecutor) unwrap;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void replace(Object executor, Executor dynamicThreadPoolExecutor) {
|
||||||
|
ReflectUtil.setFieldValue(executor, FIELD_NAME, dynamicThreadPoolExecutor);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue