spring 事件为bean 与 bean之间传递消息。一个bean处理完了希望其余一个接着处理.这时我们就需要其余的一个bean监听当前bean所发送的事件.

spring事件使用步骤如下:

1.先自定义事件:你的事件需要继承 ApplicationEvent

2.定义事件监听器: 需要实现 ApplicationListener

3.使用容器对事件进行发布

  • 首先定义一个事件
/**
* @Title:
* @Auther: hangyu
* @Date: 2019/3/13
* @Description
* @Version:1.0
*/
public class TestEvent extends ApplicationEvent { private String name; private String msg; public TestEvent(Object source){
super(source);
} public TestEvent(Object source, String name, String msg) {
super(source);
this.name = name;
this.msg = msg;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
}
}
  • 其次定义事件监听
/**
* @Title:
* @Auther: hangyu
* @Date: 2019/3/13
* @Description
* @Version:1.0
*/
@Component
public class TestEventListener implements ApplicationListener<TestEvent> { @Async
@Override
public void onApplicationEvent(TestEvent testEvent) {
System.out.println("姓名:"+testEvent.getName()+"得到消息:"+testEvent.getMsg());
}
}
  • 使用容器发布事件

/**
* @Title:
* @Auther: hangyu
* @Date: 2019/3/13
* @Description
* @Version:1.0
*/
@Component
public class TestEventPublisher { @Autowired
private ApplicationContext applicationContext; public void pushlish(String name, String msg){
applicationContext.publishEvent(new TestEvent(this, name,msg));
}
}
  • 测试事件是否能够生效
/**
* @Title:
* @Auther: hangyu
* @Date: 2019/3/13
* @Description
* @Version:1.0
*/
@Configuration
@ComponentScan("cn.*.event")
public class EventConfig {
} /**
* @Title:
* @Auther: hangyu
* @Date: 2019/3/13
* @Description
* @Version:1.0
*/
public class TestMain { private static AnnotationConfigApplicationContext context; public static void main(String[] args) {
start();
} private static void start() {
if (context == null) {
context=new AnnotationConfigApplicationContext(EventConfig.class);
}
context.getBean(TestEventPublisher.class).pushlish("hangyu","申请退款!");
}
}
 
输出打印结果

最后有一个思考:ApplicationEvent事件执行部分和起一个TaskExecutor去执行 有啥区别吗?反正都是异步。

可以这样实现;

 @Autowired
private AsyncExecutor asyncExecutor; Executor executor = asyncExecutor.getAsyncExecutor();
executor.execute(
//具体业务
}); @Configuration
@EnableAsync
public class AsyncExecutor implements AsyncConfigurer { // 日志
static final Logger logger = LoggerFactory.getLogger(AsyncExecutor.class); public Executor getAsyncExecutor() { ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); taskExecutor.setCorePoolSize(5); taskExecutor.setMaxPoolSize(10); taskExecutor.setQueueCapacity(20000); taskExecutor.setKeepAliveSeconds(120); taskExecutor.setAllowCoreThreadTimeOut(true); taskExecutor.initialize(); return taskExecutor;
}
}

还可以这样实现;

    <!-- 异步线程池 -->
<bean id="taskExecutor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<!-- 核心线程数 -->
<property name="corePoolSize" value="3" />
<!-- 最大线程数 -->
<property name="maxPoolSize" value="10" />
<!-- 队列最大长度 >=mainExecutor.maxSize -->
<property name="queueCapacity" value="25" />
<!-- 线程池维护线程所允许的空闲时间 -->
<property name="keepAliveSeconds" value="300" />
<!-- 线程池对拒绝任务(无线程可用)的处理策略 ThreadPoolExecutor.CallerRunsPolicy策略 ,调用者的线程会执行该任务,如果执行器已关闭,则丢弃. -->
<property name="rejectedExecutionHandler">
<bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />
</property>
</bean> @Resource
private TaskExecutor taskExecutor; taskExecutor.execute(new Runnable() {
@Override
public void run() {
//具体业务
}
}
 
这就是注解方式和直接引用方式,本质是一样的

我的思考:ApplicationEvent是观察者设计模式,这种设计模式使得主题和观察者之间的耦合度降低,松耦合是面向对象设计中很重要的一个原则,最终也是使用@Async来实现异步。而TaskExecutor则是启动一个线程池任务异步执行任务,两者效果一样,但原理不同。

通过我的思考,又带来一个疑问:那观察者模式是不是就是我们MQ中的发布订阅模式呢?只不过观察者模式是进程内的,而MQ是跨进程的?就这唯一的区别吗?

经过一些资料的查阅:大多数地方观察者模式约等于发布订阅模式,但是观察者模式是由具体目标调度的,而发布/订阅模式是统一由调度中心调的,所以观察者模式的订阅者与发布者之间是存在依赖的,而发布/订阅模式则不会。

所以说观察者模式是小米加步枪,发布订阅模式是95式自动步枪,是它的进化版!

作者:杭宇_8ba6
链接:https://www.jianshu.com/p/e03c5c53d2e9
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

最新文章

  1. Windows Phone开发需要了解的背景
  2. OAF_开发系列02_实现OAF页面的通过个性化多语言开发国际化(案例)
  3. 阿里云 OSS+CDN
  4. [Asp.net]Uploadify所有配置说明,常见bug问题分析
  5. Ubuntu快捷键
  6. 【转载】UVa 11464 Even Parity 偶数矩阵
  7. Javaweb实现的优优图书商城(含源码)
  8. iOS动画——弹窗动画(pop动画)
  9. deferred initcalls与模块化
  10. O2O、B2B、C2C(通俗讲解)
  11. POIUtils 导出 poi Test 100w 600w 条数据
  12. C++ Opencv 自写函数实现膨胀腐蚀处理
  13. Symantec Backup Exec 2010 安装报 bad ELF interpreter: No such file or directory
  14. [日志]SAP S/4 HANA 启动与关闭的顺序
  15. Angular 2基础(一) 环境搭建
  16. golang中的init函数以及main函数
  17. Spring-data-redis redis
  18. linux代码笔记
  19. if 判断文件
  20. SDN竞赛思考总结

热门文章

  1. 【2020.12.03提高组模拟】A组反思
  2. spring java config配置搭建工程资料收集(网文)
  3. PyQt学习随笔:QTableWidgetItem项的setSizeHint()方法的作用
  4. 第六篇 Scrum 冲刺博客
  5. C++异常之六 异常处理的基本思想
  6. 最新快手抖音短视频源码web+APP架设教程+完整数据
  7. Android全面解析之Window机制
  8. Java8新特性探索之新日期时间库
  9. oracle DG查看延时时间
  10. Kubernetes【K8S】(五):Service