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

springboot监听器

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

springboot监听器

通过类进行注册 事件类
package com.example.event;

import org.springframework.context.ApplicationEvent;


public class MyEvent extends ApplicationEvent {
    public MyEvent(Object source) {
        super(source);
    }
}

监听器
package com.example.listener;

import com.example.event.MyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;


@Component
public class EmailListener implements ApplicationListener {
    @Override
    public void onApplicationEvent(MyEvent event) {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("发送邮件");
    }
}
package com.example.listener;

import com.example.event.MyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;


@Component
public class SmsListener implements ApplicationListener {
    @Override
    public void onApplicationEvent(MyEvent event) {
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("发短信");
    }
}

调用
package com.example.service;

import com.example.event.MyEvent;
import com.example.event.MyEvent1;
import org.apache.catalina.core.ApplicationPushBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.EventListener;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;


@Service
public class MyService {
    @Autowired
    ApplicationEventPublisher publisher;

    public void doSomething() {
        System.out.println("做点事情");
        System.out.println("发布事件");
        publisher.publishEvent(new MyEvent("MyService发布的doSomething"));
    }


    public void doSomething2() {
        System.out.println("做点事情");
        System.out.println("发布事件");
        publisher.publishEvent(new MyEvent1("MyService1发布的doSomething"));
    }


}
通过注解的方式
package com.example.listener;

import com.example.event.MyEvent;
import com.example.event.MyEvent1;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.EventListener;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;


@Component
public class AnnotationListeners {

    @EventListener
    public void sendEmail(MyEvent1 myEvent) throws InterruptedException {
        Thread.sleep(10000);
        System.out.println("通过注解的发送邮件");
    }


    @EventListener
    public void sendSms(MyEvent1 myEvent) throws InterruptedException {
        Thread.sleep(15000);
        System.out.println("通过注解的发送短信");
    }


通过以上的方式注册的监听器,是会同步执行监听器的内容,所以会阻塞,这显然不是好的处理策略,要想实现异步处理,需要为默认的事件处理器增加一个线程池,代码如下:

    
    @Bean
    public ThreadPoolTaskExecutor executor() {
        ThreadPoolTaskExecutor ex = new ThreadPoolTaskExecutor();
        ex.setCorePoolSize(3);
        ex.setMaxPoolSize(10);
        ex.setQueueCapacity(100);
        return ex;
    }

    
    @Bean
    public SimpleApplicationEventMulticaster applicationEventMulticaster(ThreadPoolTaskExecutor executor) {
        SimpleApplicationEventMulticaster mu = new SimpleApplicationEventMulticaster();
        mu.setTaskExecutor(executor);
        return mu;
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1029053.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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