|
1 | 1 | package com.codingapi.springboot.framework.handler;
|
2 | 2 |
|
3 | 3 | import com.codingapi.springboot.framework.event.IEvent;
|
4 |
| -import lombok.extern.slf4j.Slf4j; |
5 | 4 |
|
6 |
| -import java.lang.reflect.ParameterizedType; |
7 |
| -import java.lang.reflect.Type; |
8 | 5 | import java.util.ArrayList;
|
9 | 6 | import java.util.List;
|
10 | 7 |
|
11 |
| -@Slf4j |
12 | 8 | class ApplicationHandlerUtils implements IHandler<IEvent> {
|
13 | 9 |
|
14 | 10 | private static ApplicationHandlerUtils instance;
|
15 |
| - private List<IHandler<IEvent>> handlers; |
| 11 | + private final List<IHandler<IEvent>> handlers; |
16 | 12 |
|
17 | 13 |
|
18 | 14 | private ApplicationHandlerUtils() {
|
@@ -47,39 +43,27 @@ public void addHandler(IHandler handler) {
|
47 | 43 | @Override
|
48 | 44 | public void handler(IEvent event) {
|
49 | 45 | for (IHandler<IEvent> handler : handlers) {
|
50 |
| - String targetClassName = null; |
51 | 46 | try {
|
52 | 47 | Class<?> eventClass = event.getClass();
|
53 |
| - Class<?> targetClass = getHandlerEventClass(handler); |
| 48 | + Class<?> targetClass = handler.getHandlerEventClass(); |
54 | 49 | if (eventClass.equals(targetClass)) {
|
55 |
| - targetClassName = targetClass.getName(); |
56 | 50 | handler.handler(event);
|
57 | 51 | }
|
58 | 52 | } 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; |
62 | 58 | }
|
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); |
78 | 61 | }
|
79 | 62 | }
|
80 | 63 | }
|
81 |
| - return null; |
82 | 64 | }
|
83 | 65 |
|
84 | 66 |
|
| 67 | + |
| 68 | + |
85 | 69 | }
|
0 commit comments