springboot监听器

通过类进行注册

事件类


package com.example.event;

import org.springframework.context.ApplicationEvent;

/**
 * @author micro.cloud.fly
 * @date 2022/7/25 2:08 下午
 * @desc
 */
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;

/**
 * @author micro.cloud.fly
 * @date 2022/7/25 2:10 下午
 * @desc
 */
@Component
public class EmailListener implements ApplicationListener<MyEvent> {
    @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;

/**
 * @author micro.cloud.fly
 * @date 2022/7/25 2:09 下午
 * @desc
 */
@Component
public class SmsListener implements ApplicationListener<MyEvent> {
    @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;

/**
 * @author micro.cloud.fly
 * @date 2022/7/25 2:11 下午
 * @desc
 */
@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;

/**
 * @author micro.cloud.fly
 * @date 2022/7/25 2:34 下午
 * @desc
 */
@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("通过注解的发送短信");
    }


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


    /**
     * 配置线程池,用来实现异步的事件监听
     *
     * @return
     */
    @Bean
    public ThreadPoolTaskExecutor executor() {
        ThreadPoolTaskExecutor ex = new ThreadPoolTaskExecutor();
        ex.setCorePoolSize(3);
        ex.setMaxPoolSize(10);
        ex.setQueueCapacity(100);
        return ex;
    }

    /**
     * 默认是发起事件的线程会执行监听器,这显然会阻塞住
     * 默认使用SimpleApplicationEventMulticaster进行事件的管理,
     * 此处给加入一个线程池,就会使用线程池来处理监听的事件
     * <p>
     * 配置了线程池的SimpleApplicationEventMulticaster,将会异步处理事件监听
     *
     * @param executor
     * @return
     */
    @Bean
    public SimpleApplicationEventMulticaster applicationEventMulticaster(ThreadPoolTaskExecutor executor) {
        SimpleApplicationEventMulticaster mu = new SimpleApplicationEventMulticaster();
        mu.setTaskExecutor(executor);
        return mu;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

micro_cloud_fly

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值