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