• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

详解SpringBoot 发布ApplicationEventPublisher和监听ApplicationEvent事件

springboot 搞代码 4年前 (2022-01-05) 34次浏览 已收录 0个评论

这篇文章主要介绍了详解SpringBoot 发布ApplicationEvent来源[email protected]搞@^&代*@码网Publisher和监听ApplicationEvent事件,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

资料地址

Spring @Aync

实现方法

  1. 自定义需要发布的事件类,需要继承ApplicationEvent类或PayloadApplicationEvent(该类也仅仅是对ApplicationEvent的一层封装)
  2. 使用@EventListener来监听事件
  3. 使用ApplicationEventPublisher来发布自定义事件(@Autowired注入即可)
 /** * 自定义保存事件 * @author peter * 2019/1/27 14:59 */ public class PersonSaveEvent extends ApplicationEvent { private DATA data; public PersonSaveEvent(DATA source) { super(source); this.data = source; } public DATA getData() { return data; } } //发布事件 public void savePerson(Person person){ personDao.save(person); publisher.publishEvent(new PersonSaveEvent(1)); } //监听事件 @EventListener public void listenEvent(PersonSaveEvent event) { System.out.println("监听到PersonSaveEvent事件; 接收到的值:" + event.getData() + ";发布的时间为" + Instant.ofEpochMilli(event.getTimestamp())); }

好处

可以使核心业务与子业务进行解耦,也方便后期的业务的扩展。如新用户注册之后,需要发放优惠券,此时可以在保存用户之后,发布一个新用户的注册成功事件,通过监听该事件来实现发放优惠券的功能。后期新增一个对新用户进行xxx功能,此时可以新写一个监听注册成功事件的监听器,来处理新的业务逻辑,而不需要修改之前的注册逻辑。

注意事项

1、监听器方法中一定要try-catch异常,否则会造成发布事件(有事物的)的方法进行回滚
2、可以使用@Order注解来控制多个监听器的执行顺序,@Order传入的值越小,执行顺序越高
3、对于需要进行事物监听或不想try-catch runtime异常,可以使用@TransactionalEventListener注解

@TransactionalEventListener 监听器

在该注解的源码中:

 * <p>If the event is not published within the boundaries of a managed transaction, the * event is discarded unless the {@link #fallbackExecution} flag is explicitly set. If a * transaction is running, the event is processed according to its {@code TransactionPhase}. 

大意是:如果事件的发布不是在事物(@Transactional)范围内,则监听不到该事件,除非将fallbackExecution标志设置为true(@TransactionalEventListener(fallbackExecution = true));如果在事物中,可以选择在事物的哪个阶段来监听事件,默认在事物提交后监听。

修改监听事物的范围:@TransactionalEventListener(phase = TransactionPhase.AFTER_COMPLETION)

在监听器中重新开一个事物

 @TransactionalEventListener(phase = TransactionPhase.AFTER_COMPLETION) public void listenEvent1(PersonSaveEvent event) { divide(event); } @Transactional(propagation = Propagation.REQUIRES_NEW) public void divide(PersonSaveEvent event) { System.out.println("监听到PersonSaveEvent事件; 接收到的值:" + event.getData() + ";接受的时间为" + Instant.ofEpochMilli(event.getTimestamp())); } 

以上事件都是同步,如果需要异步则需要开启异步支持,在监听器方法加上@Async 注解即可。

 /** * @author peter * @version 1.0 * @date 2019/04/18 08:47 */ @Configuration @EnableAsync public class AsyncEventConfiguration implements AsyncConfigurer { @Override public Executor getAsyncExecutor() { return Executors.newCachedThreadPool(); } } 

一旦开始异步执行,方法的异常将不会抛出,只能在方法内部处理。如需在方法外处理异常:Async 异常处理在文章最后

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网

以上就是详解SpringBoot 发布ApplicationEventPublisher和监听ApplicationEvent事件的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:详解SpringBoot 发布ApplicationEventPublisher和监听ApplicationEvent事件

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址