Detailed explanation of SpringBoot tasks

Zero degrees Celsius 2022-08-06 16:24:24 阅读数:979

detailedexplanationspringboottasks

SpringBoot任务详解

  • 异步任务
  • 定时任务
  • 邮件发送

1、异步任务

  1. 写一个service

AsyncService.java

package com.qian.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class AsyncService {

//告诉Spring这是一个异步的方法
@Async
public void hello(){

try{

Thread.sleep(3000);
}catch (InterruptedException e){

e.printStackTrace();
}
System.out.println("数据正在处理...");
}
}

@Async 告诉spring这是一个异步的方法

  1. Add to enable asynchronous annotations in startup
package com.qian;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
//开启异步注解功能
@EnableAsync
@SpringBootApplication
public class SpringbootTestApplication {

public static void main(String[] args) {

SpringApplication.run(SpringbootTestApplication.class, args);
}
}

@EnableAsync 开启异步注解功能

  1. controller测试
package com.qian.controller;
import com.qian.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController //返回字符串,用RestController
public class AsyncController {

@Autowired
AsyncService asyncService;
@RequestMapping("/hello")
public String hello(){

asyncService.hello();//停止三秒
return "OK";
}
}

//The page will refresh immediately and returnok,But the backend will wait3秒后才输出“数据正在处理中”

2、邮件发送

  1. 相比于之前的Javaweb,springbootThe email sending function has also been simplified a lot

  2. 首先导入相关依赖

<!--导入java-mail-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
  1. 写mail配置
#加密后的密码,从qq邮箱开启POP3/SMTPreceived in service
spring.mail.password=fiqktykwcdpybdjc
[email protected]
spring.mail.host=smtp.qq.com
# 开启加密验证(QQEmail has)
spring.mail.properties.mail.smtp.ssl.enable=true
  1. Write the implementation function
@SpringBootTest
class SpringbootTestApplicationTests {

@Autowired
JavaMailSenderImpl mailSender;
@Test
void contextLoads() {

//一个简单的邮件发送
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setSubject("小狂神你好呀~"); //发送主题(标题)
mailMessage.setText("谢谢你"); //发送内容
mailMessage.setTo("[email protected]"); //发送给谁
mailMessage.setFrom("[email protected]"); //谁发送
mailSender.send(mailMessage);
}
@Test
void contextLoads2() throws MessagingException {

//一个复杂的邮件发送
MimeMessage mimeMessage = mailSender.createMimeMessage();
//组装~
//正文
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setSubject("小狂神你好呀~plus");
helper.setText("<p style='color:red'>Thanks for your course~</p>",true);
//附件
helper.addAttachment("3.jpg",new File("D:\\我的文件\\我的素材\\图片\\3.jpg"));
helper.addAttachment("1.jpeg",new File("D:\\我的文件\\我的素材\\图片\\1.jpeg"));
helper.setTo("[email protected]"); //发送给谁
helper.setFrom("[email protected]"); //谁发送
mailSender.send(mimeMessage);
}

Then you can receive the email~

3、定时执行任务

  1. Add an annotation to the startup program first
package com.qian;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableAsync //开启异步注解功能
@EnableScheduling // 开启定时功能的注解
@SpringBootApplication
public class SpringbootTestApplication {

public static void main(String[] args) {

SpringApplication.run(SpringbootTestApplication.class, args);
}
}

@EnableScheduling // 开启定时功能的注解

  1. Write a timer program
package com.qian.service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
public class ScheduledService {

//在一个特定的时间执行这个方法! Timer
//cron表达式
//秒 分 时 日 月 Week to week 0-7It means every day from Sunday to Sunday
//0 15 10 ? * 1-6 每个月的周一到周六的10:15执行
@Scheduled(cron = "0/2 * * * * ?") //每天的15:24:00执行
public void hello(){

System.out.println("hello,你被执行了");
}
}

**@Scheduled(cron = “0/2 * * * * ?”) ** cron表达式

copyright:author[Zero degrees Celsius],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/218/202208061619191296.html