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;
}
}



