Skip to content

Commit 19e4941

Browse files
committed
添加线程池变量传递的aop类
1 parent 13a17eb commit 19e4941

File tree

9 files changed

+174
-4
lines changed

9 files changed

+174
-4
lines changed

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
<guava.version>27.0.1-jre</guava.version>
7373
<mapstruct.version>1.1.0.Final</mapstruct.version>
7474
<caffeine.version>2.7.0</caffeine.version>
75+
<aspectj.version>1.8.13</aspectj.version>
7576
</properties>
7677

7778

@@ -163,6 +164,13 @@
163164
<version>${project.version}</version>
164165
</dependency>
165166

167+
<dependency>
168+
<groupId>org.aspectj</groupId>
169+
<artifactId>aspectjrt</artifactId>
170+
<version>${aspectj.version}</version>
171+
<scope>compile</scope>
172+
</dependency>
173+
166174

167175
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
168176
<dependency>

spring-cloud-gray-client-netflix/pom.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@
102102
<dependency>
103103
<groupId>org.aspectj</groupId>
104104
<artifactId>aspectjrt</artifactId>
105-
<version>1.8.13</version>
106-
<scope>compile</scope>
107105
</dependency>
108106

109107

spring-cloud-gray-client/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@
7373
<scope>compile</scope>
7474
</dependency>
7575

76+
<dependency>
77+
<groupId>org.aspectj</groupId>
78+
<artifactId>aspectjrt</artifactId>
79+
</dependency>
80+
7681
</dependencies>
7782

7883

spring-cloud-gray-client/src/main/java/cn/springcloud/gray/concurrent/GrayConcurrentHelper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,5 @@ public static GrayTrackInfo getGrayTrackInfo() {
6161
RequestLocalStorage requestLocalStorage = GrayClientHolder.getRequestLocalStorage();
6262
return requestLocalStorage == null ? null : requestLocalStorage.getGrayTrackInfo();
6363
}
64+
6465
}

spring-cloud-gray-client/src/main/java/cn/springcloud/gray/concurrent/GrayExecutor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@ public GrayExecutor(Executor delegater) {
1414
public void execute(Runnable command) {
1515
delegater.execute(GrayConcurrentHelper.createDelegateRunnable(command));
1616
}
17+
18+
public Executor getDelegater() {
19+
return delegater;
20+
}
1721
}

spring-cloud-gray-client/src/main/java/cn/springcloud/gray/concurrent/GrayExecutorService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,7 @@ public void execute(Runnable command) {
9292
}
9393

9494

95+
public ExecutorService getDelegater() {
96+
return delegater;
97+
}
9598
}

spring-cloud-gray-client/src/main/java/cn/springcloud/gray/concurrent/GrayTaskDecorator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package cn.springcloud.gray.concurrent;
22

3-
import cn.springcloud.gray.request.RequestLocalStorage;
43
import org.springframework.core.task.TaskDecorator;
54

65
public class GrayTaskDecorator implements TaskDecorator {
76

87

9-
public GrayTaskDecorator(RequestLocalStorage requestLocalStorage) {
8+
public GrayTaskDecorator() {
109
}
1110

1211
@Override
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cn.springcloud.gray.concurrent.aspect;
2+
3+
import cn.springcloud.gray.concurrent.GrayConcurrentHelper;
4+
import cn.springcloud.gray.concurrent.GrayExecutor;
5+
import cn.springcloud.gray.concurrent.GrayExecutorService;
6+
import cn.springcloud.gray.concurrent.GrayRunnable;
7+
import org.aspectj.lang.ProceedingJoinPoint;
8+
import org.aspectj.lang.annotation.Around;
9+
import org.aspectj.lang.annotation.Aspect;
10+
import org.aspectj.lang.annotation.Pointcut;
11+
12+
@Aspect
13+
public class ExecutorGrayAspect {
14+
15+
@Pointcut("execution(* java.util.concurrent.Executor.execute(..)) && args(command)")
16+
public void pointCut(Runnable command) {
17+
18+
}
19+
20+
21+
@Around("pointCut(command)")
22+
public Object aroundLog(ProceedingJoinPoint joinpoint, Runnable command) throws Throwable {
23+
if (joinpoint.getTarget() instanceof GrayExecutor
24+
|| joinpoint.getTarget() instanceof GrayExecutorService
25+
|| command instanceof GrayRunnable) {
26+
return joinpoint.proceed();
27+
}
28+
29+
Runnable runnable = GrayConcurrentHelper.createDelegateRunnable(command);
30+
return joinpoint.proceed(new Object[]{runnable});
31+
32+
}
33+
34+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package cn.springcloud.gray.concurrent.aspect;
2+
3+
import cn.springcloud.gray.concurrent.*;
4+
import org.aspectj.lang.ProceedingJoinPoint;
5+
import org.aspectj.lang.annotation.Around;
6+
import org.aspectj.lang.annotation.Aspect;
7+
import org.aspectj.lang.annotation.Pointcut;
8+
9+
import java.util.Collection;
10+
import java.util.concurrent.Callable;
11+
import java.util.concurrent.TimeUnit;
12+
13+
@Aspect
14+
public class ExecutorServiceGrayAspect {
15+
16+
@Pointcut("execution(* java.util.concurrent.ExecutorService.execute(..)) && args(command)")
17+
public void executePointCut(Runnable command) {
18+
19+
}
20+
21+
22+
@Pointcut("execution(* java.util.concurrent.ExecutorService.submit(..)) && args(command)")
23+
public void submitPointCut(Callable command) {
24+
25+
}
26+
27+
@Pointcut("execution(* java.util.concurrent.ExecutorService.submit(..)) && args(command, result)")
28+
public void submitPointCut2(Runnable command, Object result) {
29+
30+
}
31+
32+
33+
@Pointcut("execution(* java.util.concurrent.ExecutorService.submit(..)) && args(command)")
34+
public void submitPointCut3(Runnable command) {
35+
36+
}
37+
38+
@Pointcut("(execution(* java.util.concurrent.ExecutorService.invokeAll(..)) || execution(* java.util.concurrent.ExecutorService.invokeAny(..)) ) && args(tasks)")
39+
public <V> void invokeAllOrAnyPointCut(Collection<? extends Callable<V>> tasks) {
40+
41+
}
42+
43+
@Pointcut("(execution(* java.util.concurrent.ExecutorService.invokeAll(..)) || execution(* java.util.concurrent.ExecutorService.invokeAny(..))) && args(tasks, timeout, unit)")
44+
public <V> void invokeAllOrAnyPointCut2(Collection<? extends Callable<V>> tasks, long timeout, TimeUnit unit) {
45+
46+
}
47+
48+
@Around("executePointCut(command)")
49+
public Object executeAround(ProceedingJoinPoint joinpoint, Runnable command) throws Throwable {
50+
if (joinpoint.getTarget() instanceof GrayExecutor
51+
|| joinpoint.getTarget() instanceof GrayExecutorService
52+
|| command instanceof GrayRunnable) {
53+
return joinpoint.proceed();
54+
}
55+
56+
Runnable runnable = GrayConcurrentHelper.createDelegateRunnable(command);
57+
return joinpoint.proceed(new Object[]{runnable});
58+
59+
}
60+
61+
62+
@Around("submitPointCut(command)")
63+
public Object submitAround(ProceedingJoinPoint joinpoint, Callable command) throws Throwable {
64+
if (joinpoint.getTarget() instanceof GrayExecutorService
65+
|| command instanceof GrayCallable) {
66+
return joinpoint.proceed();
67+
}
68+
69+
Callable runnable = GrayConcurrentHelper.createDelegateCallable(command);
70+
return joinpoint.proceed(new Object[]{runnable});
71+
72+
}
73+
74+
@Around("submitPointCut2(command, result)")
75+
public Object submitAround(ProceedingJoinPoint joinpoint, Runnable command, Object result) throws Throwable {
76+
if (joinpoint.getTarget() instanceof GrayExecutorService
77+
|| command instanceof GrayRunnable) {
78+
return joinpoint.proceed();
79+
}
80+
81+
Runnable runnable = GrayConcurrentHelper.createDelegateRunnable(command);
82+
return joinpoint.proceed(new Object[]{runnable, result});
83+
}
84+
85+
@Around("submitPointCut3(command)")
86+
public Object submitAround(ProceedingJoinPoint joinpoint, Runnable command) throws Throwable {
87+
if (joinpoint.getTarget() instanceof GrayExecutorService
88+
|| command instanceof GrayRunnable) {
89+
return joinpoint.proceed();
90+
}
91+
92+
Runnable runnable = GrayConcurrentHelper.createDelegateRunnable(command);
93+
return joinpoint.proceed(new Object[]{runnable});
94+
}
95+
96+
@Around("invokeAllOrAnyPointCut(tasks)")
97+
public <V> Object invokeAllOrAnyAround(ProceedingJoinPoint joinpoint, Collection<? extends Callable<V>> tasks) throws Throwable {
98+
if (joinpoint.getTarget() instanceof GrayExecutorService) {
99+
return joinpoint.proceed();
100+
}
101+
102+
return joinpoint.proceed(new Object[]{GrayConcurrentHelper.mapDelegateCallables(tasks)});
103+
}
104+
105+
@Around("invokeAllOrAnyPointCut2(tasks, timeout, unit)")
106+
public <V> Object invokeAllOrAnyAround(
107+
ProceedingJoinPoint joinpoint,
108+
Collection<? extends Callable<V>> tasks,
109+
long timeout, TimeUnit unit) throws Throwable {
110+
if (joinpoint.getTarget() instanceof GrayExecutorService) {
111+
return joinpoint.proceed();
112+
}
113+
114+
return joinpoint.proceed(new Object[]{GrayConcurrentHelper.mapDelegateCallables(tasks), timeout, unit});
115+
}
116+
117+
118+
}

0 commit comments

Comments
 (0)