简介
使用案例介绍了Spring封装的ThreadPoolTaskExecutor线程池的使用方式,
以多线程并发执行任务为例
业务需求
模拟用户注册账号,注册成功发送短信和邮件通知
一、编写实体类/服务接口/控制器
package com.sundial.listener.entity;
import lombok.Data;
@Data
public class UserBean {
private String name;
private String password;
}
package com.sundial.listener.service;
import com.sundial.listener.entity.UserBean;
public interface IUserService {
void register(UserBean user);
}
package com.sundial.listener.controller;
import com.sundial.listener.entity.UserBean;
import com.sundial.listener.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@Controller
public class UserController {
@Autowired
private IUserService userService;
@ResponseBody
@RequestMapping("/register")
public String register(UserBean user){
user.setName("小明");
user.setPassword("123456");
userService.register(user);
return "注册成功";
}
}
用户注册完成我们可以采取事件监听的方式发送邮件和短信以此来达到松耦合的目的
二、用户注册事件/监听
package com.sundial.listener.event;
import com.sundial.listener.entity.UserBean;
import lombok.Getter;
import org.springframework.context.ApplicationEvent;
@Getter
public class UserRegisterEvent extends ApplicationEvent {
private UserBean userBean;
public UserRegisterEvent(Object source,UserBean userBean) {
super(source);
this.userBean = userBean;
}
}
package com.sundial.listener.event;
import com.sundial.listener.entity.UserBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.SmartApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class UserRegisterSmsListener implements SmartApplicationListener {
@Override
public boolean supportsEventType(Class<? extends ApplicationEvent> aClass) {
boolean flag = aClass == UserRegisterEvent.class;
return flag;
}
@Async
@Override
public void onApplicationEvent(ApplicationEvent applicationEvent) {
UserRegisterEvent userRegisterEvent = (UserRegisterEvent) applicationEvent;
UserBean userBean = userRegisterEvent.getUserBean();
System.out.println("线程:"+Thread.currentThread().getName());
log.info("用户:{},密码:{},注册成功,发送短信通知",userBean.getName(),userBean.getPassword());
}
}
package com.sundial.listener.event;
import com.sundial.listener.entity.UserBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.EventListener;
import org.springframework.context.event.SmartApplicationListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class UserRegisterEmailListener implements SmartApplicationListener {
@Override
public boolean supportsEventType(Class<? extends ApplicationEvent> aClass) {
boolean flag = aClass == UserRegisterEvent.class;
return flag;
}
@Async
@Override
public void onApplicationEvent(ApplicationEvent applicationEvent) {
UserRegisterEvent userRegisterEvent = (UserRegisterEvent) applicationEvent;
UserBean user = userRegisterEvent.getUserBean();
System.out.println("线程:"+Thread.currentThread().getName());
log.info("用户:{},密码:{},注册成功,发送邮件通知",user.getName(),user.getPassword());
}
}
在UserServiceImpl中使用监听
package com.sundial.listener.service.impl;
import com.sundial.listener.entity.UserBean;
import com.sundial.listener.event.UserRegisterEvent;
import com.sundial.listener.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Service
public class UserServiceImpl implements IUserService {
private Map<String,UserBean> map = new HashMap<>();
@Autowired
private ApplicationContext applicationContext;
public void register(UserBean user){
map.put(user.getName(),user);
log.info("保存成功");
applicationContext.publishEvent(new UserRegisterEvent(this,user));
}
}
三、加入ThreadPoolTaskExecutor线程池配置
package com.sundial.listener.config;
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 {
@Override
public Executor getAsyncExecutor(){
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(5);
taskExecutor.setMaxPoolSize(10);
taskExecutor.setQueueCapacity(25);
taskExecutor.setKeepAliveSeconds(300);
taskExecutor.setThreadNamePrefix("ics-Executor-");
taskExecutor.initialize();
return taskExecutor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b309bb1e9ba6098922d70eb77c5f87e3.png)