mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-19 13:27:10 +00:00
Merge pull request #110 from LucaLq/main
feat[event]: support exception handle for event
This commit is contained in:
@@ -59,7 +59,9 @@ public abstract class EventBus {
|
||||
/**
|
||||
* event exception handler
|
||||
*/
|
||||
public static BiConsumer<IEventReceiver, IEvent> exceptionFunction = (receiver, event) -> {};
|
||||
public static TriConsumer<IEventReceiver, IEvent, Throwable> exceptionFunction = (receiver, event, throwable) -> {
|
||||
event.exceptionHandle(receiver, throwable);
|
||||
};
|
||||
public static Consumer<IEvent> noReceiverFunction = event -> {};
|
||||
|
||||
static {
|
||||
@@ -122,11 +124,15 @@ public abstract class EventBus {
|
||||
try {
|
||||
receiver.invoke(event);
|
||||
} catch (Throwable t) {
|
||||
logger.error("eventBus {} [{}] unknown error", receiver.bus(), event.getClass().getSimpleName(), t);
|
||||
exceptionFunction.accept(receiver, event);
|
||||
exceptionFunction.accept(receiver, event, t);
|
||||
}
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface TriConsumer<T, U, V> {
|
||||
void accept(T t, U u, V v);
|
||||
}
|
||||
|
||||
public static void asyncExecute(Runnable runnable) {
|
||||
asyncExecute(RandomUtils.randomInt(), runnable);
|
||||
}
|
||||
|
||||
@@ -12,12 +12,16 @@
|
||||
|
||||
package com.zfoo.event.model;
|
||||
|
||||
import com.zfoo.event.enhance.IEventReceiver;
|
||||
import com.zfoo.protocol.util.RandomUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
*/
|
||||
public interface IEvent {
|
||||
Logger logger = LoggerFactory.getLogger(IEvent.class);
|
||||
|
||||
/**
|
||||
* 这个返回的是一个用于确定事件在EventBus中的哪个线程池的执行的一个参数,只有异步事件才会有作用
|
||||
@@ -39,4 +43,12 @@ public interface IEvent {
|
||||
return RandomUtils.randomInt();
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理事件的异常
|
||||
* @param receiver IEventReceiver
|
||||
* @param throwable Throwable
|
||||
*/
|
||||
default void exceptionHandle(IEventReceiver receiver, Throwable throwable) {
|
||||
logger.error("eventBus {} [{}] unknown error", receiver.bus(), this.getClass().getSimpleName(), throwable);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user