|
23 | 23 | import java.util.logging.Level; |
24 | 24 |
|
25 | 25 | /** |
26 | | - * Executes an {@link RunnableEx} using a thread pool. Thrown exceptions are propagated by posting failure events of any |
27 | | - * given type (default is {@link ThrowableFailureEvent}). |
28 | | - * |
29 | | - * @author Markus |
| 26 | + * Executes an {@link RunnableEx} using a thread pool. Thrown exceptions are propagated by posting failure events. |
| 27 | + * By default, uses {@link ThrowableFailureEvent}. |
| 28 | + * <p> |
| 29 | + * Set a custom event type using {@link Builder#failureEventType(Class)}. |
| 30 | + * The failure event class must have a constructor with one parameter of type {@link Throwable}. |
| 31 | + * If using ProGuard or R8 make sure the constructor of the failure event class is kept, it is accessed via reflection. |
| 32 | + * E.g. add a rule like |
| 33 | + * <pre> |
| 34 | + * -keepclassmembers class com.example.CustomThrowableFailureEvent { |
| 35 | + * <init>(java.lang.Throwable); |
| 36 | + * } |
| 37 | + * </pre> |
30 | 38 | */ |
31 | 39 | public class AsyncExecutor { |
32 | 40 |
|
@@ -103,24 +111,21 @@ private AsyncExecutor(Executor threadPool, EventBus eventBus, Class<?> failureEv |
103 | 111 |
|
104 | 112 | /** Posts an failure event if the given {@link RunnableEx} throws an Exception. */ |
105 | 113 | public void execute(final RunnableEx runnable) { |
106 | | - threadPool.execute(new Runnable() { |
107 | | - @Override |
108 | | - public void run() { |
| 114 | + threadPool.execute(() -> { |
| 115 | + try { |
| 116 | + runnable.run(); |
| 117 | + } catch (Exception e) { |
| 118 | + Object event; |
109 | 119 | try { |
110 | | - runnable.run(); |
111 | | - } catch (Exception e) { |
112 | | - Object event; |
113 | | - try { |
114 | | - event = failureEventConstructor.newInstance(e); |
115 | | - } catch (Exception e1) { |
116 | | - eventBus.getLogger().log(Level.SEVERE, "Original exception:", e); |
117 | | - throw new RuntimeException("Could not create failure event", e1); |
118 | | - } |
119 | | - if (event instanceof HasExecutionScope) { |
120 | | - ((HasExecutionScope) event).setExecutionScope(scope); |
121 | | - } |
122 | | - eventBus.post(event); |
| 120 | + event = failureEventConstructor.newInstance(e); |
| 121 | + } catch (Exception e1) { |
| 122 | + eventBus.getLogger().log(Level.SEVERE, "Original exception:", e); |
| 123 | + throw new RuntimeException("Could not create failure event", e1); |
123 | 124 | } |
| 125 | + if (event instanceof HasExecutionScope) { |
| 126 | + ((HasExecutionScope) event).setExecutionScope(scope); |
| 127 | + } |
| 128 | + eventBus.post(event); |
124 | 129 | } |
125 | 130 | }); |
126 | 131 | } |
|
0 commit comments