一、异步任务
异步处理采用多线程的方式去处理,只需要在方式上加注解@Async,告诉spring这是一个异步方法,在后台处理请求时间,网页瞬间响应
第一步 创建一个类AsyncService
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class AsyncService {
@Async //告诉Spring这是一个异步方法
public void Hello(){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("正在执行中.....");
}
}
第二步 创建一个AsyncController类
import com.dyt.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @RestController注解相当于@ResponseBody + @Controller合在一起的作用。
* 1) 如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,
* 或者html,配置的视图解析器 InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。
* 2) 如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。
* 如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。
* 3)如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。
*/
@RestController
public class AsyncController {
@Autowired
AsyncService asyncService;
@GetMapping("/hello")
public String Hello(){
asyncService.Hello();
return "success";
}
}
第三步 在主程序上添加一个注解@EnableAsync ,开启异步注解功能
@EnableAsync //开启异步注解功能
@SpringBootApplication
public class Springboot01ShiroApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot01ShiroApplication.class, args);
}
}
第四步 运行网页瞬间响应,后台代码依旧执行
二、定时任务
项目开发中经常需要执行一些定时任务,比如需要在每天凌晨的时候,分析一次前一天的日志信息,Spring为我们提供了异步执行任务调度的方式,提供了两个接口。
- TaskExecutor接口
- TaskScheduler接口
两个注解:
- @EnableScheduling
- @Scheduled
主要用到的是cron表达式 详细解释地址http://www.bejson.com/othertools/cron/
第一步 创建一个ScheduledService
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
public class ScheduledService {
//秒 分 时 日 月 周几
@Scheduled(cron="0 25 10 ? * *") //每天上午10:25触发
public void hello(){
System.out.println("scheduled正在执行.....");
}
}
第二步 在主程序上增加@EnableScheduling 开启定时任务功能
@EnableScheduling //开启基于注解的定时任务
@SpringBootApplication
public class Springboot01ShiroApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot01ShiroApplication.class, args);
}
}
第三步 运行主程序
三、邮件任务
邮件发送,在我们的日常开发中,也非常的多,Springboot也帮我们做了支持
- 邮件发送需要引入spring-boot-start-mail
- SpringBoot 自动配置MailSenderAutoConfiguration
- 定义MailProperties内容,配置在application.yml中
- 自动装配JavaMailSender
- 测试邮件发送
第一步 引入pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
第二步 配置文件
在application.properties中配置如下
spring.mail.host=smtp.qq.com
spring.mail.username=你的qq号
spring.mail.password=你的qq授权码
# qq需要配置ssl
spring.mail.properties.mail.smtp.ssl.enable=true
如何获取授权码?
在QQ邮箱中的设置->账户->开启pop3和smtp服务
第三步 Spring单元测试
package com.dyt;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
@SpringBootTest
class Springboot01ShiroApplicationTests {
@Autowired
JavaMailSenderImpl mailSender;
@Test
void contextLoads() {
//邮件设置1:一个简单的邮件
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setSubject("学习邮件发送");
simpleMailMessage.setText("主要学到了JavaMailSenderImpl SimpleMailMessage ");
simpleMailMessage.setTo("1658875679@qq.com");
simpleMailMessage.setFrom("1658875679@qq.com");
mailSender.send(simpleMailMessage);
}
@Test
void contextLoads2() throws MessagingException {
//邮件设置2:一个复杂的邮件
MimeMessage mimeMessage = mailSender.createMimeMessage();
//组装
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
//正文
helper.setSubject("通知-明天来继续学习");
helper.setText("<b style='color:red'>明天 8:30来开会</b>",true);
//发送附件
helper.addAttachment("1.jpg",new File("C:\\Users\\Administrator\\Desktop\\1.jpg"));
helper.setTo("1658875679@qq.com");
helper.setFrom("1658875679@qq.com");
mailSender.send(mimeMessage);
}
}
封装发送复杂邮件方法工具
/**
*
* @param html 是否支持多文件
* @param subject 标题
* @param text 正文
* @param filename 文件名
* @param file 文件路径
* @param to 发送方
* @param from 接收方
* @throws MessagingException
* @Author gdh 作者
*/
public void sendMail(Boolean html,String subject,String text,String filename,File file,String to,String from) throws MessagingException {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,html);
helper.setSubject(subject);
helper.setText(text,true);
helper.addAttachment(filename, file);
helper.setTo(to);
helper.setFrom(from);
mailSender.send(mimeMessage);
}