1、springboot中执行异步任务:@EnableAysnc、@Aysnc
1)创建任务
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class AsyncService {
public void hello(){
try {
Thread.sleep(3000);//同步等待
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("处理数据中....");
}
}
2)调用方法(此时是同步,3s之后才会响应返回success)
import com.example.cache.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AsyncController {
@Autowired
AsyncService asyncService;
@GetMapping("/hello")
public String hello(){
asyncService.hello();//调用方法
return "success";//3秒之后才会响应
}
}
3)将步骤1)中的方法标注为异步:@Aysnc
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class AsyncService {
//@Async:标注这是一个异步方法
@Async
public void hello(){
try {
Thread.sleep(3000);//等待
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("处理数据中....");
}
}
4)开启异步注解@EnableAsync //开启异步注解功能
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@EnableAsync //开启异步注解功能
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
5)此时调用方法,浏览器立即反应返回success
2、springboot执行定时任务:@EnableScheduling、@Scheduled
1)定时cron表达式
/:步长(每隔多长时间)
例:"0/4 * * * * MON-SAT"(从0秒启动,每隔4秒执行一次)
?:冲突匹配
例:用*代表每一天,MON代表星期一,此时会冲突,因为不是每一天都是星期一,所以要用?代替任意天的*
2)定时任务
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
public class ScheduledService {
/**
* cron:
* second(秒),minute(分),hour(时),day of month(日),month(月),day of week(周几)
* 例:0 * * * * MON-FRI
* MON-FRI:从周一到周五,*:任意时刻,* * * *:任意月任意日任意小时任意分钟,0:整秒启动
* 即:周一到周五每一分钟都运行一次
*/
@Scheduled(cron = "0 * * * * MON-FRI")
public void hello(){
System.out.println("hello ... ");
}
}
3)开启基于注解的定时任务:@EnableScheduling //开启基于注解的定时任务
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableScheduling //开启基于注解的定时任务
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
3、springboot与邮件任务
1)引入依赖:spring-boot-starter-mail(https://docs.spring.io/spring-boot/docs/2.0.4.RELEASE/reference/htmlsingle/#using-boot-starter)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2)自动配置原理
3)配置发件人用户名、密码、邮件服务器地址
spring.mail.username=yourname@foxmail.com
#为了安全起见,该密码不是邮箱的密码,而是第三方授权码
spring.mail.password=dajjilvjikmi
#主机地址(SMTP地址)
spring.mail.host=smtp.qq.com
#额外配置(ssl安全连接)开启ssl
#spring.mail.properties.mail.smtp.ssl.enable=true
使用qq邮箱开启服务,生成授权码
4)简单邮件测试
4)简单邮件测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootApplicationTests implements Serializable {
@Autowired
JavaMailSenderImpl mailSender;
@Test
public void test05() {
//创建简单邮件
SimpleMailMessage message = new SimpleMailMessage();
//邮件设置
message.setSubject("通知.今晚开会");//邮件标题
message.setText("今晚7.30开会");//邮件内容
message.setTo("yourname1@qq.com");
message.setFrom("yourname@qq.com");//发送者
mailSender.send(message);
}
}
5)复杂邮件测试(带附件)
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootApplicationTests implements Serializable {
@Autowired
JavaMailSenderImpl mailSender;
@Test
public void test06() throws MessagingException {
//1、创建一个复杂的消息邮件(最终要发送的邮件)
MimeMessage mimeMessage = mailSender.createMimeMessage();
//new MimeMessageHelper(mimeMessage,是否上传文件);
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
//邮件设置
helper.setSubject("通知.今晚开会");//邮件标题
helper.setText("<b style='color:red'>今晚7.30开会</b>",true);//邮件内容 html片段
helper.setTo("yourname1@qq.com");
helper.setFrom("yourname@qq.com");//发送者
//上传文件
helper.addAttachment("1.png",new File("D:\\log\\1.png"));
helper.addAttachment("2.png",new File("D:\\log\\2.png"));
mailSender.send(mimeMessage);
}
}