Skip to content

Commit

Permalink
JSR 305 intergration #101
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Sep 28, 2018
1 parent 7d3c6dd commit 0125cf5
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 33 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ install:
test_script:
- ./mvnw.cmd clean install --batch-mode
- ./mvnw.cmd dependency:copy-dependencies -DincludeScope=test
- rm target/dependency/javassist*
- rm target/dependency/javassist* target/dependency/jsr305-*
- echo "Run Agent test"
- ps: |
$ttl_jar=Get-ChildItem target\transmittable-thread-local-*.jar -Exclude *-sources.jar | Select-Object -ExpandProperty FullName
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@
<version>3.23.1-GA</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
<optional>true</optional>
</dependency>
<!-- Testing frameworks and related dependencies -->
<dependency>
<groupId>junit</groupId>
Expand Down
6 changes: 6 additions & 0 deletions pom4ide.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@
<version>3.23.1-GA</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
<optional>true</optional>
</dependency>
<!-- Testing frameworks and related dependencies -->
<dependency>
<groupId>junit</groupId>
Expand Down
4 changes: 2 additions & 2 deletions scripts/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ mvnCopyDependencies() {
runCmd "${MVN_CMD[@]}" dependency:copy-dependencies -DincludeScope=test || die "fail to mvn copy-dependencies!"

# remove repackaged and shaded javassist lib
rm $dependencies_dir/javassist-*
rm "$dependencies_dir"/javassist-* "$dependencies_dir"/jsr305-*
}

getClasspathOfDependencies() {
[ -e "$dependencies_dir" ] || mvnCopyDependencies 1>&2

echo $dependencies_dir/*.jar | tr ' ' :
echo "$dependencies_dir"/*.jar | tr ' ' :
}

getClasspathWithoutTtlJar() {
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/com/alibaba/ttl/TransmittableThreadLocal.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.alibaba.ttl;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
Expand Down Expand Up @@ -145,7 +147,7 @@ private static void doExecuteCallback(boolean isBefore) {
/**
* Debug only method!
*/
static void dump(String title) {
static void dump(@Nullable String title) {
if (title != null && title.length() > 0) {
System.out.printf("Start TransmittableThreadLocal[%s] Dump...\n", title);
} else {
Expand Down Expand Up @@ -248,6 +250,7 @@ public static class Transmitter {
* @return the captured {@link TransmittableThreadLocal} values
* @since 2.3.0
*/
@Nonnull
public static Object capture() {
Map<TransmittableThreadLocal<?>, Object> captured = new HashMap<TransmittableThreadLocal<?>, Object>();
for (TransmittableThreadLocal<?> threadLocal : holder.get().keySet()) {
Expand All @@ -265,7 +268,8 @@ public static Object capture() {
* @see #capture()
* @since 2.3.0
*/
public static Object replay(Object captured) {
@Nonnull
public static Object replay(@Nonnull Object captured) {
@SuppressWarnings("unchecked")
Map<TransmittableThreadLocal<?>, Object> capturedMap = (Map<TransmittableThreadLocal<?>, Object>) captured;
Map<TransmittableThreadLocal<?>, Object> backup = new HashMap<TransmittableThreadLocal<?>, Object>();
Expand Down Expand Up @@ -301,7 +305,7 @@ public static Object replay(Object captured) {
* @param backup the backup {@link TransmittableThreadLocal} values from {@link Transmitter#replay(Object)}
* @since 2.3.0
*/
public static void restore(Object backup) {
public static void restore(@Nonnull Object backup) {
@SuppressWarnings("unchecked")
Map<TransmittableThreadLocal<?>, Object> backupMap = (Map<TransmittableThreadLocal<?>, Object>) backup;
// call afterExecute callback
Expand All @@ -324,7 +328,7 @@ public static void restore(Object backup) {
setTtlValuesTo(backupMap);
}

private static void setTtlValuesTo(Map<TransmittableThreadLocal<?>, Object> ttlValues) {
private static void setTtlValuesTo(@Nonnull Map<TransmittableThreadLocal<?>, Object> ttlValues) {
for (Map.Entry<TransmittableThreadLocal<?>, Object> entry : ttlValues.entrySet()) {
@SuppressWarnings("unchecked")
TransmittableThreadLocal<Object> threadLocal = (TransmittableThreadLocal<Object>) entry.getKey();
Expand All @@ -344,7 +348,7 @@ private static void setTtlValuesTo(Map<TransmittableThreadLocal<?>, Object> ttlV
* @see #restore(Object)
* @since 2.3.1
*/
public static <R> R runSupplierWithCaptured(Object captured, Supplier<R> bizLogic) {
public static <R> R runSupplierWithCaptured(@Nonnull Object captured, Supplier<R> bizLogic) {
Object backup = replay(captured);
try {
return bizLogic.get();
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/alibaba/ttl/TtlCallable.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.alibaba.ttl;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -31,7 +32,7 @@ public final class TtlCallable<V> implements Callable<V> {
private final Callable<V> callable;
private final boolean releaseTtlValueReferenceAfterCall;

private TtlCallable(Callable<V> callable, boolean releaseTtlValueReferenceAfterCall) {
private TtlCallable(@Nonnull Callable<V> callable, boolean releaseTtlValueReferenceAfterCall) {
this.capturedRef = new AtomicReference<Object>(capture());
this.callable = callable;
this.releaseTtlValueReferenceAfterCall = releaseTtlValueReferenceAfterCall;
Expand All @@ -55,6 +56,7 @@ public V call() throws Exception {
}
}

@Nonnull
public Callable<V> getCallable() {
return callable;
}
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/com/alibaba/ttl/TtlRunnable.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.alibaba.ttl;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -28,7 +30,7 @@ public final class TtlRunnable implements Runnable {
private final Runnable runnable;
private final boolean releaseTtlValueReferenceAfterRun;

private TtlRunnable(Runnable runnable, boolean releaseTtlValueReferenceAfterRun) {
private TtlRunnable(@Nonnull Runnable runnable, boolean releaseTtlValueReferenceAfterRun) {
this.capturedRef = new AtomicReference<Object>(capture());
this.runnable = runnable;
this.releaseTtlValueReferenceAfterRun = releaseTtlValueReferenceAfterRun;
Expand All @@ -55,6 +57,7 @@ public void run() {
/**
* return original/unwrapped {@link Runnable}.
*/
@Nonnull
public Runnable getRunnable() {
return runnable;
}
Expand Down Expand Up @@ -86,7 +89,8 @@ public String toString() {
* @return Wrapped {@link Runnable}
* @throws IllegalStateException when input is {@link TtlRunnable} already.
*/
public static TtlRunnable get(Runnable runnable) {
@Nullable
public static TtlRunnable get(@Nullable Runnable runnable) {
return get(runnable, false, false);
}

Expand All @@ -98,7 +102,8 @@ public static TtlRunnable get(Runnable runnable) {
* @return Wrapped {@link Runnable}
* @throws IllegalStateException when input is {@link TtlRunnable} already.
*/
public static TtlRunnable get(Runnable runnable, boolean releaseTtlValueReferenceAfterRun) {
@Nullable
public static TtlRunnable get(@Nullable Runnable runnable, boolean releaseTtlValueReferenceAfterRun) {
return get(runnable, releaseTtlValueReferenceAfterRun, false);
}

Expand All @@ -113,7 +118,8 @@ public static TtlRunnable get(Runnable runnable, boolean releaseTtlValueReferenc
* @return Wrapped {@link Runnable}
* @throws IllegalStateException when input is {@link TtlRunnable} already and not idempotent.
*/
public static TtlRunnable get(Runnable runnable, boolean releaseTtlValueReferenceAfterRun, boolean idempotent) {
@Nullable
public static TtlRunnable get(@Nullable Runnable runnable, boolean releaseTtlValueReferenceAfterRun, boolean idempotent) {
if (null == runnable) {
return null;
}
Expand All @@ -136,7 +142,8 @@ public static TtlRunnable get(Runnable runnable, boolean releaseTtlValueReferenc
* @return wrapped tasks
* @throws IllegalStateException when input is {@link TtlRunnable} already.
*/
public static List<TtlRunnable> gets(Collection<? extends Runnable> tasks) {
@Nonnull
public static List<TtlRunnable> gets(@Nullable Collection<? extends Runnable> tasks) {
return gets(tasks, false, false);
}

Expand All @@ -148,7 +155,8 @@ public static List<TtlRunnable> gets(Collection<? extends Runnable> tasks) {
* @return wrapped tasks
* @throws IllegalStateException when input is {@link TtlRunnable} already.
*/
public static List<TtlRunnable> gets(Collection<? extends Runnable> tasks, boolean releaseTtlValueReferenceAfterRun) {
@Nonnull
public static List<TtlRunnable> gets(@Nullable Collection<? extends Runnable> tasks, boolean releaseTtlValueReferenceAfterRun) {
return gets(tasks, releaseTtlValueReferenceAfterRun, false);
}

Expand All @@ -163,7 +171,8 @@ public static List<TtlRunnable> gets(Collection<? extends Runnable> tasks, boole
* @return wrapped tasks
* @throws IllegalStateException when input is {@link TtlRunnable} already and not idempotent.
*/
public static List<TtlRunnable> gets(Collection<? extends Runnable> tasks, boolean releaseTtlValueReferenceAfterRun, boolean idempotent) {
@Nonnull
public static List<TtlRunnable> gets(@Nullable Collection<? extends Runnable> tasks, boolean releaseTtlValueReferenceAfterRun, boolean idempotent) {
if (null == tasks) {
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.alibaba.ttl.TtlCallable;
import com.alibaba.ttl.TtlRunnable;

import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;
Expand All @@ -19,7 +20,7 @@
class ExecutorServiceTtlWrapper extends ExecutorTtlWrapper implements ExecutorService {
private final ExecutorService executorService;

ExecutorServiceTtlWrapper(ExecutorService executorService) {
ExecutorServiceTtlWrapper(@Nonnull ExecutorService executorService) {
super(executorService);
this.executorService = executorService;
}
Expand Down Expand Up @@ -84,6 +85,7 @@ public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, Ti
return executorService.invokeAny(TtlCallable.gets(tasks), timeout, unit);
}

@Nonnull
@Override
public ExecutorService unwrap() {
return executorService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.alibaba.ttl.TransmittableThreadLocal;
import com.alibaba.ttl.TtlRunnable;

import javax.annotation.Nonnull;
import java.util.concurrent.Executor;

/**
Expand All @@ -16,7 +17,7 @@
class ExecutorTtlWrapper implements Executor {
private final Executor executor;

ExecutorTtlWrapper(Executor executor) {
ExecutorTtlWrapper(@Nonnull Executor executor) {
this.executor = executor;
}

Expand All @@ -25,6 +26,7 @@ public void execute(Runnable command) {
executor.execute(TtlRunnable.get(command));
}

@Nonnull
public Executor unwrap() {
return executor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.alibaba.ttl.TtlCallable;
import com.alibaba.ttl.TtlRunnable;

import javax.annotation.Nonnull;
import java.util.concurrent.*;

/**
Expand All @@ -17,7 +18,7 @@
class ScheduledExecutorServiceTtlWrapper extends ExecutorServiceTtlWrapper implements ScheduledExecutorService {
final ScheduledExecutorService scheduledExecutorService;

public ScheduledExecutorServiceTtlWrapper(ScheduledExecutorService scheduledExecutorService) {
public ScheduledExecutorServiceTtlWrapper(@Nonnull ScheduledExecutorService scheduledExecutorService) {
super(scheduledExecutorService);
this.scheduledExecutorService = scheduledExecutorService;
}
Expand All @@ -43,6 +44,7 @@ public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialD
}

@Override
@Nonnull
public ScheduledExecutorService unwrap() {
return scheduledExecutorService;
}
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/com/alibaba/ttl/threadpool/TtlExecutors.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.alibaba.ttl.TransmittableThreadLocal;

import javax.annotation.Nullable;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
Expand All @@ -25,7 +26,8 @@ public final class TtlExecutors {
* transmit the {@link TransmittableThreadLocal} from the task submit time of {@link Runnable}
* to the execution time of {@link Runnable}.
*/
public static Executor getTtlExecutor(Executor executor) {
@Nullable
public static Executor getTtlExecutor(@Nullable Executor executor) {
if (null == executor || executor instanceof ExecutorTtlWrapper) {
return executor;
}
Expand All @@ -37,7 +39,8 @@ public static Executor getTtlExecutor(Executor executor) {
* transmit the {@link TransmittableThreadLocal} from the task submit time of {@link Runnable} or {@link java.util.concurrent.Callable}
* to the execution time of {@link Runnable} or {@link java.util.concurrent.Callable}.
*/
public static ExecutorService getTtlExecutorService(ExecutorService executorService) {
@Nullable
public static ExecutorService getTtlExecutorService(@Nullable ExecutorService executorService) {
if (executorService == null || executorService instanceof ExecutorServiceTtlWrapper) {
return executorService;
}
Expand All @@ -49,7 +52,8 @@ public static ExecutorService getTtlExecutorService(ExecutorService executorServ
* transmit the {@link TransmittableThreadLocal} from the task submit time of {@link Runnable} or {@link java.util.concurrent.Callable}
* to the execution time of {@link Runnable} or {@link java.util.concurrent.Callable}.
*/
public static ScheduledExecutorService getTtlScheduledExecutorService(ScheduledExecutorService scheduledExecutorService) {
@Nullable
public static ScheduledExecutorService getTtlScheduledExecutorService(@Nullable ScheduledExecutorService scheduledExecutorService) {
if (scheduledExecutorService == null || scheduledExecutorService instanceof ScheduledExecutorServiceTtlWrapper) {
return scheduledExecutorService;
}
Expand All @@ -71,7 +75,7 @@ public static ScheduledExecutorService getTtlScheduledExecutorService(ScheduledE
* @see #unwrap(Executor)
* @since 2.8.0
*/
public static <T extends Executor> boolean isTtlWrapper(T executor) {
public static <T extends Executor> boolean isTtlWrapper(@Nullable T executor) {
return (executor instanceof ExecutorTtlWrapper);
}

Expand All @@ -91,8 +95,9 @@ public static <T extends Executor> boolean isTtlWrapper(T executor) {
* @see #isTtlWrapper(Executor)
* @since 2.8.0
*/
@Nullable
@SuppressWarnings("unchecked")
public static <T extends Executor> T unwrap(T executor) {
public static <T extends Executor> T unwrap(@Nullable T executor) {
if (!isTtlWrapper(executor)) return executor;

return (T) ((ExecutorTtlWrapper) executor).unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ private fun rpcInvokeIn() {
decreaseSpanIdRefCount()
}

private val executorService = TtlExecutors.getTtlExecutorService(
Executors.newFixedThreadPool(1) { r: Runnable ->
Thread(r, "Executors").apply { isDaemon = true }
}
)
private val executorService = Executors.newFixedThreadPool(1) { r: Runnable ->
Thread(r, "Executors").apply { isDaemon = true }
}.let { TtlExecutors.getTtlExecutorService(it) }!!

private fun syncMethod() {
// async call by TTL Executor, Test OK!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private val executorService: ExecutorService = Executors.newFixedThreadPool(1) {
// ensure threads in pool is pre-created.
expandThreadPool(it)
TtlExecutors.getTtlExecutorService(it)
}
}!!

/**
* DistributedTracer(DT) use demo.
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/alibaba/ttl/TtlRunnableTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class TtlRunnableTest {
val ttlInstances = createParentTtlInstances()

val task = Task("1", ttlInstances)
val ttlRunnable = TtlRunnable.get(task)
val ttlRunnable = TtlRunnable.get(task)!!

// create after new Task, won't see parent value in in task!
createParentTtlInstancesAfterCreateChild(ttlInstances)
Expand Down Expand Up @@ -204,7 +204,7 @@ class TtlRunnableTest {
@Test
fun test_get_same() {
val task = Task("1")
val ttlRunnable = TtlRunnable.get(task)
val ttlRunnable = TtlRunnable.get(task)!!
assertSame(task, ttlRunnable.runnable)
}

Expand Down
Loading

0 comments on commit 0125cf5

Please sign in to comment.