同步事件:在一个线程里,按顺序执行业务,作完一件事再去作下一件事.spring
异步事件:在一个线程里,作一个事的同事,能够另起一个新的线程执行另外一件事,这样两件事能够同时执行.app
用一个例子来解释同步事件和异步事件的使用场景,有时候一段完整的代码逻辑,可能分为几部分,拿最多见的注册来讲,假设完整流程是,1.点击注册->2.检验信息并存库->3.发送邮件通知->4.返回给用户.代码这么写是正确,但不是最好的,缺点以下:异步
1.逻辑复杂,业务耦合,咱们把校验数据并存库和发送邮件写到一个大的业务方法里了,发邮件咱们能够看作一个相对独立的业务方法ide
2.效率低,假设2和3分别须要1秒的时候,那么用户在点击注册2秒后才能看到相应测试
同步事件能够解决上面第一个问题,咱们把发邮件的方法独立出来,放到事件里执行,这样注册的这个方法就能够只作2操做,完成以后发布一个事件去执行3,能够很好的解决业务耦合的问题.this
异步事件能够完美解决以上两个问题,注册方法执行2操做,执行以后发布一个异步事件,另起一个线程执行3操做,注册方法所在的线程可直接返回给用户,这样不只实现了业务解耦还提升了效率,用户点击注册,1秒后就能看到响应.spa
spring事件发送监听涉及3个部分线程
ApplicationEvent:表示事件自己,自定义事件须要继承该类,能够用来传递数据,好比上述操做,咱们须要将用户的邮箱地址传给事件监听器.
ApplicationEventPublisherAware:事件发送器,经过实现这个接口,来触发事件.
ApplicationListener:事件监听器接口,事件的业务逻辑封装在监听器里面.
接下来使用spring的异步事件机制来模拟上面的注册流程.有配置文件和注解两种方式.代理
1 public class TestEvent extends ApplicationEvent { 2 3 private TestParam source; 4 5 public TestEvent(TestParam source) { 6 super(source); 7 this.source = source; 8 } 9 } 10 11 @Data 12 public class TestParam { 13 private String email; 14 }
1 @Component 2 public class TestListener implements ApplicationListener<TestEvent> { 3 4 @Override 5 public void onApplicationEvent(TestEvent testEvent) { 6 7 TestParam param = (TestParam) testEvent.getSource(); 8 System.out.println(".......开始......."); 9 System.out.println("发送邮件:"+param.getEmail()); 10 System.out.println(".......结束....."); 11 } 12 }
@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); } }
1 <bean id="applicationEventAsyncMulticaster" class="org.springframework.context.event.SimpleApplicationEventMulticaster"> 2 <property name="taskExecutor"> 3 <bean class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> 4 <property name="corePoolSize" value="5"/> 5 <property name="keepAliveSeconds" value="3000"/> 6 <property name="maxPoolSize" value="50"/> 7 <property name="queueCapacity" value="200"/> 8 </bean> 9 </property> 10 </bean>
注意:若是加<propery name="taskExecutor",则使用异步方式执行,不然为同步方式code
使用@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
1 @Component 2 public class TestListener implements ApplicationListener<TestEvent> { 3 4 @Async 5 @Override 6 public void onApplicationEvent(TestEvent testEvent) { 7 8 TestParam param = (TestParam) testEvent.getSource(); 9 System.out.println(".......开始......."); 10 System.out.println("发送邮件:"+param.getEmail()); 11 System.out.println(".......结束....."); 12 } 13 }
Listener其实还能够作得更完全一点,使用注解@EventListener可代替实现ApplicationListener,原理是经过扫描这个注解来建立监听器并自动添加到ApplicationContext中.
1 @Component 2 public class TestEventHandler { 3 4 @Async 5 @EventListener 6 public void handleTestEvent(TestEvent testEvent) { 7 8 TestParam param = (TestParam) testEvent.getSource(); 9 System.out.println(".......开始......."); 10 System.out.println("发送邮件:"+param.getEmail()); 11 System.out.println(".......结束....."); 12 } 13 }
测试及控制台的打印就不贴了,这里主要记录一下具体的实现方法.
使用spring事件机制能很好地帮助咱们消除不一样业务间的耦合关系,也能够提升执行效率,应该根据业务场景灵活选择.