Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions EventBus/src/org/greenrobot/eventbus/util/AsyncExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,18 @@
import java.util.logging.Level;

/**
* Executes an {@link RunnableEx} using a thread pool. Thrown exceptions are propagated by posting failure events of any
* given type (default is {@link ThrowableFailureEvent}).
*
* @author Markus
* Executes an {@link RunnableEx} using a thread pool. Thrown exceptions are propagated by posting failure events.
* By default, uses {@link ThrowableFailureEvent}.
* <p>
* Set a custom event type using {@link Builder#failureEventType(Class)}.
* The failure event class must have a constructor with one parameter of type {@link Throwable}.
* If using ProGuard or R8 make sure the constructor of the failure event class is kept, it is accessed via reflection.
* E.g. add a rule like
* <pre>
* -keepclassmembers class com.example.CustomThrowableFailureEvent {
* &lt;init&gt;(java.lang.Throwable);
* }
* </pre>
*/
public class AsyncExecutor {

Expand Down Expand Up @@ -103,24 +111,21 @@ private AsyncExecutor(Executor threadPool, EventBus eventBus, Class<?> failureEv

/** Posts an failure event if the given {@link RunnableEx} throws an Exception. */
public void execute(final RunnableEx runnable) {
threadPool.execute(new Runnable() {
@Override
public void run() {
threadPool.execute(() -> {
try {
runnable.run();
} catch (Exception e) {
Object event;
try {
runnable.run();
} catch (Exception e) {
Object event;
try {
event = failureEventConstructor.newInstance(e);
} catch (Exception e1) {
eventBus.getLogger().log(Level.SEVERE, "Original exception:", e);
throw new RuntimeException("Could not create failure event", e1);
}
if (event instanceof HasExecutionScope) {
((HasExecutionScope) event).setExecutionScope(scope);
}
eventBus.post(event);
event = failureEventConstructor.newInstance(e);
} catch (Exception e1) {
eventBus.getLogger().log(Level.SEVERE, "Original exception:", e);
throw new RuntimeException("Could not create failure event", e1);
}
if (event instanceof HasExecutionScope) {
((HasExecutionScope) event).setExecutionScope(scope);
}
eventBus.post(event);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

/**
* A generic failure event, which can be used by apps to propagate thrown exceptions.
* Used as default failure event by {@link AsyncExecutor}.
*/
public class ThrowableFailureEvent implements HasExecutionScope {
protected final Throwable throwable;
Expand Down
5 changes: 5 additions & 0 deletions EventBusPerformance/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ android {
}
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
5 changes: 3 additions & 2 deletions eventbus-android/consumer-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }

# And if you use AsyncExecutor:
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
# If using AsyncExecutord, keep required constructor of default event used.
# Adjust the class name if a custom failure event type is used.
-keepclassmembers class org.greenrobot.eventbus.util.ThrowableFailureEvent {
<init>(java.lang.Throwable);
}

Expand Down