异步处理还是非常常用的,比如我们在网站上发送邮件,后台会去发送邮件,此时前台会造成响应不动,直到邮件发送完毕,响应才会成功,所以我们一般会采用多线程的方式去处理这些任务。
编写方法,假装正在处理数据,使用线程设置一些延时,模拟同步等待的情况
AsncService:
@Service
public class AsyncService {// @Async注解告诉Spring这是一个异步的方法@Asyncpublic void hello() {try{Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("正在处理异步请求...");}
}
AsncController使用该异步服务:
@Controller
public class AsyncController {@Autowiredprivate AsyncService asyncService;// 开启异步后,先返回结果,在等待后台处理请求@RequestMapping("/sleep")@ResponseBodypublic String sleep() {// hello开启异步后,会创建另一个线程进行该操作// 也就是会立即返回ok字符串asyncService.hello();return "ok";}
}
最后不要忘记,主启动类要开启异步注解功能:
@EnableAsync
邮件发送,在我们的日常开发中,也非常的多,Springboot也帮我们做了支持
导入依赖
org.springframework.boot spring-boot-starter-mail
配置邮件信息:
spring:application:name: mail-demomail:host: smtp.exmail.qq.comusername: xxx@xxxpassword: xxxxxxproperties:mail.smtp.auth: truemail.smtp.socketFactory.class: javax.net.ssl.SSLSocketFactorymail.smtp.socketFactory.fallback: falsemail.smtp.socketFactory.port: 465
配置完成后,现在来测试一下邮件发送吧!
@SpringBootTest
class WorkApplicationTests {@AutowiredJavaMailSenderImpl mailSender;@Testvoid contextLoads() throws UnsupportedEncodingException {SimpleMailMessage simpleMailMessage = new SimpleMailMessage();simpleMailMessage.setSubject("KLZA验证码");simpleMailMessage.setText("您的验证码是126073");simpleMailMessage.setTo("yyy@qq.com");simpleMailMessage.setFrom(String.valueOf(new InternetAddress("xxx@xxx", "昆仑智安", "UTF-8")));mailSender.send(simpleMailMessage);}
}
成功收到邮件信息:
当然,我们不可能永远只发送文字的邮件,也可以指定html语句和添加附件:
如下例子:
@SpringBootTest
class WorkApplicationTests {@AutowiredJavaMailSenderImpl mailSender;@Testvoid contextLoads() throws MessagingException, UnsupportedEncodingException {MimeMessage mimeMessage = mailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);helper.setSubject("通知 - 西湖CTF论剑开始啦!");// 添加HTML格式helper.setText("今天 7:30 开始啦!", true);// 发送附件helper.addAttachment("logo.png", new File("D:\\System Data\\mine\\图片\\data\\logo.png"));helper.setTo("3390205563@qq.com");helper.setFrom(String.valueOf(new InternetAddress("guohongquan@dhkjyxgs107.wecom.work", "昆仑智安", "UTF-8")));mailSender.send(mimeMessage);}
}
收到邮件啦!
项目开发中经常需要执行一些定时任务,比如需要在每天凌晨的时候,分析一次前一天的日志信息,Spring为我们提供了异步执行任务调度的方式,提供了两个接口。
TaskExecutor
接口TaskScheduler
接口两个注解:
@EnableScheduling
@Scheduled
实例:一个简单的定时任务:(该任务会在每天的20点36分30秒执行!)
@Service
public class ScheduledService {/*** 秒 分 时 日 月 周几* 0 * * * * MON-FRI*/@Scheduled(cron = "30 36 20 * * ?")public void hello(){System.out.println("一个任务...");}
}
写完定时任务之后,我们需要在主程序上增加 @EnableScheduling
开启定时任务功能:
@EnableScheduling // 开启基于注解的定时任务
我们来详细了解下cron表达式:
(1)0/2 * * * * ? 表示每2秒 执行任务
(1)0 0/2 * * * ? 表示每2分钟 执行任务
(1)0 0 2 1 * ? 表示在每月的1日的凌晨2点调整任务
(2)0 15 10 ? * MON-FRI 表示周一到周五每天上午10:15执行作业
(3)0 15 10 ? 6L 2002-2006 表示2002-2006年的每个月的最后一个星期五上午10:15执行作业
(4)0 0 10,14,16 * * ? 每天上午10点,下午2点,4点
(5)0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时
(6)0 0 12 ? * WED 表示每个星期三中午12点
(7)0 0 12 * * ? 每天中午12点触发
(8)0 15 10 ? * * 每天上午10:15触发
(9)0 15 10 * * ? 每天上午10:15触发
(10)0 15 10 * * ? 每天上午10:15触发
(11)0 15 10 * * ? 2005 2005年的每天上午10:15触发
(12)0 * 14 * * ? 在每天下午2点到下午2:59期间的每1分钟触发
(13)0 0/5 14 * * ? 在每天下午2点到下午2:55期间的每5分钟触发
(14)0 0/5 14,18 * * ? 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
(15)0 0-5 14 * * ? 在每天下午2点到下午2:05期间的每1分钟触发
(16)0 10,44 14 ? 3 WED 每年三月的星期三的下午2:10和2:44触发
(17)0 15 10 ? * MON-FRI 周一至周五的上午10:15触发
(18)0 15 10 15 * ? 每月15日上午10:15触发
(19)0 15 10 L * ? 每月最后一日的上午10:15触发
(20)0 15 10 ? * 6L 每月的最后一个星期五上午10:15触发
(21)0 15 10 ? * 6L 2002-2005 2002年至2005年的每月的最后一个星期五上午10:15触发
(22)0 15 10 ? * 6#3 每月的第三个星期五上午10:15触发
下一篇:Activity