栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

使用Spring事件机制实现异步

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Spring事件机制实现异步

当把一个事件发布到Spring提供的ApplicationContext中,被监听器侦测到,就会执行对应的处理方法。

事件本身

事件是一个自定义的类,需要继承Spring提供的ApplicationEvent。

@Datapublic class MyEvent extends ApplicationEvent {    private String msg;    public MyEvent(Object source, String msg) {        super(source);        this.msg = msg;
    }
}
事件监听

基本方法是实现ApplicationListener接口,自定义一个监听器,实现onApplicationEvent()方法,然后添加到ApplicationContext。
比如:

public class MyListener implements ApplicationListener {  

    @Override  
    public void onApplicationEvent(MyEvent event) {  
        System.out.print("监听到MyEvent事件");  
    }  
}  
...// SpringBoot的启动类中添加监听器
        public static void main(String[] args) {
        SpringApplication application = new SpringApplication(MyApplication.class);
        application.addListeners(new MyListener());
        application.run(args);
    }

也可以使用注解@EventListener(推荐):原理就是通过扫描这个注解,创建监听器并添加到ApplicationContext。

@Component@Slf4jpublic class MyEventHandler {    @EventListener
    public void handleEvent(MyEvent event) {
        log.info("------------处理事件:{}", event.getMsg());        try {
            Thread.sleep(5 * 1000L);
            log.info("事件1(5s)处理完成");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
事件发布

可以通过上下文对象的发布方法ConfigurableApplicationContext::publishEvent()来发布。
也可以实现ApplicationEventPublisherAware接口来发布(推荐)。

@Component@Slf4jpublic class EventService implements ApplicationEventPublisherAware {    public ApplicationEventPublisher publisher;    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {        this.publisher = applicationEventPublisher;
    }    public String doEventWork(String msg) {
        log.info("------------publish event:" + msg);
        MyEvent event = new MyEvent(this, msg);
        publisher.publishEvent(event);        return "OK";
    }
}
测试代码
@SpringBootTest@RunWith(SpringRunner.class)public class EventServiceTest {    @Autowired
    private EventService service;    @Test
    public void eventTest() {
        String msg="Java Code";
        service.doEventWork(msg);
    }
}

image

注意

如果2个事件之间是继承关系,会先监听到子类事件,处理完再监听父类。

// MyEvent2 extends MyEvent@Component@Slf4jpublic class MyEventHandler {    @EventListener
    public void handleEvent(MyEvent event) {
        log.info("------------处理事件:{}", event.getMsg());        try {
            Thread.sleep(5 * 1000L);
            log.info("事件1(5s)处理完成");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }    @EventListener
    public void handleEvent2(MyEvent2 event) {
        log.info("------------处理事件2:{}", event.getMsg());        try {
            Thread.sleep(10 * 1000L);
            log.info("事件2(10s)处理完成");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

当我publish一个子类事件MyEvent2时,日志如下:


image



作者:Joepis
链接:https://www.jianshu.com/p/47ae0bbdf205

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/235327.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号