欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring的事件机制知识点详解及实例分析

程序员文章站 2022-06-19 17:53:14
同步事件和异步事件 同步事件: 在一个线程里,按顺序执行业务,做完一件事再去做下一件事。 异步事件: 在一个线程里,做一个事的同事,可以另起一个新的线程执行另一件事,这样两件事可以...

同步事件和异步事件

  • 同步事件: 在一个线程里,按顺序执行业务,做完一件事再去做下一件事。
  • 异步事件: 在一个线程里,做一个事的同事,可以另起一个新的线程执行另一件事,这样两件事可以同时执行。

用一个例子来解释同步事件和异步事件的使用场景,有时候一段完整的代码逻辑,可能分为几部分,拿最常见的注册来说,假设完整流程是,1.点击注册->2.检验信息并存库->3.发送邮件通知->4.返回给用户.代码这么写是正确,但不是最好的,缺点如下:

逻辑复杂,业务耦合,我们把校验数据并存库和发送邮件写到一个大的业务方法里了,发送邮件我们可以看做一个相对独立的业务方法。
效率低,假设2和3分别需要1秒的时候,那么用户在点击注册2秒后才能看到响应。

同步事件可以解决上面第一个问题,我们把发邮件的方法独立出来,放到事件里执行,这样注册的这个方法就可以只做2操作,完成之后发布一个事件去执行3,可以很好的解决业务耦合的问题.

异步事件可以完美解决以上两个问题,注册方法执行2操作,执行之后发布一个异步事件,另起一个线程执行3操作,注册方法所在的线程可直接返回给用户,这样不仅实现了业务解耦还提高了效率,用户点击注册,1秒后就能看到响应.

spring的事件机制

spring 事件发送监听涉及3个部分

  • applicationevent:表示事件本身,自定义事件需要继承该类,可以用来传递数据,比如上述操作,我们需要将用户的邮箱地址传给事件监听器.
  • applicationeventpublisheraware :事件发送器,通过实现这个接口,来触发事件.  
  • applicationlistener:事件监听器接口,事件的业务逻辑封装在监听器里面.

接下来使用spring的异步事件机制来模拟上面的注册流程.有配置文件和注解两种方式。

使用配置文件的方式创建事件:

新建testevent:

public class testevent extends applicationevent {
 
     private testparam source;
 
     public testevent(testparam source) {
         super(source);
         this.source = source;
     }
 }
 
 @data
 public class testparam {
     private string email;
 }

新建testlistener:

@component
 public class testlistener implements applicationlistener<testevent> {
 
     @override
     public void onapplicationevent(testevent testevent) {
 
         testparam param = (testparam) testevent.getsource();
         system.out.println(".......开始.......");
         system.out.println("发送邮件:"+param.getemail());
         system.out.println(".......结束.....");
     }
 }

新建 eventpublisher:

@component
public class testpublish implements applicationeventpublisheraware {

    private static applicationeventpublisher applicationeventpublisher;

    @override
    public void setapplicationeventpublisher(applicationeventpublisher applicationeventpublisher) {
        testpublish.applicationeventpublisher = applicationeventpublisher;
    }

    public static void  publishevent(applicationevent communityarticleevent) {
        applicationeventpublisher.publishevent(communityarticleevent);
    }
}

spring-context.xml中添加:

  <bean id="applicationeventasyncmulticaster" class="org.springframework.context.event.simpleapplicationeventmulticaster">
         <property name="taskexecutor">
             <bean class="org.springframework.scheduling.concurrent.threadpooltaskexecutor">
                 <property name="corepoolsize" value="5"/>
                 <property name="keepaliveseconds" value="3000"/>
                 <property name="maxpoolsize" value="50"/>
                 <property name="queuecapacity" value="200"/>
             </bean>
         </property>
     </bean>

注意:如果加<propery name="taskexecutor",则使用异步方式执行,否则为同步方式

使用注解方式创建事件

使用 @async 需要在配置文件添加一下支持,线程池也是需要配置一下的

<!-- 开启@aspectj aop代理 -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>

    <!-- 任务执行器 -->
    <task:executor id="executor" pool-size="10"/>

    <!--开启注解调度支持 @async -->
    <task:annotation-driven executor="executor" proxy-target-class="true"/>

testlistener中在方法中添加@async

 @component
 public class testlistener implements applicationlistener<testevent> {
 
     @async
     @override
     public void onapplicationevent(testevent testevent) {
 
         testparam param = (testparam) testevent.getsource();
         system.out.println(".......开始.......");
         system.out.println("发送邮件:"+param.getemail());
         system.out.println(".......结束.....");
     }
 }

listener其实还可以做得更彻底一点,使用注解@eventlistener可代替实现applicationlistener,原理是通过扫描这个注解来创建监听器并自动添加到applicationcontext中.

新建自定义eventhandler:

 @component
 public class testeventhandler {
 
     @async
     @eventlistener
     public void handletestevent(testevent testevent) {
 
         testparam param = (testparam) testevent.getsource();
         system.out.println(".......开始.......");
         system.out.println("发送邮件:"+param.getemail());
         system.out.println(".......结束.....");
     }
 } 

测试及控制台的打印就不贴了,这里主要记录一下具体的实现方法.

总结:

使用spring事件机制能很好地帮助我们消除不同业务间的耦合关系,也可以提高执行效率,应该根据业务场景灵活选择。

到此这篇关于spring的事件机制知识点详解及实例分析的文章就介绍到这了,更多相关spring的事件机制详解内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: Spring 事件机制