提交 ca491326 编写于 作者: oldratlee's avatar oldratlee 🔥

add unwrap method for TtlExecutors to get the original/underneath executor #112

上级 16ffb128
......@@ -83,4 +83,9 @@ class ExecutorServiceTtlWrapper extends ExecutorTtlWrapper implements ExecutorSe
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
return executorService.invokeAny(TtlCallable.gets(tasks), timeout, unit);
}
@Override
public ExecutorService unwrap() {
return executorService;
}
}
......@@ -24,4 +24,8 @@ class ExecutorTtlWrapper implements Executor {
public void execute(Runnable command) {
executor.execute(TtlRunnable.get(command));
}
public Executor unwrap() {
return executor;
}
}
......@@ -4,10 +4,7 @@ import com.alibaba.ttl.TransmittableThreadLocal;
import com.alibaba.ttl.TtlCallable;
import com.alibaba.ttl.TtlRunnable;
import java.util.concurrent.Callable;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;
/**
* {@link TransmittableThreadLocal} Wrapper of {@link ScheduledExecutorService},
......@@ -44,4 +41,9 @@ class ScheduledExecutorServiceTtlWrapper extends ExecutorServiceTtlWrapper imple
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
return scheduledExecutorService.scheduleWithFixedDelay(TtlRunnable.get(command), initialDelay, delay, unit);
}
@Override
public ScheduledExecutorService unwrap() {
return scheduledExecutorService;
}
}
......@@ -7,7 +7,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
/**
* Factory Utils for getting TTL Wrapper of jdk executors.
* Factory Utils for getting TTL wrapper of jdk executors.
*
* @author Jerry Lee (oldratlee at gmail dot com)
* @see java.util.concurrent.Executor
......@@ -56,6 +56,48 @@ public final class TtlExecutors {
return new ScheduledExecutorServiceTtlWrapper(scheduledExecutorService);
}
/**
* check the executor is TTL wrapper executor or not.
* <p>
* if the parameter executor is TTL wrapper, return {@code true}, otherwise {@code false}.
* <p>
* NOTE: if input executor is {@code null}, return {@code false}.
*
* @param executor input executor
* @param <T> Executor type
* @see #getTtlExecutor(Executor)
* @see #getTtlExecutorService(ExecutorService)
* @see #getTtlScheduledExecutorService(ScheduledExecutorService)
* @see #unwrap(Executor)
* @since 2.8.0
*/
public static <T extends Executor> boolean isTtlWrapper(T executor) {
return (executor instanceof ExecutorTtlWrapper);
}
/**
* Unwrap TTL wrapper executor to the original/underneath one.
* <p>
* if the parameter executor is TTL wrapper, return the original/underneath executor;
* otherwise, just return the input parameter executor.
* <p>
* NOTE: if input executor is {@code null}, return {@code null}.
*
* @param executor input executor
* @param <T> Executor type
* @see #getTtlExecutor(Executor)
* @see #getTtlExecutorService(ExecutorService)
* @see #getTtlScheduledExecutorService(ScheduledExecutorService)
* @see #isTtlWrapper(Executor)
* @since 2.8.0
*/
@SuppressWarnings("unchecked")
public static <T extends Executor> T unwrap(T executor) {
if (!isTtlWrapper(executor)) return executor;
return (T) ((ExecutorTtlWrapper) executor).unwrap();
}
private TtlExecutors() {
}
}
......@@ -9,7 +9,10 @@ import com.alibaba.ttl.threadpool.agent.internal.transformlet.impl.TtlTimerTaskT
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Instrumentation;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
/**
......
package com.alibaba.ttl.threadpool.agent.internal.transformlet;
import javassist.CannotCompileException;
import javassist.CtClass;
import javassist.NotFoundException;
import java.io.IOException;
......
package com.alibaba.ttl.threadpool.agent.internal.transformlet.impl;
import com.alibaba.ttl.threadpool.agent.internal.logging.Logger;
import com.alibaba.ttl.threadpool.agent.internal.transformlet.JavassistTransformlet;
import javassist.*;
import java.io.IOException;
import java.lang.reflect.Modifier;
import com.alibaba.ttl.threadpool.agent.internal.logging.Logger;
import static com.alibaba.ttl.threadpool.agent.internal.transformlet.impl.Utils.getCtClass;
import static com.alibaba.ttl.threadpool.agent.internal.transformlet.impl.Utils.signatureOfMethod;
......
......@@ -6,8 +6,8 @@ import java.io.ByteArrayInputStream;
import java.io.IOException;
/**
* @since 2.6.0
* @author Jerry Lee (oldratlee at gmail dot com)
* @since 2.6.0
*/
class Utils {
/**
......
......@@ -3,7 +3,6 @@
package com.alibaba.demo.agent
import com.alibaba.ttl.TransmittableThreadLocal
import java.lang.IllegalStateException
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
......
......@@ -2,11 +2,11 @@ package com.alibaba.demo.distributed_tracer.refcount
import com.alibaba.ttl.TransmittableThreadLocal
import com.alibaba.ttl.threadpool.TtlExecutors
import java.lang.Thread.sleep
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.Executors
import java.util.concurrent.atomic.AtomicInteger
import kotlin.concurrent.thread
import java.lang.Thread.sleep
/**
* DistributedTracer(DT) use demo.
......
package com.alibaba.demo.timer
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Timer
import java.util.TimerTask
import java.util.*
/**
* @see [Java Timer TimerTask Example](https://www.journaldev.com/1050/java-timer-timertask-example)
......
package com.alibaba.ttl.threadpool
import org.junit.Assert.assertNull
import com.alibaba.ttl.threadpool.TtlExecutors.*
import org.junit.Assert.*
import org.junit.Test
import java.util.concurrent.Executors.newScheduledThreadPool
/**
* @author Jerry Lee (oldratlee at gmail dot com)
*/
class TtlExecutorsTest {
@Test
fun test_common() {
val newScheduledThreadPool = newScheduledThreadPool(3)
getTtlExecutor(newScheduledThreadPool).let {
assertTrue(it is ExecutorTtlWrapper)
assertTrue(isTtlWrapper(it))
assertSame(newScheduledThreadPool, unwrap(it))
}
getTtlExecutorService(newScheduledThreadPool).let {
assertTrue(it is ExecutorServiceTtlWrapper)
assertTrue(isTtlWrapper(it))
assertSame(newScheduledThreadPool, unwrap(it))
}
getTtlScheduledExecutorService(newScheduledThreadPool).let {
assertTrue(it is ScheduledExecutorServiceTtlWrapper)
assertTrue(isTtlWrapper(it))
assertSame(newScheduledThreadPool, unwrap(it))
}
}
@Test
fun test_null() {
assertNull(TtlExecutors.getTtlExecutor(null))
assertNull(TtlExecutors.getTtlExecutorService(null))
assertNull(TtlExecutors.getTtlScheduledExecutorService(null))
assertNull(getTtlScheduledExecutorService(null))
assertFalse(isTtlWrapper(null))
assertNull(unwrap(null))
}
}
package com.alibaba.ttl.threadpool.agent
import com.alibaba.ttl.threadpool.agent.TtlAgent.splitCommaColonStringToKV
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.Assert.*
class TtlAgentTest {
@Test
fun test_splitCommaColonStringToKV() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册