-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf[event]:Optimize event exception handling and unsubscribed event …
…handling
- Loading branch information
weixiaoqiang
committed
Mar 23, 2024
1 parent
8ade37b
commit b51fd4e
Showing
12 changed files
with
175 additions
and
18 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
event/src/main/java/com/zfoo/event/EventExceptionContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.zfoo.event; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* 订阅者抛出异常上下文 | ||
* | ||
* @param event 事件对象 | ||
* @param subscriber 订阅者 | ||
* @param subscriberMethod 订阅方法 | ||
*/ | ||
public record EventExceptionContext(Object event, Object subscriber, Method subscriberMethod) { | ||
} |
48 changes: 48 additions & 0 deletions
48
event/src/main/java/com/zfoo/event/EventExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.zfoo.event; | ||
|
||
import com.zfoo.event.manager.EventBus; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* 处理事件订阅者抛出异常 | ||
* | ||
* @author veione | ||
*/ | ||
@FunctionalInterface | ||
public interface EventExceptionHandler { | ||
/** | ||
* 处理订阅者抛出的异常 | ||
* | ||
* @param exception 异常对象 | ||
* @param context 异常上下文 | ||
*/ | ||
void handleException(Throwable exception, EventExceptionContext context); | ||
|
||
final class LoggingEventExceptionHandler implements EventExceptionHandler { | ||
public static final LoggingEventExceptionHandler INSTANCE = new LoggingEventExceptionHandler(); | ||
private final Logger logger = LoggerFactory.getLogger(EventBus.class.getName()); | ||
|
||
@Override | ||
public void handleException(Throwable exception, EventExceptionContext context) { | ||
if (logger.isErrorEnabled()) { | ||
logger.error(message(context), exception); | ||
} | ||
} | ||
|
||
private static String message(EventExceptionContext context) { | ||
Method method = context.subscriberMethod(); | ||
return "Exception thrown by subscriber method " | ||
+ method.getName() | ||
+ '(' | ||
+ method.getParameterTypes()[0].getName() | ||
+ ')' | ||
+ " on subscriber " | ||
+ context.subscriber() | ||
+ " when dispatching event: " | ||
+ context.event(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.zfoo.event.anno; | ||
|
||
import org.springframework.aot.hint.annotation.Reflective; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* 用于标记处理事件接口 | ||
* | ||
* @author veione | ||
*/ | ||
@Documented | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
@Reflective | ||
@Service | ||
public @interface EventService { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
event/src/main/java/com/zfoo/event/model/IPlayerEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.zfoo.event.model; | ||
|
||
/** | ||
* 玩家事件接口 | ||
* <p> | ||
* 主要用于让同一个玩家ID在同一个线程里面进行事件的业务处理,防止玩家事件在不同的线程中处理造成的玩家数据冲突或者线程竞争导致的性能下降. | ||
* </p> | ||
* | ||
* @author veione | ||
*/ | ||
public interface IPlayerEvent extends IEvent { | ||
|
||
@Override | ||
default int executorHash() { | ||
return (int) executePlayerId(); | ||
} | ||
|
||
/** | ||
* 执行玩家ID | ||
* | ||
* @return 玩家ID | ||
*/ | ||
long executePlayerId(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.zfoo.event; | ||
|
||
import com.zfoo.event.model.IEvent; | ||
|
||
public class UnhandledEvent implements IEvent { | ||
} |