Skip to content

Commit

Permalink
perf[event]: function style handler
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Apr 14, 2024
1 parent 76c20ff commit 1ee869a
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions event/src/main/java/com/zfoo/event/manager/EventBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public abstract class EventBus {
/**
* event exception handler
*/
public static BiConsumer<IEventReceiver, IEvent> exceptionHandler = null;
public static Consumer<IEvent> noEventReceiverHandler = null;
public static BiConsumer<IEventReceiver, IEvent> exceptionFunction = (receiver, event) -> {};
public static Consumer<IEvent> noReceiverFunction = event -> {};

static {
for (int i = 0; i < executors.length; i++) {
Expand Down Expand Up @@ -106,9 +106,7 @@ public static void post(IEvent event) {
var clazz = event.getClass();
var receivers = receiverMap.get(clazz);
if (CollectionUtils.isEmpty(receivers)) {
if (noEventReceiverHandler != null) {
noEventReceiverHandler.accept(event);
}
noReceiverFunction.accept(event);
return;
}
for (var receiver : receivers) {
Expand All @@ -125,9 +123,7 @@ private static void doReceiver(IEventReceiver receiver, IEvent event) {
receiver.invoke(event);
} catch (Throwable t) {
logger.error("eventBus {} [{}] unknown error", receiver.bus(), event.getClass().getSimpleName(), t);
if (exceptionHandler != null) {
exceptionHandler.accept(receiver, event);
}
exceptionFunction.accept(receiver, event);
}
}

Expand Down

0 comments on commit 1ee869a

Please sign in to comment.