task~
- 异步任务
- 邮箱任务
- 定时任务
源码下载
异步任务
开启多线程,我飞了。
package cn.bitqian.service;import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;/*** 异步任务 目的 多线程,交给spring托管,带来良好的用户体验* @author echo lovely* @date 2020/10/30 8:55*/
@Service
public class AsyncService {// 开启了异步任务 多线程@Asyncpublic void hello() {try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("任务进行中...");}}
启动函数EnableXXX
@EnableAsync // 开启了异步任务
上面的睡眠和打印,会同时进行。
如果不开启,程序会等三秒钟响应。
邮箱任务
邮箱附件,使用qq可以发送邮箱。
导入启动器
<!-- com.sun.mail --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency>
这里使用qq邮箱发送,application.properties邮箱的配置
spring.mail.username=999999@qq.com
# 此密码为qq邮箱网页版的授权码,绑定了手机,会发验证码验证
spring.mail.password=abcdefg
spring.mail.host=smtp.qq.com
# 开启加密验证
spring.mail.properties.mail.smtp.ssl.enalbe=true
邮箱发送(发送普通文本,附件)
package cn.bitqian;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;
import java.io.FileNotFoundException;@SpringBootTest
class SpringbootTaskApplicationTests {@AutowiredJavaMailSenderImpl mailSender;@Testvoid contextLoads() {// 一封简单的mail~SimpleMailMessage mailMessage = new SimpleMailMessage();mailMessage.setSubject("hello bitqian");mailMessage.setText("send a simple email using springboot.. by qq");mailMessage.setFrom("999999@qq.com");mailMessage.setTo("999999@qq.com");mailSender.send(mailMessage);}@Testvoid test1() throws MessagingException, FileNotFoundException {// 发送带有附件 的邮件MimeMessage mimeMessage = mailSender.createMimeMessage();// 发送多个附件,并设置编码MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);// 发送图片helper.addAttachment("1.png", new File("C:\\Users\\Pictures\\Eibw4t2UMAEoosS.jfif"));// 发送音乐helper.addAttachment("Innocence.mp3", new File("F:\\music\\Avril Lavigne - Innocence.mp3"));helper.setSubject("发送了图片和音乐");helper.setText("<p style='color:red'>springboot 邮箱发送附件测试</p>");helper.setFrom("999999@qq.com");helper.setTo("999999@qq.com");mailSender.send(mimeMessage);}}
定时任务
quartz,这个。在boot里面更简单了。
- 启用定时任务
package cn.bitqian;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;@SpringBootApplication
@EnableScheduling // 开启定时任务
public class SpringbootTaskApplication {public static void main(String[] args) {SpringApplication.run(SpringbootTaskApplication.class, args);}}
- corn表达式
package cn.bitqian.service;import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;/*** 定时任务 springboot 已经集成* @author echo lovely* @date 2020/10/30 11:26*/
@Service
public class ScheduledService {// corn表达式: 秒 时 分 日 月 周几@Scheduled(cron = "0/1 0/1 0/1 * * ?")public void show() {System.out.println("执行123...");}}
spring实现定时任务