Skip to content

Commit

Permalink
perf[event]: the event method name is changeless
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Mar 23, 2024
1 parent 9932e04 commit 92b8ee0
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 128 deletions.
13 changes: 0 additions & 13 deletions event/src/main/java/com/zfoo/event/EventExceptionContext.java

This file was deleted.

48 changes: 0 additions & 48 deletions event/src/main/java/com/zfoo/event/EventExceptionHandler.java

This file was deleted.

19 changes: 0 additions & 19 deletions event/src/main/java/com/zfoo/event/anno/EventService.java

This file was deleted.

21 changes: 4 additions & 17 deletions event/src/main/java/com/zfoo/event/enhance/EnhanceUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,11 @@ public static IEventReceiver createEventReceiver(EventReceiverDefinition definit
field.setModifiers(Modifier.PRIVATE + Modifier.FINAL);
enhanceClazz.addField(field);

// 定义类中的一个成员method
CtClass methodClass = classPool.get(method.getClass().getName());
CtField methodField = new CtField(methodClass, "method", enhanceClazz);
methodField.setModifiers(Modifier.PRIVATE + Modifier.FINAL);
enhanceClazz.addField(methodField);

// 定义类的构造器
// 创建构造函数参数数组
CtClass[] parameterTypes = {beanClass, methodClass};
CtClass[] parameterTypes = {beanClass};
CtConstructor constructor = new CtConstructor(parameterTypes, enhanceClazz);
constructor.setBody("{this.bean=$1; this.method=$2;}");
constructor.setBody("{this.bean=$1;}");
constructor.setModifiers(Modifier.PUBLIC);
enhanceClazz.addConstructor(constructor);

Expand All @@ -98,18 +92,11 @@ public static IEventReceiver createEventReceiver(EventReceiverDefinition definit
beanMethod.setBody(beanMethodBody);
enhanceClazz.addMethod(beanMethod);

// 定义类实现的接口方法getMethod
CtMethod getMethod = new CtMethod(classPool.get(Method.class.getName()), "getMethod", null, enhanceClazz);
getMethod.setModifiers(Modifier.PUBLIC + Modifier.FINAL);
String getMethodBody = "{ return this.method; }";
getMethod.setBody(getMethodBody);
enhanceClazz.addMethod(getMethod);

// 释放缓存
enhanceClazz.detach();

Class<?> resultClazz = enhanceClazz.toClass(IEventReceiver.class);
Constructor<?> resultConstructor = resultClazz.getConstructor(bean.getClass(), method.getClass());
return (IEventReceiver) resultConstructor.newInstance(bean, method);
Constructor<?> resultConstructor = resultClazz.getConstructor(bean.getClass());
return (IEventReceiver) resultConstructor.newInstance(bean);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,4 @@ public interface IEventReceiver {
void invoke(IEvent event);

Object getBean();

Method getMethod();
}
25 changes: 11 additions & 14 deletions event/src/main/java/com/zfoo/event/manager/EventBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
*/
package com.zfoo.event.manager;

import com.zfoo.event.EventExceptionContext;
import com.zfoo.event.EventExceptionHandler;
import com.zfoo.event.enhance.IEventReceiver;
import com.zfoo.event.model.IEvent;
import com.zfoo.protocol.collection.CollectionUtils;
Expand All @@ -35,6 +33,8 @@
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;

/**
* @author godotg
Expand All @@ -59,7 +59,8 @@ public abstract class EventBus {
/**
* event exception handler
*/
private static EventExceptionHandler exceptionHandler = EventExceptionHandler.LoggingEventExceptionHandler.INSTANCE;
public static BiConsumer<IEventReceiver, IEvent> exceptionHandler = null;
public static Consumer<IEvent> noEventReceiverHandler = null;

static {
for (int i = 0; i < executors.length; i++) {
Expand Down Expand Up @@ -93,15 +94,6 @@ public Thread newThread(Runnable runnable) {
}
}

/**
* 设置异常处理器
*
* @param handler 异常处理器
*/
public static void setExceptionHandler(EventExceptionHandler handler) {
exceptionHandler = handler;
}

/**
* Publish the event
*
Expand All @@ -114,7 +106,9 @@ public static void post(IEvent event) {
var clazz = event.getClass();
var receivers = receiverMap.get(clazz);
if (CollectionUtils.isEmpty(receivers)) {
logger.warn("This event [" + clazz.getName() + "] has not registered any valid event handler");
if (noEventReceiverHandler != null) {
noEventReceiverHandler.accept(event);
}
return;
}
for (var receiver : receivers) {
Expand All @@ -130,7 +124,10 @@ private static void doReceiver(IEventReceiver receiver, IEvent event) {
try {
receiver.invoke(event);
} catch (Throwable t) {
exceptionHandler.handleException(t.getCause(), new EventExceptionContext(event, receiver.getBean(), receiver.getMethod()));
logger.error("eventBus {} [{}] unknown error", receiver.bus(), event.getClass().getSimpleName(), t);
if (exceptionHandler != null) {
exceptionHandler.accept(receiver, event);
}
}
}

Expand Down
2 changes: 0 additions & 2 deletions event/src/test/java/com/zfoo/event/ApplicationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public void startEventTest() {
// see receiver method of MyController1 and MyController2
EventBus.post(MyNoticeEvent.valueOf("我的事件"));

EventBus.post(new UnhandledEvent());

ThreadUtils.sleep(1000);
}

Expand Down
5 changes: 2 additions & 3 deletions event/src/test/java/com/zfoo/event/MyController1.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
package com.zfoo.event;

import com.zfoo.event.anno.EventReceiver;
import com.zfoo.event.anno.EventService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
* @author godotg
*/
@EventService
@Component
public class MyController1 {

private static final Logger logger = LoggerFactory.getLogger(MyController1.class);
Expand All @@ -30,7 +30,6 @@ public class MyController1 {
@EventReceiver
public void onMyNoticeEvent(MyNoticeEvent event) {
logger.info("方法1同步执行事件:" + event.getMessage());
throw new NullPointerException();
}

}
4 changes: 2 additions & 2 deletions event/src/test/java/com/zfoo/event/MyController2.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@

import com.zfoo.event.anno.Bus;
import com.zfoo.event.anno.EventReceiver;
import com.zfoo.event.anno.EventService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
* @author godotg
*/
@EventService
@Component
public class MyController2 {

private static final Logger logger = LoggerFactory.getLogger(MyController2.class);
Expand Down
4 changes: 2 additions & 2 deletions event/src/test/java/com/zfoo/event/MyController3.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@

import com.zfoo.event.anno.Bus;
import com.zfoo.event.anno.EventReceiver;
import com.zfoo.event.anno.EventService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
* @author godotg
*/
@EventService
@Component
public class MyController3 {

private static final Logger logger = LoggerFactory.getLogger(MyController3.class);
Expand Down
6 changes: 0 additions & 6 deletions event/src/test/java/com/zfoo/event/UnhandledEvent.java

This file was deleted.

0 comments on commit 92b8ee0

Please sign in to comment.