perf[event]: post exception whatever if the exceptionFunction is override or not

This commit is contained in:
godotg
2024-06-12 12:38:27 +08:00
parent 837b6d8e9d
commit f7e6e51edd
2 changed files with 31 additions and 10 deletions
@@ -15,6 +15,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.event.model.TripleConsumer;
import com.zfoo.protocol.collection.CollectionUtils;
import com.zfoo.protocol.collection.concurrent.CopyOnWriteHashMapLongObject;
import com.zfoo.protocol.util.AssertionUtils;
@@ -60,10 +61,7 @@ public abstract class EventBus {
/**
* event exception handler
*/
public static TriConsumer<IEventReceiver, IEvent, Throwable> exceptionFunction = (receiver, event, throwable) -> {
logger.error("bean:[{}] event:[{}] unhandled exception", receiver.getBean().getClass().getSimpleName(), event.getClass().getSimpleName(), throwable);
post(new ExceptionEvent(receiver, event, throwable));
};
public static TripleConsumer<IEventReceiver, IEvent, Throwable> exceptionFunction = null;
/**
* event noReceiver handler
*/
@@ -129,15 +127,15 @@ public abstract class EventBus {
try {
receiver.invoke(event);
} catch (Throwable t) {
exceptionFunction.accept(receiver, event, t);
if (exceptionFunction == null) {
logger.error("bean:[{}] event:[{}] unhandled exception", receiver.getBean().getClass().getSimpleName(), event.getClass().getSimpleName(), t);
} else {
exceptionFunction.accept(receiver, event, t);
}
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,23 @@
/*
* 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;
/**
* @author godotg
*/
@FunctionalInterface
public interface TripleConsumer<L, M, R> {
void accept(L left, M middle, R right);
}