Skip to content

Commit f39dfce

Browse files
committed
support loop event pusher
1 parent 98c8e1d commit f39dfce

File tree

8 files changed

+57
-11
lines changed

8 files changed

+57
-11
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<groupId>com.codingapi.springboot</groupId>
1414
<artifactId>springboot-parent</artifactId>
15-
<version>2.9.0</version>
15+
<version>2.9.1</version>
1616

1717
<url>https://github.com/codingapi/springboot-framewrok</url>
1818
<name>springboot-parent</name>

springboot-starter-data-fast/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>springboot-parent</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>2.9.0</version>
8+
<version>2.9.1</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

springboot-starter-flow/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<artifactId>springboot-parent</artifactId>
88
<groupId>com.codingapi.springboot</groupId>
9-
<version>2.9.0</version>
9+
<version>2.9.1</version>
1010
</parent>
1111

1212
<name>springboot-starter-flow</name>

springboot-starter-security/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<artifactId>springboot-parent</artifactId>
88
<groupId>com.codingapi.springboot</groupId>
9-
<version>2.9.0</version>
9+
<version>2.9.1</version>
1010
</parent>
1111

1212
<artifactId>springboot-starter-security</artifactId>

springboot-starter/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.codingapi.springboot</groupId>
77
<artifactId>springboot-parent</artifactId>
8-
<version>2.9.0</version>
8+
<version>2.9.1</version>
99
</parent>
1010
<artifactId>springboot-starter</artifactId>
1111

springboot-starter/src/main/java/com/codingapi/springboot/framework/event/DomainEventContext.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ public static DomainEventContext getInstance() {
2323
return instance;
2424
}
2525

26-
private void push(IEvent event, boolean sync) {
26+
private void push(IEvent event, boolean sync,boolean hasLoopEvent) {
2727
if (context != null) {
2828
String traceId = EventTraceContext.getInstance().getOrCreateTrace();
29+
if(hasLoopEvent){
30+
EventTraceContext.getInstance().clearTrace();
31+
traceId = EventTraceContext.getInstance().getOrCreateTrace();
32+
}
2933
EventTraceContext.getInstance().addEvent(traceId,event);
3034
context.publishEvent(new DomainEvent(event, sync,traceId));
3135
}
@@ -36,13 +40,13 @@ private void push(IEvent event, boolean sync) {
3640
* @see EventPusher
3741
* 默认 同步事件
3842
*/
39-
public void push(IEvent event) {
43+
public void push(IEvent event,boolean hasLoopEvent) {
4044
if (event instanceof IAsyncEvent) {
41-
this.push(event, false);
45+
this.push(event, false,hasLoopEvent);
4246
} else if (event instanceof ISyncEvent) {
43-
this.push(event, true);
47+
this.push(event, true,hasLoopEvent);
4448
} else {
45-
this.push(event, true);
49+
this.push(event, true,hasLoopEvent);
4650
}
4751
}
4852

springboot-starter/src/main/java/com/codingapi/springboot/framework/event/EventPusher.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,23 @@
55
*/
66
public class EventPusher {
77

8+
/**
9+
* 推送事件
10+
* 默认将自动检测事件是否有循环事件,当出现循环事件时,系统将会抛出循环调用异常。
11+
* @param event 事件
12+
*/
813
public static void push(IEvent event) {
9-
DomainEventContext.getInstance().push(event);
14+
push(event, false);
15+
}
16+
17+
/**
18+
* 推送事件
19+
* 默认将自动检测事件是否有循环事件,当出现循环事件时,系统将会抛出循环调用异常。
20+
* 设置hasLoopEvent为true,将不会检测循环事件。
21+
* @param event 事件
22+
* @param hasLoopEvent 是否有循环事件
23+
*/
24+
public static void push(IEvent event, boolean hasLoopEvent) {
25+
DomainEventContext.getInstance().push(event, hasLoopEvent);
1026
}
1127
}

springboot-starter/src/main/java/com/codingapi/springboot/framework/event/EventTraceContext.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,19 @@ public String getEventKey() {
4545
return threadLocal.get();
4646
}
4747

48+
/**
49+
* create event key
50+
* @param traceId traceId
51+
*/
4852
void createEventKey(String traceId) {
4953
String eventKey = traceId + "#" + RandomGenerator.randomString(8);
5054
eventKeyState.put(eventKey, false);
5155
threadLocal.set(eventKey);
5256
}
5357

58+
/**
59+
* check event state
60+
*/
5461
void checkEventState() {
5562
String eventKey = threadLocal.get();
5663
if (eventKey != null) {
@@ -66,6 +73,11 @@ void checkEventState() {
6673
threadLocal.remove();
6774
}
6875

76+
/**
77+
* add event
78+
* @param traceId traceId
79+
* @param event event
80+
*/
6981
void addEvent(String traceId, IEvent event) {
7082
boolean hasEventLoop = EventStackContext.getInstance().checkEventLoop(traceId, event);
7183
if (hasEventLoop) {
@@ -78,4 +90,18 @@ void addEvent(String traceId, IEvent event) {
7890
}
7991
EventStackContext.getInstance().addEvent(traceId, event);
8092
}
93+
94+
/**
95+
* clear trace
96+
*/
97+
public void clearTrace() {
98+
String eventKey = threadLocal.get();
99+
if (eventKey != null) {
100+
String traceId = eventKey.split("#")[0];
101+
traceKeys.remove(traceId);
102+
EventStackContext.getInstance().remove(traceId);
103+
eventKeyState.remove(eventKey);
104+
threadLocal.remove();
105+
}
106+
}
81107
}

0 commit comments

Comments
 (0)