前言
JFinal-event 是参考 Spring 中 Event 的使用方式而生,为 JFinal 用户带来更多方便。其核心目标是深层次解耦,为您节约更多时间,去陪恋人、家人和朋友 :)
距 gitee issues #IR96V 6个月,终于把【3.0优化】有生之年闲下来了弄吧的第二点优化进行了完善,鼓掌!!!
更新记录
2019-07-19 v3.0.0
更新说明
此次更新主要是采用 Annotation Processor 技术,将运行期的事件类扫描改到了编译期,
加快服务的启动时间,减少各种容器差异导致的类扫描问题。
使用
maven
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>JFinal-event</artifactId>
<version>3.0.0</version>
</dependency>
gradle >= 5.x
api("net.dreamlu:JFinal-event:3.0.0")
annotationProcessor("net.dreamlu:JFinal-event:3.0.0")
gradle < 5.x
compile("net.dreamlu:JFinal-event:3.0.0")
注意
-
3.0.0 由于使用了 Annotation Processor 技术,Idea 需要开启注解处理器。
-
对于手动导入 jar (非 maven、gradle)依赖管理的,您需要手动注入事件类或者在 classes 目录下新建 META-INF/dream.events 文件,格式如下:
net.dreamlu.event.test.Test1Listener
net.dreamlu.event.test.Test2Listener
启动插件(v3.0.0 去掉了类扫描更加简单)
// 初始化插件
EventPlugin plugin = new EventPlugin();
// 设置为异步,默认同步,或者使用`threadPool(ExecutorService executorService)`自定义线程池。
plugin.async();
// 手动启动插件,用于main方法启动,jfinal中不需要,添加插件即可。
plugin.start();
// 停止插件,用于main方法测试,jfinal中不需要,添加插件即可。
plugin.stop();
新建事件类
public class AccountEvent {
private Integer id;
private String name;
private Integer age;
// 省略 get set
}
编写监听方法
public class Test1Listener {
@EventListener
public void listenTest1Event(AccountEvent event) {
System.out.println("AccountEvent:" + event);
}
}
发送事件
AccountEvent event = new AccountEvent();
event.setId(1);
event.setName("张三");
event.setAge(18);
EventKit.post(event);
@EventListener注解说明
示例
@EventListener(events = Test1Event.class, order = 1, async = true, condition = "event.isExec()")
注解说明
public class Test {
@EventListener({Test1Event.class, Test2Event.class})
public void applicationEvent(ApplicationEvent event) {
String xx = (String) event.getSource();
System.out.println(Thread.currentThread().getName() + "\tsource:" + xx);
}
}
-
order 排序,数值越小越先执行,默认为Integer.MAX_VALUE
-
async 异步执行,需要插件开启async() 或者自定义线程池。
-
condition 表达式条件,使用event.xxxx,event.isExec() == true 判定event的属性或者方法。
回顾
或许这是最后的一个版本了,由于精力问题,笔者已经停止更新其他的几个 JFinal 插件,下面随我一起回顾下 JFinal-event 的这4年多。
-
2015-04-27 22:19:33 初始化项目 网名(孤独的√3)
-
2015-05-06 23:40:53 v0.1 版本发布推送到 maven 中央库
-
2015-06-25 21:01:13 v0.3 支持异步,去掉了 guava 包,因为只用了一个 Multimap 集合。
-
2015-07-04 17:43:23 v0.4.2 添加事件排序
-
2016-08-19 10:46:58 v1.4.0 添加事件 tag
-
2017-04-22 00:10:41 v1.5.1 添加了基于 rmi 的远程 Event(2.x弃用)
-
2017-10-10 11:23:34 v2.0.0 基于注解和方法的兼听,不再需要单独编写 Listener 类。
-
2018-03-02 17:05:33 v2.1.0 添加CtrlHolderEvent处理同步、异步中request、session、header等参数传递。网名(如梦技术)
-
2018-10-09 21:37:18 v2.2.2 升级到 jfinal 3.5 支持JFinal新版本的 bean inject。
-
2019-04-08 20:55:52 v2.3.0 支持普通类作为 event 事件源,不再需要继承 ApplicationEvent 。
感谢码云提供了这么好的一个平台作为码云提供了这么一个优秀的平台,也感谢 JFinal 从 JFinal 中学习到了不少极简设计。
最后
笔者近2年将更多的精力放到的我的新开源——Spring Cloud 微服务开发核心包 mica:https://gitee.com/596392912/mica 欢迎 star ,各种骚操作等你来发现。 |