One, asynchronous processing
@Async
: Written before the service method, it means that this method needs to be processed asynchronously@EnableAsync
: Write before the spring boot startup class to enable the function of asynchronous processing
AsynService:
@Service
public class AsynService {
@Async
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("数据处理中");
}
}
AsynController:
@RestController
public class AsynController {
@Autowired
AsynService asynService;
@GetMapping("/hello")
public String hello(){
asynService.hello();
return "success";
}
}
SpringBootTaskApplication:
@EnableAsync
@SpringBootApplication
public class SpringBootTaskApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootTaskApplication.class, args);
}
}
Originally, there was no + asynchronous processing. Every time you visit http://localhost:8080/hello
, you have to wait 3 seconds for the result. Added this, the result in seconds
Two, timed tasks
@EnableScheduling
: Before writing in the spring boot startup class, turn on the timing task function@Scheduled
: Used before the method in the service class, used to set up timing tasks@Scheduled(cron = "xxx")
This xxx has grammatical rules- The attribute value of cron is specified as a string. This string specifies 6 times, which areSeconds, minutes, hours, day of month, month, day of week, Each time period is separated by a space. For example
"0 * * * * 1-7"
, it means that during any time period from Monday to Sunday, when the second is 0, the function will be executed once
2. Some examples
@Scheduled(cron = "0,1,2,3,4 * * * * 1-5")
: From Monday to Friday, when the second is 0, 1, 2, 3, 4, execute the code once@Scheduled(cron = "0-5 * * * * 1,2,3,4,5")
: Same as above@Scheduled(cron = "0/4 * * * * 1,2,3,4,5")
: From Monday to Friday, when the second is 0, it will be executed every 4 seconds0 0/5 14,18 ? * ?
: Every day at 14 o'clock, 18 o'clock, once every 5 minutes0 0 2 ? * 6L
: Executed at 2 AM on the last Saturday of every month
Three, send email
Spring boot comes with the function of sending mailboxes, I didn't expect it
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
1. Simple mail
application.yml :
spring:
mail:
username: [email protected]
password: xxxxxxxxxxxxxxx(这个密码,下面有说是什么)
host: smtp.qq.com
profiles:
mail:
smtp:
ssl:
enable: true
- The password is not your QQ password, but
this authorization code
Test in the test class:
@SpringBootTest
class SpringBootTaskApplicationTests {
@Autowired
JavaMailSenderImpl mailSender;
//简单的发送邮件
@Test
void contextLoads() {
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject("今晚打老虎");//发送邮件标题
message.setText("多谢乌蝇哥");//正文
message.setTo("[email protected]");//发给谁
message.setFrom("[email protected]");//发送者
this.mailSender.send(message);
}
}
2. Complex mail
//复杂的邮件
@Test
public void test02() throws Exception {
MimeMessage mimeMessage = this.mailSender.createMimeMessage();//要用MimeMessage类型
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);//然后用helper操作
helper.setSubject("今晚打老虎");//标题
helper.setText("<b style='color:red'>多谢乌蝇哥</b>", true);//内容,第二个参数为true就开启html格式
helper.setTo("[email protected]");//发给谁
helper.setFrom("[email protected]");//发送者
helper.addAttachment("1.jpg", new File("D:\\壁纸\\59e06a3543101.png"));
helper.addAttachment("2.jpg", new File("D:\\壁纸\\336-160G2110S6.png"));
this.mailSender.send(mimeMessage);
}