背景
过程
- MyEvent
import lombok.Data;
import org.springframework.context.ApplicationEvent;
import java.util.Date;
@Data
public class MyEvent extends ApplicationEvent {
private String addr;
private String operator;
private Date time;
public MyEvent(Object source, String addr, String operator, Date time) {
super(source);
this.addr = addr;
this.operator = operator;
this.time = time;
}
}
- AnotherEvent
import lombok.Data;
import org.springframework.context.ApplicationEvent;
import java.util.Date;
@Data
public class AnotherEvent extends ApplicationEvent {
private String addr;
private String operator;
private Date time;
public AnotherEvent(Object source, String addr, String operator, Date time) {
super(source);
this.addr = addr;
this.operator = operator;
this.time = time;
}
}
import com.sanding.attachfile.event.AnotherEvent;
import com.sanding.attachfile.event.MyEvent;
import com.sanding.attachfile.service.IEventService;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class AsyncAnnotationListener {
@Resource
private IEventService eventService;
@EventListener
@Async("AsyncTaskExecutor")
public void handleMyEvent(MyEvent myEvent) {
System.out.println("线程:" + Thread.currentThread().getName() + "执行了MyEvent事件");
this.eventService.handleEvent(myEvent.getAddr(), myEvent.getTime(), myEvent.getOperator());
}
@EventListener
@Async("AsyncTaskExecutor")
public void handleAnotherEvent(AnotherEvent anotherEvent) {
System.out.println("线程:" + Thread.currentThread().getName() + "执行了AnotherEvent事件");
this.eventService.handleAnotherEvent(anotherEvent.getAddr(), anotherEvent.getTime(), anotherEvent.getOperator());
}
}
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
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
{
@Override
@Bean("AsyncTaskExecutor")
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(5);
taskExecutor.setMaxPoolSize(10);
taskExecutor.setQueueCapacity(25);
taskExecutor.setThreadNamePrefix("Spring AsyncTaskExecutor Thread-");
taskExecutor.initialize();
return taskExecutor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
}
}
import com.sanding.attachfile.mapper.EventMapper;
import com.sanding.attachfile.service.IEventService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
@Service
public class EventServiceImpl implements IEventService {
@Resource
private EventMapper eventMapper;
@Override
public void handleEvent(String addr, Date time, String operator) {
System.out.println("event: 模拟具体的业务逻辑处理过程, 这里单纯地打印出来即可");
System.out.println("myEvent.addr: " + addr);
System.out.println("myEvent.time: " + time);
System.out.println("myEvent.operator: " + operator);
}
@Override
public void handleAnotherEvent(String addr, Date time, String operator) {
System.out.println("another event: 模拟具体的业务逻辑处理过程, 这里单纯地打印出来即可");
System.out.println("anotherEvent.addr: " + addr);
System.out.println("anotherEvent.time: " + time);
System.out.println("anotherEvent.operator: " + operator);
}
}
import com.sanding.attachfile.AttachfileApplication;
import com.sanding.attachfile.event.AnotherEvent;
import com.sanding.attachfile.event.MyEvent;
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.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Date;
@RunWith(SpringRunner.class)
@SpringBootTest(classes={AttachfileApplication.class})
public class SpringEventLisnerTest {
@Autowired
ApplicationContext applicationContext;
@Test
public void test() {
MyEvent myEvent = new MyEvent(this, "北京", "范柳原", new Date());
this.applicationContext.publishEvent(myEvent);
AnotherEvent anotherEvent = new AnotherEvent(this, "北京", "白流苏", new Date());
this.applicationContext.publishEvent(anotherEvent);
}
}
线程:Spring AsyncTaskExecutor Thread-1执行了MyEvent事件
event: 模拟具体的业务逻辑处理过程, 这里单纯地打印出来即可
myEvent.addr: 北京
myEvent.time: Sat Dec 26 16:54:31 CST 2020
myEvent.operator: 范柳原
线程:Spring AsyncTaskExecutor Thread-2执行了AnotherEvent事件
another event: 模拟具体的业务逻辑处理过程, 这里单纯地打印出来即可
anotherEvent.addr: 北京
anotherEvent.time: Sat Dec 26 16:54:31 CST 2020
anotherEvent.operator: 白流苏
- Event是一个名词,描述着,实际情况下的业务逻辑。任何概念都都行。【邮件事件,短信事件】。
- @EventListener,描述着【监听的某个事件】。
- @Async(“AsyncTaskExecutor”), 描述着【事件由哪个线程池执行的】。
- @EnableAsync, 描述着【异步事件监听配置】
- EventServiceImpl, 模拟我们实际的业务场景。
小结
- 简单梳理Spring Event的基础组件。
- 理解Spring Event基本概念。
- 熟悉Spring Event基础使用过程。