Spring中使用@Async注解使Even监听事件之间的执行变为异步
2016-11-27 00:00
471 查看
下面我们来介绍,使用spring的@Async注解使Even监听事件,变为异步事件,这只是很简单的应用,如果要配合业务使用,请根据自身业务来调整
本人环境是:JDK版本号1.8,spring版本为4.2首先,我们先来看看,不使用@Async注解的基础事件,是怎么写的
1.我们先定义一个事件,该类继承 ApplicationEven的抽象类
public class DemoEvent extends ApplicationEvent{ //事件监听消息 private String msg; public DemoEvent(Object source,String msg) { super(source); this.msg=msg; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; }
2.事件有了,那么我们就肯定需要一个发布者吧,不然,谁知道你需要发布任务呢.
@Component public class DemoPubLisher { @Autowired private ApplicationContext applicationContext; //事件发布方法 public void pushListener(String msg){ applicationContext.publishEvent(new DemoEvent(this,msg)); } }
3.既然发布者有了,肯定也要有一个监听者嘛,时刻监听者发布者是否有发布任务,然后进行处理
@Component public class DemoEventListener implements ApplicationListener<DemoEvent>{ static Logger log = Logger.getLogger(DemoEventListener.class); //监听事件执行方法 public void onApplicationEvent(DemoEvent event) { String msg = event.getMsg(); log.info("DemoEventListener,监听到了 DemoEvent 发布的消息:"+msg); //让线程睡眠是用来测试,监听者监听到发布者发布事件后,执行任务的时候 //是否是同步执行 if(msg.equals("测试消息监听")){ try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
监听者跟发布者@Component 注解相信大家一定不陌生了,但是怕有些同学不知道,还是解释下,这表示将该类注册为一个spring容器中的一个bean!
事件,监听者,发布者都有了,那么我们就来进行测试,看看事件之间的执行,是同步的,还是异步的.
下面就是测试代码
@Configuration @ComponentScan("com.spring.annotation.event") public class EventConfig { public static void main(String[] args){ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(EventConfig.class); //用注入的形式完成事件发布 DemoPubLisher pushListener = context.getBean(DemoPubLisher.class); pushListener.pushListener("测试消息监听"); pushListener.pushListener("测试消息监听1"); context.close(); } }
@Configuration 注解表示,这是一个组合注解,表示这是一个配置文件,也是一个bean,因为里面包含了@Component注解
@ComponentScan("com.spring.annotation.event") 扫描包,将包含spring注解的类,注册为一个bean
下面,我们执行main方法,测试看打印结果
红色区域就是打印结果,箭头是打印时间,可以发现,这是同步执行的,只有等第一个事件执行完毕后,才会执行第二个,如果想要事件监听者,异步执行怎么办?我们可以使用@Async 注解,改2个地方就好了,下面是代码
我们先修改监听者.
@Component public class DemoEventListener implements ApplicationListener<DemoEvent>{ static Logger log = Logger.getLogger(DemoEventListener.class); //监听事件执行方法 @Async public void onApplicationEvent(DemoEvent event) { String msg = event.getMsg(); log.info("DemoEventListener,监听到了 DemoEvent 发布的消息:"+msg); //让线程睡眠是用来测试,监听者监听到发布者发布事件后,执行任务的时候 //是否是同步执行 if(msg.equals("测试消息监听")){ try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
在方法上,增加了@Async注解,表示这是一个异步方法,如果注解使用到类上,表示类下所有方法,都是异步的.
接下来,我们在修改EvenConfig类
@Configuration @ComponentScan("com.spring.annotation.event") @EnableAsync public class EventConfig { public static void main(String[] args){ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(EventConfig.class); //用注入的形式完成事件发布 DemoPubLisher pushListener = context.getBean(DemoPubLisher.class); pushListener.pushListener("测试消息监听"); pushListener.pushListener("测试消息监听1"); context.close(); } }
在类上,我们增加了注解@EnableAsync 表示开启异步@Async注解,不使用@EnableAsync注解,@Async是不会生效的.
执行方法,来看看打印结果吧
我们可以发现,打印时间是一致的,表示第一个监听执行睡眠,并不会影响第二个的执行,我们可以从日志看出一二,在打印结果的时间后面 有个 SimpleAsyncTaskExecutor-1,跟SimpleAsyncTaskExecutor-2 分别是两个线程,说明,监听者,每次执行该方法,都是新起一个线程去执行,所以监听到的第二个,并不会等第一个执行完毕在执行第二个.
有同学会问了,如果是启动tomcat怎么办,@EnableAsync注解应该加到哪里呢?
如果是普通的web项目,那么@EnableAsync注解,你添加到监听者类上边就可以了,表示开启异步注解
以上,@Async跟@EnableAsync注解都是分别是Spring3.0跟3.1版本才有,如果高于此版本,就不需要担心没有该注解,如果想知道@EnableAsync注解的原理,可以查看 AsyncConfigurationSelector 类的源码,上面有解释,这里,就不给大家进行解释了
以上,均为本人测试而得出的结果,可能会有出入,或者错误,欢迎指正欢迎转载,请注明出处跟作者,谢谢!
相关文章推荐
- 使用spring的@Async异步执行方法
- 使用spring的@Async异步执行方法
- 使用spring的@Async异步执行方法
- 使用spring的@Async异步执行方法以及不执行问题
- 使用spring的@Async异步执行方法
- spring中使用@Async注解进行异步处理
- spring中使用@Async注解进行异步处理
- 使用spring的@Async异步执行方法
- 使用spring的@Async异步执行方法
- 使用spring @Scheduled注解执行定时任务、
- spring 使用注解启动定时器不执行
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务、
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务
- Spring中使用注解 @Scheduled执行定时任务
- 使用spring @Scheduled注解执行定时任务、
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务