ref[event]: spring like exception handler

This commit is contained in:
godotg
2024-06-09 15:55:48 +08:00
parent 3980af119c
commit 2d5dffa8e9
3 changed files with 66 additions and 18 deletions
@@ -13,6 +13,7 @@
package com.zfoo.event.manager;
import com.zfoo.event.enhance.IEventReceiver;
import com.zfoo.event.model.ExceptionEvent;
import com.zfoo.event.model.IEvent;
import com.zfoo.protocol.collection.CollectionUtils;
import com.zfoo.protocol.collection.concurrent.CopyOnWriteHashMapLongObject;
@@ -33,6 +34,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
/**
@@ -55,6 +57,11 @@ public abstract class EventBus {
* event mapping
*/
private static final Map<Class<? extends IEvent>, List<IEventReceiver>> receiverMap = new HashMap<>();
/**
* event exception handler
*/
public static BiConsumer<IEventReceiver, IEvent> exceptionFunction = (receiver, event) -> {};
/**
* event noReceiver handler
*/
@@ -120,15 +127,12 @@ public abstract class EventBus {
try {
receiver.invoke(event);
} catch (Throwable t) {
event.exceptionHandle(receiver, t);
logger.error("bean:[{}] event:[{}] unhandled exception", receiver.getBean().getClass().getSimpleName(), event.getClass().getSimpleName(), t);
exceptionFunction.accept(receiver, event);
post(new ExceptionEvent(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);
}
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2020 The zfoo Authors
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.event.model;
import com.zfoo.event.enhance.IEventReceiver;
/**
* @author godotg
*/
public class ExceptionEvent implements IEvent {
private IEventReceiver receiver;
private IEvent event;
private Throwable throwable;
public ExceptionEvent(IEventReceiver receiver, IEvent event, Throwable throwable) {
this.receiver = receiver;
this.event = event;
this.throwable = throwable;
}
public IEventReceiver getReceiver() {
return receiver;
}
public void setReceiver(IEventReceiver receiver) {
this.receiver = receiver;
}
public IEvent getEvent() {
return event;
}
public void setEvent(IEvent event) {
this.event = event;
}
public Throwable getThrowable() {
return throwable;
}
public void setThrowable(Throwable throwable) {
this.throwable = throwable;
}
}
@@ -12,16 +12,12 @@
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中的哪个线程池的执行的一个参数,只有异步事件才会有作用
@@ -43,12 +39,4 @@ public interface IEvent {
return RandomUtils.randomInt();
}
/**
* 处理事件的异常
* @param receiver IEventReceiver
* @param throwable Throwable
*/
default void exceptionHandle(IEventReceiver receiver, Throwable throwable) {
logger.error("bean:[{}] event:[{}] unhandled exception", receiver.getBean().getClass().getSimpleName(), this.getClass().getSimpleName(), throwable);
}
}