mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-12 16:15:42 +00:00
docs: Translate the Chinese comments in the event module into English.
This commit is contained in:
1 parent
af4e65516c
commit
6a4e71b2f1
14 files changed
+48
-50
No files matched your search
@@ -50,7 +50,7 @@ public class EventContext implements ApplicationListener<ApplicationContextEvent
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationContextEvent event) {
|
||||
if (event instanceof ContextRefreshedEvent) {
|
||||
// 初始化上下文
|
||||
// Initialize context
|
||||
EventContext.instance = this;
|
||||
instance.applicationContext = event.getApplicationContext();
|
||||
} else if (event instanceof ContextClosedEvent) {
|
||||
|
||||
@@ -30,7 +30,7 @@ import java.lang.reflect.Modifier;
|
||||
public abstract class EnhanceUtils {
|
||||
|
||||
static {
|
||||
// 适配Tomcat,因为Tomcat不是用的默认的类加载器,而Javassist用的是默认的加载器
|
||||
// Adapt to Tomcat: Tomcat uses a custom class loader, while Javassist uses the default class loader
|
||||
var classArray = new Class<?>[]{
|
||||
IEventReceiver.class,
|
||||
IEvent.class
|
||||
@@ -53,46 +53,46 @@ public abstract class EnhanceUtils {
|
||||
Method method = definition.getMethod();
|
||||
Class<?> clazz = definition.getEventClazz();
|
||||
|
||||
// 定义类名称
|
||||
// Define the class name
|
||||
CtClass enhanceClazz = classPool.makeClass(EnhanceUtils.class.getName() + StringUtils.capitalize(NamespaceHandler.EVENT) + UuidUtils.getLocalIntId());
|
||||
enhanceClazz.addInterface(classPool.get(IEventReceiver.class.getName()));
|
||||
|
||||
// 定义类中的一个成员bean
|
||||
// Define a member field 'bean' in the class
|
||||
CtClass beanClass = classPool.get(bean.getClass().getName());
|
||||
CtField field = new CtField(beanClass, "bean", enhanceClazz);
|
||||
field.setModifiers(Modifier.PRIVATE + Modifier.FINAL);
|
||||
enhanceClazz.addField(field);
|
||||
|
||||
// 定义类的构造器
|
||||
// 创建构造函数参数数组
|
||||
// Define the class constructor
|
||||
// Create constructor parameter array
|
||||
CtClass[] parameterTypes = {beanClass};
|
||||
CtConstructor constructor = new CtConstructor(parameterTypes, enhanceClazz);
|
||||
constructor.setBody("{this.bean=$1;}");
|
||||
constructor.setModifiers(Modifier.PUBLIC);
|
||||
enhanceClazz.addConstructor(constructor);
|
||||
|
||||
// 定义类实现的接口方法invoker
|
||||
// Define the 'invoke' method from the interface
|
||||
CtMethod invokeMethod = new CtMethod(classPool.get(void.class.getName()), "invoke", classPool.get(new String[]{IEvent.class.getName()}), enhanceClazz);
|
||||
invokeMethod.setModifiers(Modifier.PUBLIC + Modifier.FINAL);
|
||||
String invokeMethodBody = StringUtils.format("{ this.bean.{}(({})$1); }", method.getName(), clazz.getName()); // 强制类型转换,转换为具体的Event类型的类型
|
||||
String invokeMethodBody = StringUtils.format("{ this.bean.{}(({})$1); }", method.getName(), clazz.getName()); // Cast to the specific Event type
|
||||
invokeMethod.setBody(invokeMethodBody);
|
||||
enhanceClazz.addMethod(invokeMethod);
|
||||
|
||||
// 定义类实现的接口方法bus
|
||||
// Define the 'bus' method from the interface
|
||||
CtMethod busMethod = new CtMethod(classPool.get(Bus.class.getName()), "bus", null, enhanceClazz);
|
||||
busMethod.setModifiers(Modifier.PUBLIC + Modifier.FINAL);
|
||||
String busMethodBody = StringUtils.format("{ return {}.{}; }", Bus.class.getName(), definition.getBus());
|
||||
busMethod.setBody(busMethodBody);
|
||||
enhanceClazz.addMethod(busMethod);
|
||||
|
||||
// 定义类实现的接口方法getBean
|
||||
// Define the 'getBean' method from the interface
|
||||
CtMethod beanMethod = new CtMethod(classPool.get(Object.class.getName()), "getBean", null, enhanceClazz);
|
||||
beanMethod.setModifiers(Modifier.PUBLIC + Modifier.FINAL);
|
||||
String beanMethodBody = "{ return this.bean; }";
|
||||
beanMethod.setBody(beanMethodBody);
|
||||
enhanceClazz.addMethod(beanMethod);
|
||||
|
||||
// 释放缓存
|
||||
// Detach from pool to release cached class definition
|
||||
enhanceClazz.detach();
|
||||
|
||||
Class<?> resultClazz = enhanceClazz.toClass(IEventReceiver.class);
|
||||
|
||||
@@ -19,22 +19,23 @@ import com.zfoo.protocol.util.ReflectionUtils;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* 动态代理被EventReceiver注解标注的方法,为了避免反射最终会用javassist字节码增强的方法去代理EventReceiverDefinition
|
||||
* Dynamic proxy for methods annotated with @EventReceiver. To avoid reflection overhead,
|
||||
* Javassist bytecode enhancement is used to proxy EventReceiverDefinition.
|
||||
*
|
||||
* @author godotg
|
||||
*/
|
||||
public class EventReceiverDefinition implements IEventReceiver {
|
||||
|
||||
// 观察者的bean
|
||||
// The observer's bean instance
|
||||
private Object bean;
|
||||
|
||||
// 被EventReceiver注解标注的方法
|
||||
// The method annotated with @EventReceiver
|
||||
private Method method;
|
||||
|
||||
// 事件接收方式
|
||||
// The event dispatching strategy
|
||||
private Bus bus;
|
||||
|
||||
// 接收的参数Class
|
||||
// The event parameter class type
|
||||
private Class<? extends IEvent> eventClazz;
|
||||
|
||||
public EventReceiverDefinition(Object bean, Method method, Bus bus, Class<? extends IEvent> eventClazz) {
|
||||
|
||||
@@ -45,7 +45,7 @@ public abstract class EventBus {
|
||||
|
||||
/**
|
||||
* EN: The size of the thread pool. Event's thread pool is often used to do time-consuming operations, so set it a little bigger
|
||||
* CN: 线程池的大小. event的线程池经常用来做一些耗时的操作,所以要设置大一点
|
||||
* Thread pool size. The event thread pool is often used for time-consuming operations, so it should be set larger.
|
||||
*/
|
||||
public static final int EXECUTORS_SIZE = MathUtil.safeFindNextPositivePowerOfTwo(Math.max(Runtime.getRuntime().availableProcessors(), 4) * 2);
|
||||
private static final int EXECUTOR_MASK = EXECUTORS_SIZE - 1;
|
||||
|
||||
@@ -16,9 +16,9 @@ import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.event.ApplicationContextEvent;
|
||||
|
||||
/**
|
||||
* 应用启动事件,这个使用spring自带的事件机制,自研的event事件仅用在业务逻辑
|
||||
* Application startup event. This uses Spring's built-in event mechanism; the custom event system is used only for business logic.
|
||||
* <p>
|
||||
* 启动顺序为:AppStartBeforeEvent -> AppStartEvent -> AppStartAfterEvent
|
||||
* Startup order: AppStartBeforeEvent -> AppStartEvent -> AppStartAfterEvent
|
||||
*
|
||||
* @author godotg
|
||||
*/
|
||||
|
||||
@@ -16,9 +16,9 @@ import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.event.ApplicationContextEvent;
|
||||
|
||||
/**
|
||||
* 应用启动事件,这个使用spring自带的事件机制,自研的event事件仅用在业务逻辑
|
||||
* Application startup event. This uses Spring's built-in event mechanism; the custom event system is used only for business logic.
|
||||
* <p>
|
||||
* 启动顺序为:AppStartBeforeEvent -> AppStartEvent -> AppStartAfterEvent
|
||||
* Startup order: AppStartBeforeEvent -> AppStartEvent -> AppStartAfterEvent
|
||||
*
|
||||
* @author godotg
|
||||
*/
|
||||
|
||||
@@ -16,9 +16,9 @@ import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.event.ApplicationContextEvent;
|
||||
|
||||
/**
|
||||
* 应用启动事件,这个使用spring自带的事件机制,自研的event事件仅用在业务逻辑
|
||||
* Application startup event. This uses Spring's built-in event mechanism; the custom event system is used only for business logic.
|
||||
* <p>
|
||||
* 启动顺序为:AppStartBeforeEvent -> AppStartEvent -> AppStartAfterEvent
|
||||
* Startup order: AppStartBeforeEvent -> AppStartEvent -> AppStartAfterEvent
|
||||
*
|
||||
* @author godotg
|
||||
*/
|
||||
|
||||
@@ -20,20 +20,17 @@ import com.zfoo.protocol.util.RandomUtils;
|
||||
public interface IEvent {
|
||||
|
||||
/**
|
||||
* 这个返回的是一个用于确定事件在EventBus中的哪个线程池的执行的一个参数,只有异步事件才会有作用
|
||||
* Returns a parameter used to determine which thread pool in EventBus the event should execute on. Only applies to async events.
|
||||
* <p>
|
||||
* 比如cpu是四核,那么EventBus中的executors线程池的数量为8个,通过取余可以确定异步事件在哪个线程池中执行。
|
||||
* 如果参数是0,0 % 8 = 0,那么异步事件最终会在executors[0]表示的线程池中执行
|
||||
* 如果参数是1,1 % 8 = 1,那么异步事件最终会在executors[1]表示的线程池中执行
|
||||
* 如果参数是8,8 % 8 = 0,那么异步事件最终会在executors[0]表示的线程池中执行
|
||||
* 如果参数是9,9 % 8 = 1,那么异步事件最终会在executors[1]表示的线程池中执行
|
||||
* <p>
|
||||
* 通过返回的参数,可以轻松控制异步事件在哪个线程池中去执行。
|
||||
* 因为EventBus中的线程池都是单线程线程池,如果将一些异步事件放在同一个线程池中执行,可以不用加锁,提高程序运行的效率。
|
||||
* <p>
|
||||
* 默认返回一个随机数,这就导致如果不重写这个方法,那么异步事件有可能会在EventBus中的任何一条线程池中去执行。
|
||||
*
|
||||
* @return 线程池的执行的一个参数
|
||||
* For example, if the CPU has 4 cores, EventBus creates 8 executor thread pools. Modulo determines the target pool.
|
||||
* If the value is 0: 0 % 8 = 0, the async event executes in executors[0].
|
||||
* If the value is 1: 1 % 8 = 1, the async event executes in executors[1].
|
||||
* If the value is 8: 8 % 8 = 0, the async event executes in executors[0].
|
||||
* If the value is 9: 9 % 8 = 1, the async event executes in executors[1].
|
||||
* By choosing the return value, you can control which thread pool handles the async event.
|
||||
* Since each pool is single-threaded, routing related events to the same pool avoids locking and improves throughput.
|
||||
* By default returns a random value, so async events may execute on any thread pool if this method is not overridden.
|
||||
* @return the parameter used to select the target thread pool
|
||||
*/
|
||||
default int executorHash() {
|
||||
return RandomUtils.randomInt();
|
||||
|
||||
@@ -32,13 +32,13 @@ public class EventDefinitionParser implements BeanDefinitionParser {
|
||||
String name;
|
||||
BeanDefinitionBuilder builder;
|
||||
|
||||
// 注册EventContext
|
||||
// Register EventContext
|
||||
clazz = EventContext.class;
|
||||
name = StringUtils.uncapitalize(clazz.getName());
|
||||
builder = BeanDefinitionBuilder.rootBeanDefinition(clazz);
|
||||
parserContext.getRegistry().registerBeanDefinition(name, builder.getBeanDefinition());
|
||||
|
||||
// 注册EventRegisterProcessor,event事件处理
|
||||
// Register EventRegisterProcessor for event handling
|
||||
clazz = EventRegisterProcessor.class;
|
||||
name = StringUtils.uncapitalize(clazz.getName());
|
||||
builder = BeanDefinitionBuilder.rootBeanDefinition(clazz);
|
||||
|
||||
@@ -30,8 +30,8 @@ import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
/**
|
||||
* 这是一个后置处理器,在boot项目中注册EventContext时,会import导入EventRegisterProcessor这个组件,这是一个后置处理器,
|
||||
* 断点发现 在AbstractAutowireCapableBeanFactory或调用getBeanPostProcessors,这样子每一个Bean创建后都会走postProcessAfterInitialization这个方法
|
||||
* A BeanPostProcessor registered via @Import when EventContext is set up in the boot module.
|
||||
* Debugging reveals that AbstractAutowireCapableBeanFactory calls getBeanPostProcessors, so postProcessAfterInitialization is invoked after every bean is created.
|
||||
*
|
||||
* @author godotg
|
||||
*/
|
||||
@@ -84,7 +84,7 @@ public class EventRegisterProcessor implements BeanPostProcessor {
|
||||
if (GraalVmUtils.isGraalVM()) {
|
||||
EventBus.registerEventReceiver(eventClazz, receiverDefinition);
|
||||
} else {
|
||||
// key:class类型 value:观察者 注册Event的receiverMap中
|
||||
// key: event class type, value: observer, registered in the receiverMap of EventBus
|
||||
EventBus.registerEventReceiver(eventClazz, EnhanceUtils.createEventReceiver(receiverDefinition));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,14 +23,14 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
*/
|
||||
public class ApplicationTest {
|
||||
|
||||
// Event Bus tutorial, the core idea is the observer pattern.(核心思想是设计模式中的观察者模式)
|
||||
// Event Bus tutorial. The core idea is the Observer design pattern.
|
||||
@Test
|
||||
public void startEventTest() {
|
||||
// load the configuration file which must import event.(加载配置文件,配置文件中必须引入event)
|
||||
// Load the configuration file (must import the event module)
|
||||
var context = new ClassPathXmlApplicationContext("application.xml");
|
||||
|
||||
// see receiver method of MyController1 and MyController2
|
||||
EventBus.post(MyNoticeEvent.valueOf("我的事件"));
|
||||
EventBus.post(MyNoticeEvent.valueOf("my-event"));
|
||||
|
||||
ThreadUtils.sleep(1000);
|
||||
}
|
||||
|
||||
@@ -26,10 +26,10 @@ public class MyController1 {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(MyController1.class);
|
||||
|
||||
// 事件会被当前线程立刻执行,注意日志打印的线程号
|
||||
// The event is executed immediately on the current thread; note the thread ID in the log
|
||||
@EventReceiver
|
||||
public void onMyNoticeEvent(MyNoticeEvent event) {
|
||||
logger.info("方法1同步执行事件:" + event.getMessage());
|
||||
logger.info("method1 sync event executed: " + event.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -28,12 +28,12 @@ public class MyController2 {
|
||||
private static final Logger logger = LoggerFactory.getLogger(MyController2.class);
|
||||
|
||||
/**
|
||||
* 同一个事件可以被重复注册和接受
|
||||
* 异步事件会被不会立刻执行,注意日志打印的线程号
|
||||
* The same event can be registered and received by multiple listeners
|
||||
* Async events are NOT executed immediately; note the thread ID in the log
|
||||
*/
|
||||
@EventReceiver(Bus.AsyncThread)
|
||||
public void onMyNoticeEvent(MyNoticeEvent event) {
|
||||
logger.info("方法2异步执行事件:" + event.getMessage());
|
||||
logger.info("method2 async event executed: " + event.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -29,7 +29,7 @@ public class MyController3 {
|
||||
|
||||
@EventReceiver(Bus.VirtualThread)
|
||||
public void onMyNoticeEvent(MyNoticeEvent event) {
|
||||
logger.info("方法3虚拟线程执行事件:" + event.getMessage());
|
||||
logger.info("method3 virtual-thread event executed: " + event.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user