Springboot异步发送邮件

发布于 2021-04-10  648 次阅读


多线程下的异步处理可以加快任务处理的速度,提升用户体验,特此记录

 

首先编写线程池配置类,配置Springboot线程池

@Configuration//声明为配置文件并装配到spring
@ComponentScan("com.yang.service")
@EnableAsync//开启异步任务支支持
public class ThreadConfig implements AsyncConfigurer {

    @Override
    public Executor getAsyncExecutor(){
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        //配置核心线程数
        taskExecutor.setCorePoolSize(5);
        //配置最大线程数
        taskExecutor.setMaxPoolSize(10);
        //空闲的线程多久时间后被销毁
        taskExecutor.setKeepAliveSeconds(25);
        //执行初始化
        taskExecutor.initialize();
        return taskExecutor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return AsyncConfigurer.super.getAsyncUncaughtExceptionHandler();
    }
}
定义异步任务实现类CustomMultiThreadingService 
在该类中创建两个异步方法进行邮件发送
@Transactional(rollbackFor = Exception.class)
@Service
public class CustomMultiThreadingService {


    @Autowired
    JavaMailSenderImpl javaMailSender;

    @Autowired
    JavaMailSenderImpl javaMailSender2;
    private Logger logger = LoggerFactory.getLogger(CustomMultiThreadingService.class);
    /**
     * @Description:通过@Async注解表明该方法是一个异步方法,
     * 如果注解在类级别上,则表明该类所有的方法都是异步方法,而这里的方法自动被注入使用ThreadPoolTaskExecutor作为TaskExecutor
     * @Title: executeAysncTask1
     */
    //在特定时间执行这行代码 Timer
    @Async
    public void executeAysncTask1(Integer i){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo("1606598203@qq.com");
        message.setSubject("你好");
        message.setText("9973");
        message.setFrom("2690706243@qq.com");
        javaMailSender.send(message);
        logger.info("CustomMultiThreadingService ==> executeAysncTask1 method: 执行异步任务{} ", i);
    }

    /**
     * @Description:通过@Async注解表明该方法是一个异步方法,
     * 如果注解在类级别上,则表明该类所有的方法都是异步方法,而这里的方法自动被注入使用ThreadPoolTaskExecutor作为TaskExecutor
     * @Title: executeAsyncTask2
     */
    @Async
    public void executeAsyncTask2(Integer i){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo("2690706243@qq.com");
        message.setSubject("你好");
        message.setText("9973");
        message.setFrom("2690706243@qq.com");
        javaMailSender.send(message);
        logger.info("CustomMultiThreadingService ==> executeAsyncTask2 method: 执行异步任务{} ", i);
    }
}

定义CustomMultiThreadingController类
CustomMultiThreadingController类调用多线程任务CustomMultiThreadingService类
@Transactional(rollbackFor = Exception.class)
@Controller
public class CustomMultiThreadingController {
    @Autowired
    private CustomMultiThreadingService customMultiThreadingService;

    TransactionManager transactionManager;
    @ResponseBody
    @RequestMapping("/dotask")
    public String doTask() {
        for (int i=0;i<1;i++){
                customMultiThreadingService.executeAysncTask1(i);
                customMultiThreadingService.executeAsyncTask2(i);

//            customMultiThreadingService.Tohello();
        }

        return "success";
    }
}

启动类 
//开启定时功能的注解
@SpringBootApplication
@EnableScheduling
public class Application {

   public static void main(String[] args) throws InterruptedException {
      SpringApplication.run(Application.class, args);
      
   }
}
测试:访问http://127.0.0.1:8080/dotask端口

成功发送,异步发送邮件模块实现!!!


她喜欢所以就做咯