@@ -22,6 +22,8 @@ public class EventExecutor {
2222
2323 private Thread .UncaughtExceptionHandler exceptionHandler = null ;
2424
25+ private EventUncaughtExceptionHandler eventExceptionHandler = null ;
26+
2527 /**
2628 * 构造一个EventExecutor.
2729 * @param threadPoolExecutor 事件线程池, 线程池将用于执行Handler中的EventMethod.
@@ -45,6 +47,15 @@ public EventExecutor(ThreadPoolExecutor threadPoolExecutor,
4547 Thread newThread = threadFactory .newThread (r );
4648 if (newThread .getUncaughtExceptionHandler () == newThread .getThreadGroup ()){
4749 newThread .setUncaughtExceptionHandler ((t , e ) -> {
50+ if (e instanceof EventInvokeException && eventExceptionHandler != null ){
51+ EventInvokeException exception = (EventInvokeException ) e ;
52+ eventExceptionHandler .exceptionHandler (
53+ exception .getHandler (),
54+ exception .getHandlerMethod (),
55+ exception .getEventObject (),
56+ exception .getCause ());
57+ return ;
58+ }
4859 if (this .exceptionHandler != null ){
4960 this .exceptionHandler .uncaughtException (t , e );
5061 }
@@ -88,15 +99,7 @@ public void executor(final EventObject eventObject){
8899 }
89100 eventHandlerMethod .forEach (method -> {
90101 final Set <EventHandler > handlerSet = eventHandlerObjectMap .getHandlerObject (method .getDeclaringClass ());
91- threadPoolExecutor .execute (() -> handlerSet .forEach (handler -> {
92- try {
93- method .invoke (handler , eventObject );
94- } catch (IllegalAccessException e ) {
95- throw new RuntimeException (e );
96- } catch (InvocationTargetException e ){
97- throw new RuntimeException (e .getCause () == null ? e : e .getCause ());
98- }
99- }));
102+ threadPoolExecutor .execute (() -> handlerSet .forEach (handler -> executeEvent (handler , eventObject , method )));
100103 });
101104 }
102105
@@ -122,20 +125,24 @@ public int executor(EventHandler handler, EventObject eventObject){
122125 continue ;
123126 }
124127
125- threadPoolExecutor .execute (() -> {
126- try {
127- method .invoke (handler , eventObject );
128- } catch (IllegalAccessException e ) {
129- throw new RuntimeException (e );
130- } catch (InvocationTargetException e ){
131- throw new RuntimeException (e .getCause () == null ? e : e .getCause ());
132- }
133- });
128+ executeEvent (handler , eventObject , method );
134129 invokeCount ++;
135130 }
136131 return invokeCount ;
137132 }
138133
134+ private void executeEvent (EventHandler handler , EventObject event , Method eventMethod ){
135+ threadPoolExecutor .execute (() -> {
136+ try {
137+ eventMethod .invoke (handler , event );
138+ } catch (IllegalAccessException e ) {
139+ throw new RuntimeException (e );
140+ } catch (InvocationTargetException e ){
141+ throw new EventInvokeException (handler , eventMethod , event , e .getCause ());
142+ }
143+ });
144+ }
145+
139146 /**
140147 * 等待线程池内任务执行完成并关闭线程池.
141148 * @param timeout 时间
@@ -149,8 +156,12 @@ public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedE
149156
150157 /**
151158 * 设置线程池异常处理类.
159+ *
152160 * EventExecutor在内部经过处理,
153161 * 可以动态更改UncaughtExceptionHandler而不用担心设置后需要等线程重新建立后才生效.
162+ * 注意: 如需捕获EventHandler方法抛出的异常请使用{@link #setEventUncaughtExceptionHandler(EventUncaughtExceptionHandler)},
163+ * 设置捕获EventHandler抛出的异常, 因EventExecutor内部处理,
164+ * UncaughtExceptionHandler无法捕获{@link InvocationTargetException}异常来获取事件方法抛出的异常.
154165 * @param handler 处理类对象
155166 */
156167 public void setUncaughtExceptionHandler (Thread .UncaughtExceptionHandler handler ){
@@ -187,6 +198,32 @@ public List<Runnable> shutdown(boolean shutdownNow){
187198 }
188199 }
189200
201+ /**
202+ * 设置事件异常捕获处理对象.
203+ * 该对象能详细获得
204+ * @param handler 事件异常捕获处理对象
205+ */
206+ public void setEventUncaughtExceptionHandler (EventUncaughtExceptionHandler handler ){
207+ this .eventExceptionHandler = handler ;
208+ }
209+
210+ /**
211+ * 事件异常处理接口
212+ */
213+ @ FunctionalInterface
214+ public interface EventUncaughtExceptionHandler {
215+
216+ /**
217+ * 当事件对象抛出异常时触发.
218+ * @param handler 事件处理方法所在{@link EventHandler}
219+ * @param handlerMethod 抛出异常的方法对象
220+ * @param event 事件对象
221+ * @param cause 异常对象
222+ */
223+ void exceptionHandler (EventHandler handler , Method handlerMethod , EventObject event , Throwable cause );
224+ }
225+
226+
190227 @ Override
191228 protected void finalize () {
192229 threadPoolExecutor .shutdownNow ();
0 commit comments