Skip to content

Commit 5553851

Browse files
committed
fix event bug
1 parent 602ad59 commit 5553851

File tree

6 files changed

+27
-33
lines changed

6 files changed

+27
-33
lines changed

pom.xml

+1-1
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.8.8</version>
15+
<version>2.8.9</version>
1616

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

springboot-starter-data-fast/pom.xml

+1-1
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.8.8</version>
8+
<version>2.8.9</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

springboot-starter-security/pom.xml

+1-1
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.8.8</version>
9+
<version>2.8.9</version>
1010
</parent>
1111

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

springboot-starter/pom.xml

+1-1
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.8.8</version>
8+
<version>2.8.9</version>
99
</parent>
1010
<artifactId>springboot-starter</artifactId>
1111

Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
package com.codingapi.springboot.framework.handler;
22

33
import com.codingapi.springboot.framework.event.IEvent;
4-
import lombok.extern.slf4j.Slf4j;
54

6-
import java.lang.reflect.ParameterizedType;
7-
import java.lang.reflect.Type;
85
import java.util.ArrayList;
96
import java.util.List;
107

11-
@Slf4j
128
class ApplicationHandlerUtils implements IHandler<IEvent> {
139

1410
private static ApplicationHandlerUtils instance;
15-
private List<IHandler<IEvent>> handlers;
11+
private final List<IHandler<IEvent>> handlers;
1612

1713

1814
private ApplicationHandlerUtils() {
@@ -47,39 +43,27 @@ public void addHandler(IHandler handler) {
4743
@Override
4844
public void handler(IEvent event) {
4945
for (IHandler<IEvent> handler : handlers) {
50-
String targetClassName = null;
5146
try {
5247
Class<?> eventClass = event.getClass();
53-
Class<?> targetClass = getHandlerEventClass(handler);
48+
Class<?> targetClass = handler.getHandlerEventClass();
5449
if (eventClass.equals(targetClass)) {
55-
targetClassName = targetClass.getName();
5650
handler.handler(event);
5751
}
5852
} catch (Exception e) {
59-
//IPersistenceEvent 抛出异常
60-
if ("com.codingapi.springboot.framework.persistence.PersistenceEvent".equals(targetClassName)) {
61-
throw e;
53+
Exception error = null;
54+
try {
55+
handler.error(e);
56+
} catch (Exception err) {
57+
error = err;
6258
}
63-
log.warn("handler exception", e);
64-
handler.error(e);
65-
66-
}
67-
}
68-
}
69-
70-
private Class<?> getHandlerEventClass(IHandler<IEvent> handler) {
71-
Type[] types = handler.getClass().getGenericInterfaces();
72-
for (Type type : types) {
73-
if (type instanceof ParameterizedType) {
74-
ParameterizedType parameterizedType = (ParameterizedType) type;
75-
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
76-
if (actualTypeArguments != null) {
77-
return (Class<?>) actualTypeArguments[0];
59+
if (error != null) {
60+
throw new RuntimeException(error);
7861
}
7962
}
8063
}
81-
return null;
8264
}
8365

8466

67+
68+
8569
}

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.codingapi.springboot.framework.handler;
22

33
import com.codingapi.springboot.framework.event.IEvent;
4+
import org.springframework.core.ResolvableType;
45

56
/**
67
* handler 订阅
@@ -21,9 +22,18 @@ public interface IHandler<T extends IEvent> {
2122
*
2223
* @param exception 异常信息
2324
*/
24-
default void error(Exception exception) {
25+
default void error(Exception exception) throws Exception{
26+
throw exception;
27+
}
28+
29+
30+
/**
31+
* 获取订阅的事件类型
32+
*/
33+
default Class<?> getHandlerEventClass() {
34+
ResolvableType resolvableType = ResolvableType.forClass(getClass()).as(IHandler.class);
35+
return (Class<T>) resolvableType.getGeneric(0).resolve();
2536
}
2637

27-
;
2838

2939
}

0 commit comments

Comments
 (0)