详解SpringBoot实现事件同步与异步监听

 更新时间:2022年06月22日 09:25:35   作者:IT利刃出鞘  
这篇文章主要通过示例为大家详细介绍了SpringBoot中的事件的用法和原理以及如何实现事件同步与异步监听,快跟随小编一起学习学习吧

简介

说明

本文用示例介绍SpringBoot中的事件的用法及原理。

事件监听简述

事件的发布与监听从属于观察者模式;和MQ相比,事件的发布与监听偏向于处理服务内的某些逻辑。 

多个监听器可以监听同一个事件。例如:发布了事件A,监听器A和监听器B都监听了事件A,则监听器A和B都会进行处理。

同步与异步监听

监听方式特点使用时机
同步监听发布器线程与监听器线程处于同一线程1.监听逻辑处理较快
2.需要紧接着根据监听器追踪业务线程
异步监听发布器线程与监听器线程处于不同线程1.监听逻辑处理比较耗时
2.追求响应性能

事件的顺序

可使用实现Ordered接口的方式,调整监听器顺序。

注意:必须是同时实现 ApplicationListener<MyEvent>,Ordered这样的方法才能控制顺序。

下边几种都是无法控制顺序的:

  • @Component+@EventListerner+实现Ordered
  • 实现 ApplicationListener<MyEvent>+@Order

实例

同步监听(无序)

事件

package com.example.event;
 
import org.springframework.context.ApplicationEvent;
 
public class MyEvent extends ApplicationEvent {
 
    public MyEvent(Object source) {
        super(source);
    }
 
}

监听器

监听器1

package com.example.event;
 
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
 
@Component
public class MyListener {
    @EventListener
    public void abc(MyEvent event) {
        System.out.println("监听器:        " + "MyListener");
        System.out.println("监听器所在线程:" + Thread.currentThread().getName());
        System.out.println("事件:          " + event);
        System.out.println("事件的数据:    " + event.getSource());
    }
}

监听器2

package com.example.event;
 
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
 
@Component
public class MyListener2 {
 
    @EventListener
    public void onApplicationEvent(MyEvent event) {
        System.out.println("监听器:      " + "MyListener2");
        System.out.println("  所在线程:  " + Thread.currentThread().getName());
        System.out.println("  事件:      " + event);
        System.out.println("  事件的数据:" + event.getSource());
    }
}

发布器

package com.example.event;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
 
@Component
public class MyPublisher {
    @Autowired
    ApplicationContext applicationContext;
 
    public void myPublish(String message) {
        System.out.println("发布器所在线程:" + Thread.currentThread().getName());
        applicationContext.publishEvent(new MyEvent(message));
    }
}

测试

写一个Controller

package com.example.controller;
 
import com.example.event.MyPublisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    @Autowired
    MyPublisher myPublisher;
 
    @GetMapping("/test1")
    public String test1() {
        myPublisher.myPublish("Hello");
        return "test1 success";
    }
}

启动后,访问:http://localhost:8080/test1

后端输出:

发布器所在线程:http-nio-8080-exec-1
监听器:      MyListener
  所在线程:  http-nio-8080-exec-1
  事件:      com.example.event.MyEvent[source=Hello]
  事件的数据:Hello
监听器:      MyListener2
  所在线程:  http-nio-8080-exec-1
  事件:      com.example.event.MyEvent[source=Hello]
  事件的数据:Hello

可以发现,所有监听器和发布器都在同一个线程。 

同步监听(有序)

事件

package com.example.event;
 
import org.springframework.context.ApplicationEvent;
 
public class MyEvent extends ApplicationEvent {
 
    public MyEvent(Object source) {
        super(source);
    }
 
}

监听器

监听器1

package com.example.event;
 
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
 
@Component
public class MyListener implements ApplicationListener<MyEvent>,  Ordered {
    @Override
    public void onApplicationEvent(MyEvent event) {
        System.out.println("监听器:      " + "MyListener");
        System.out.println("  所在线程:  " + Thread.currentThread().getName());
        System.out.println("  事件:      " + event);
        System.out.println("  事件的数据:" + event.getSource());
    }
 
    @Override
    public int getOrder() {
        return 2;
    }
}

监听器2

package com.example.event;
 
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
 
@Component
public class MyListener2 implements ApplicationListener<MyEvent>, Ordered {
 
    public void onApplicationEvent(MyEvent event) {
        System.out.println("监听器:      " + "MyListener2");
        System.out.println("  所在线程:  " + Thread.currentThread().getName());
        System.out.println("  事件:      " + event);
        System.out.println("  事件的数据:" + event.getSource());
    }
 
    @Override
    public int getOrder() {
        return 1;
    }
}

发布器

package com.example.event;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
 
@Component
public class MyPublisher {
    @Autowired
    ApplicationContext applicationContext;
 
    public void myPublish(String message) {
        System.out.println("发布器所在线程:" + Thread.currentThread().getName());
        applicationContext.publishEvent(new MyEvent(message));
    }
}

测试

写一个Controller

package com.example.controller;
 
import com.example.event.MyPublisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    @Autowired
    MyPublisher myPublisher;
 
    @GetMapping("/test1")
    public String test1() {
        myPublisher.myPublish("Hello");
        return "test1 success";
    }
}

启动后,访问:http://localhost:8080/test1

后端输出:

发布器所在线程:http-nio-8080-exec-1
监听器:      MyListener2
  所在线程:  http-nio-8080-exec-1
  事件:      com.example.event.MyEvent[source=Hello]
  事件的数据:Hello
监听器:      MyListener
  所在线程:  http-nio-8080-exec-1
  事件:      com.example.event.MyEvent[source=Hello]
  事件的数据:Hello

如果将监听器的实现的Ordered顺序颠倒,则输出结果如下:

发布器所在线程:http-nio-8080-exec-1
监听器:      MyListener
  所在线程:  http-nio-8080-exec-1
  事件:      com.example.event.MyEvent[source=Hello]
  事件的数据:Hello
监听器:      MyListener2
  所在线程:  http-nio-8080-exec-1
  事件:      com.example.event.MyEvent[source=Hello]
  事件的数据:Hello

异步监听(无序)

方法:

  • 开启异步监听
  • 在监听器上加@Async(此监听器必须是@Component方法注册的)

事件

package com.example.event;
 
import org.springframework.context.ApplicationEvent;
 
public class MyEvent extends ApplicationEvent {
 
    public MyEvent(Object source) {
        super(source);
    }
 
}

同步监听器

package com.example.event;
 
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
 
@Component
public class MyListener {
    @EventListener
    public void abc(MyEvent event) {
        System.out.println("监听器:      " + "MyListener");
        System.out.println("  所在线程:  " + Thread.currentThread().getName());
        System.out.println("  事件:      " + event);
        System.out.println("  事件的数据:" + event.getSource());
    }
}

异步监听器

package com.example.event;
 
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
 
@Component
@Async
public class MyListener2 {
 
    @EventListener
    public void onApplicationEvent(MyEvent event) {
        System.out.println("监听器:      " + "MyListener2");
        System.out.println("  所在线程:  " + Thread.currentThread().getName());
        System.out.println("  事件:      " + event);
        System.out.println("  事件的数据:" + event.getSource());
    }
}

发布器

package com.example.event;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
 
@Component
public class MyPublisher {
    @Autowired
    ApplicationContext applicationContext;
 
    public void myPublish(String message) {
        System.out.println("发布器所在线程:" + Thread.currentThread().getName());
        applicationContext.publishEvent(new MyEvent(message));
    }
}

启动类

package com.example;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
 
@SpringBootApplication
@EnableAsync
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

测试

写一个Controller

package com.example.controller;
 
import com.example.event.MyPublisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    @Autowired
    MyPublisher myPublisher;
 
    @GetMapping("/test1")
    public String test1() {
        myPublisher.myPublish("Hello");
        return "test1 success";
    }
}

启动后,访问:http://localhost:8080/test1

后端输出:

发布器所在线程:http-nio-8080-exec-1
监听器:      MyListener
  所在线程:  http-nio-8080-exec-1
  事件:      com.example.event.MyEvent[source=Hello]
  事件的数据:Hello
监听器:      MyListener2
  所在线程:  task-1
  事件:      com.example.event.MyEvent[source=Hello]
  事件的数据:Hello

到此这篇关于详解SpringBoot实现同步与异步事件监听的文章就介绍到这了,更多相关SpringBoot事件监听内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 如何处理@PathVariable中的特殊字符问题

    如何处理@PathVariable中的特殊字符问题

    这篇文章主要介绍了如何处理@PathVariable中的特殊字符问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • Java List 集合如何去除null元素

    Java List 集合如何去除null元素

    这篇文章主要介绍了Java List 集合如何去除null元素,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • SpringCloud @FeignClient参数的用法解析

    SpringCloud @FeignClient参数的用法解析

    这篇文章主要介绍了SpringCloud @FeignClient参数的用法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • netty pipeline中的inbound和outbound事件传播分析

    netty pipeline中的inbound和outbound事件传播分析

    这篇文章主要为大家介绍了netty pipeline中的inbound和outbound事件传播分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-04-04
  • Spring 与 JDK 线程池的简单使用示例详解

    Spring 与 JDK 线程池的简单使用示例详解

    这篇文章主要介绍了Spring 与 JDK 线程池的简单使用,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-09-09
  • 使用Java判定一个数值是否在指定的开闭区间范围内

    使用Java判定一个数值是否在指定的开闭区间范围内

    这篇文章主要给大家介绍了关于使用Java判定一个数值是否在指定的开闭区间范围内的相关资料,文中通过实例代码介绍的非常详细,对大家学习或者使用Java具有一定的参考学习价值,需要的朋友可以参考下
    2022-09-09
  • SWT(JFace)Group(分组显示)

    SWT(JFace)Group(分组显示)

    SWT(JFace)体验之Group(分组显示)
    2009-06-06
  • JVM的垃圾回收算法一起来看看

    JVM的垃圾回收算法一起来看看

    这篇文章主要为大家详细介绍了JVM的垃圾回收算法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-03-03
  • JAVA使用HtmlUnit爬虫工具模拟登陆CSDN案例

    JAVA使用HtmlUnit爬虫工具模拟登陆CSDN案例

    今天小编就为大家分享一篇关于JAVA使用HtmlUnit爬虫工具模拟登陆CSDN案例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • Java实现的求逆矩阵算法示例

    Java实现的求逆矩阵算法示例

    这篇文章主要介绍了Java实现的求逆矩阵算法,涉及java基于数组的矩阵遍历与运算相关操作技巧,需要的朋友可以参考下
    2017-09-09

最新评论