Skip to content

Commit eae1705

Browse files
committed
fix #76
1 parent 83dae01 commit eae1705

File tree

17 files changed

+65
-34
lines changed

17 files changed

+65
-34
lines changed

example/example-application/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-example</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>3.3.4</version>
8+
<version>3.3.5</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

example/example-application/src/main/java/com/codingapi/example/handler/CHandler.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.codingapi.example.handler;
22

3-
import com.codingapi.example.event.AEvent;
43
import com.codingapi.example.event.CEvent;
5-
import com.codingapi.springboot.framework.event.EventPusher;
64
import com.codingapi.springboot.framework.event.EventTraceContext;
75
import com.codingapi.springboot.framework.event.IHandler;
86
import lombok.extern.slf4j.Slf4j;
@@ -16,6 +14,13 @@ public class CHandler implements IHandler<CEvent> {
1614
public void handler(CEvent event) {
1715
log.info("c event:{},eventKey:{}", event, EventTraceContext.getInstance().getEventKey());
1816

19-
EventPusher.push(new AEvent());
17+
// EventPusher.push(new AEvent());
18+
throw new RuntimeException("c handler error");
19+
}
20+
21+
@Override
22+
public void error(Exception exception) throws Exception {
23+
log.error("c handler error:{}", exception.getMessage());
24+
throw exception;
2025
}
2126
}

example/example-domain/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-example</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>3.3.4</version>
8+
<version>3.3.5</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

example/example-infra-flow/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-example</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>3.3.4</version>
8+
<version>3.3.5</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

example/example-infra-jpa/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-example</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>3.3.4</version>
8+
<version>3.3.5</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

example/example-server/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-example</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>3.3.4</version>
8+
<version>3.3.5</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

example/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
<parent>
1414
<groupId>com.codingapi.springboot</groupId>
1515
<artifactId>springboot-parent</artifactId>
16-
<version>3.3.4</version>
16+
<version>3.3.5</version>
1717
</parent>
1818

1919
<artifactId>springboot-example</artifactId>
20-
<version>3.3.4</version>
20+
<version>3.3.5</version>
2121

2222
<name>springboot-example</name>
2323
<description>springboot-example project for Spring Boot</description>

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>3.3.4</version>
15+
<version>3.3.5</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>3.3.4</version>
8+
<version>3.3.5</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>3.3.4</version>
9+
<version>3.3.5</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>3.3.4</version>
9+
<version>3.3.5</version>
1010
</parent>
1111

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

springboot-starter/pom.xml

Lines changed: 1 addition & 6 deletions
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>3.3.4</version>
8+
<version>3.3.5</version>
99
</parent>
1010
<artifactId>springboot-starter</artifactId>
1111

@@ -21,11 +21,6 @@
2121
<artifactId>spring-data-commons</artifactId>
2222
</dependency>
2323

24-
<dependency>
25-
<groupId>com.esotericsoftware</groupId>
26-
<artifactId>kryo</artifactId>
27-
</dependency>
28-
2924
<dependency>
3025
<groupId>jakarta.persistence</groupId>
3126
<artifactId>jakarta.persistence-api</artifactId>

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.codingapi.springboot.framework.event;
22

3+
import com.codingapi.springboot.framework.exception.EventException;
4+
import com.codingapi.springboot.framework.exception.EventLoopException;
5+
36
import java.util.ArrayList;
47
import java.util.List;
58

@@ -40,28 +43,32 @@ public void addHandler(IHandler handler) {
4043

4144
@Override
4245
public void handler(IEvent event) {
46+
Class<?> eventClass = event.getClass();
47+
List<Exception> errorStack = new ArrayList<>();
48+
boolean throwException = false;
4349
for (IHandler<IEvent> handler : handlers) {
4450
try {
45-
Class<?> eventClass = event.getClass();
4651
Class<?> targetClass = handler.getHandlerEventClass();
4752
if (eventClass.equals(targetClass)) {
4853
handler.handler(event);
4954
}
5055
} catch (Exception e) {
51-
Exception error = null;
56+
if (e instanceof EventLoopException) {
57+
throw e;
58+
}
5259
try {
5360
handler.error(e);
61+
errorStack.add(e);
5462
} catch (Exception err) {
55-
error = err;
56-
}
57-
if (error != null) {
58-
throw new RuntimeException(error);
63+
throwException = true;
64+
errorStack.add(err);
5965
}
6066
}
6167
}
68+
if(throwException){
69+
throw new EventException(errorStack);
70+
}
6271
}
6372

6473

65-
66-
6774
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ void checkEventState() {
6161
traceKeys.remove(traceId);
6262
EventStackContext.getInstance().remove(traceId);
6363
}
64+
eventKeyState.remove(eventKey);
6465
}
65-
eventKeyState.remove(eventKey);
6666
threadLocal.remove();
6767
}
6868

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public interface IHandler<T extends IEvent> {
1818

1919
/**
2020
* 异常回掉,在多订阅的情况下,为了实现订阅的独立性,将异常的处理放在回掉函数中。
21+
* 当异常抛出以后,会阻止后续的事件执行
2122
*
2223
* @param exception 异常信息
2324
*/

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,20 @@ public void onApplicationEvent(DomainEvent domainEvent) {
3030
String traceId = domainEvent.getTraceId();
3131

3232
if (domainEvent.isSync()) {
33-
EventTraceContext.getInstance().createEventKey(traceId);
34-
ApplicationHandlerUtils.getInstance().handler(domainEvent.getEvent());
35-
EventTraceContext.getInstance().checkEventState();
36-
} else {
37-
executorService.execute(() -> {
33+
try {
3834
EventTraceContext.getInstance().createEventKey(traceId);
3935
ApplicationHandlerUtils.getInstance().handler(domainEvent.getEvent());
36+
} finally {
4037
EventTraceContext.getInstance().checkEventState();
38+
}
39+
} else {
40+
executorService.execute(() -> {
41+
try {
42+
EventTraceContext.getInstance().createEventKey(traceId);
43+
ApplicationHandlerUtils.getInstance().handler(domainEvent.getEvent());
44+
} finally {
45+
EventTraceContext.getInstance().checkEventState();
46+
}
4147
});
4248
}
4349
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.codingapi.springboot.framework.exception;
2+
3+
import lombok.Getter;
4+
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
8+
@Getter
9+
public class EventException extends RuntimeException {
10+
11+
private final List<Exception> error;
12+
13+
public EventException(List<Exception> error) {
14+
super(error.stream().map(Exception::getMessage).collect(Collectors.joining("\n")));
15+
this.error = error;
16+
}
17+
}

0 commit comments

Comments
 (0)