简单的异步任务,邮件任务,定时执行任务
@EnableAsync //开启异步注解 ~@Async 配套 @EnableScheduling //开启定时功能的注解 ~@Scheduled 配套 @SpringBootApplication public class Springboot09TestApplication { public static void main(String[] args) { SpringApplication.run(Springboot09TestApplication.class, args); } }
1.简单异步任务
1.1AsyncService.java
import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @Service public class AsyncService { //告诉spring这是一个异步的方法 @Async public void hello(){ try { Thread.sleep(3000); //延迟3000ms } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("数据正在处理..."); } }
1.2AsyncController.java
import com.zxy.service.AsyncService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class AsyncController { @Autowired AsyncService asyncService; @RequestMapping("/hello") public String hello(){ asyncService.hello(); return "OK"; } }
2.邮件任务(需要在主方法上添加启动注解@EnableAsync //开启异步注解 ~@Async 配套)
2.1依赖
<!-- javax.mail邮件依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
2.2配置文件
spring.mail.username=1603933673@qq.com spring.mail.password=yegwqubhbuaoiafc spring.mail.host=smtp.qq.com #开启加密验证 QQ需要设置ssl加密 其他邮件不需要 spring.mail.properties.mail.smtp.ssl.enable=true
2.3简单邮件和复杂邮件的编写
package com.zxy; 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 Springboot09TestApplicationTests { @Autowired JavaMailSenderImpl mailSender; @Test void contextLoads() { //一个简单的邮件 SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setSubject("zxy你好!"); //主题 mailMessage.setText("谢谢你的学习"); //内容 mailMessage.setTo("1603933673@qq.com"); //收件人 mailMessage.setFrom("1603933673@qq.com"); //发件人 mailSender.send(mailMessage); } @Test void contextLoads2() throws MessagingException { //一个复杂的邮件 MimeMessage mimeMessage = mailSender.createMimeMessage();//可以方法创建 也可以new MimeMailMessage() //组装 MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true); //true开启多文件 helper是 MimeMessage的实例 helper.setSubject("zxy你好啊 plus"); helper.setText("<p style='color:red'>谢谢你zxy的努力学习!plus+</p>",true); //后面下true开启html识别 //附件 helper.addAttachment("1.jpg",new File("C:\\Users\\zxy\\Desktop\\1.jpg")); helper.addAttachment("2.jpg",new File("C:\\Users\\zxy\\Desktop\\1.jpg")); helper.setTo("1603933673@qq.com"); helper.setFrom("1603933673@qq.com"); mailSender.send(mimeMessage); } }
3.定时执行任务(需要在主方法上添加启动注解@EnableScheduling //开启定时功能的注解 ~@Scheduled 配套)
@Service public class ScheduledService { //在一个特定的时间执行这个方法 //cron表达式 //秒 分 时 日 月 周几 //0 35 20 * * ? 表示 每天20.35分 执行一次 // 0 0/5 12,18 * * ? 表示 每天下午2点到6点 每隔5分钟执行一次 @Scheduled(cron = "0 37 20 * * ?") //? 代表需要管星期几 1-7表示星期一到日 0表示星期日 public void hello(){ System.out.println("hello,你被执行了"); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术