说明
通过ApplicationEvents以及Listener实现简单的监听事件
实现
1、自定义事件
import org.springframework.context.ApplicationEvent;
/**
* 自定义监听方法测试类
*/
public class MyEvent extends ApplicationEvent {
private String msg;
/**
* 在自定义事件的构造方法中除了第一个source参数,其他参数都可以去自定义,
* 可以根据项目实际情况进行监听传参,这里就只定义个简单的String字符串的透传
* @param source
* @param msg
*/
public MyEvent(Object source,String msg) {
super(source);
this.msg = msg;
}
/**
* 自定义监听器触发的透传打印方法
* @param msg
*/
public void printMsg(String msg)
{
System.out.println("编程事件监听:" + msg);
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
2、定义监听事件
方式1(推荐):注解方式实现监听
import com.liucz.listener.event.MyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
/**
* 测试用自定义监听器,监听事件为MyEvent
*/
@Component
public class MyListener{
/**
* 对监听到的事件进行处理
* @param myEvent
*/
@EventListener
public void print(MyEvent myEvent) {
/*
这里不做处理,只对消息进行透传打印,实际情况,
可以根据项目进行逻辑进行处理
*/
myEvent.printMsg(myEvent.getMsg());
}
}
方式2:ApplicationListener实现监听
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
/**
* 测试用自定义监听器,监听事件为MyEvent
*/
@Component
public class MyListener implements ApplicationListener<MyEvent> {
/**
* 对监听到的事件进行处理
* @param myEvent
*/
@Override
public void onApplicationEvent(MyEvent myEvent) {
/*
这里不做处理,只对消息进行透传打印,实际情况,
可以根据项目进行逻辑进行处理
*/
myEvent.printMsg(myEvent.getMsg());
}
}
3、添加触发事件业务
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
@Service
public class MyService {
@Autowired
ApplicationContext applicationContext;
public void print() throws Exception{
//...省略业务逻辑
//发布UserRegisterEvent事件
String message = "我是被监听事件。。。。";
applicationContext.publishEvent(new MyEvent(this, message));
}
}
测试
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyServiceTest {
@Autowired
MyService myService;
@Test
public void print() throws Exception{
myService.print();
}
}
异步监听
1、添加线程任务池配置,并开启异步支持
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
@Configuration
@EnableAsync
public class ListenerAsyncConfiguration implements AsyncConfigurer {
/**
* 获取异步线程池执行对象
* @return
*/
@Override
public Executor getAsyncExecutor() {
//使用Spring内置线程池任务对象
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
//设置线程池参数
taskExecutor.setCorePoolSize(5);
taskExecutor.setMaxPoolSize(10);
taskExecutor.setQueueCapacity(25);
taskExecutor.initialize();
return taskExecutor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
}
}
2、在监听事件需要异步的方法上添加注解 @Async
@EventListener
@Async
public void print(MyEvent myEvent) {
/*
这里不做处理,只对消息进行透传打印,实际情况,
可以根据项目进行逻辑进行处理
*/
myEvent.printMsg(myEvent.getMsg());
}