Asynchronous tasks
1. Build
Create an asynchronous processing class AsynService
package com.gip.service;
import org.springframework.stereotype.Service;
@Service
public class AsynService {
public void hello(a) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Data is being processed."); }}Copy the code
Use the thread.sleep (3000); Simulation process
And then we call this class real column method hello() in the Controller
AsynController
package com.gip.Controller;
import com.gip.service.AsynService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AsynController {
@Autowired
AsynService asynService;
@GetMapping("/hello")
public String hell0(a) {
asynService.hello();
return "hello"; }}Copy the code
Start the project and see what happens
The browser waits 3 seconds for a response
Before returning the Hello string
2. Enable asynchronous annotations
Add @async to the hello method door of the asynchronous class
Then in the asynchronous support SpringbootTestApplication main start up on the class
What functions should be enabled? @enable+…. function
Restart the project to run, discover and do not stall, and asynchronous processing is complete
The mail task
1. Add dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
Copy the code
Double-click Shift to find the source code for the mail configuration
It’s pretty clear here
You can configure the following properties
2. Start the configuration
First go to your email to enable this feature
Then configure it in application.properties
3. The test
Write a simple message in the test class
package com.gip;
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.scheduling.annotation.Async;
@SpringBootTest
class SpringbootTestApplicationTests {
@Autowired
JavaMailSenderImpl mailSender;
@Test
void contextLoads(a) {
SimpleMailMessage simpleMailMessage=new SimpleMailMessage();
simpleMailMessage.setSubject("This is my test send email.");
simpleMailMessage.setText("Here's the text.");
simpleMailMessage.setFrom("[email protected]");
simpleMailMessage.setTo("[email protected]"); mailSender.send(simpleMailMessage); }}Copy the code
First inject an instance of the implementation class
Then define the simple message
SimpleMailMessage simpleMailMessage=new SimpleMailMessage();
simpleMailMessage.setSubject("This is my test send email.");
simpleMailMessage.setText("Here's the text.");
// From where, the sender
simpleMailMessage.setFrom("[email protected]");
// To whom, the receiver
simpleMailMessage.setTo("[email protected]");
mailSender.send(simpleMailMessage);
Copy the code
Send with the mailsender. send method
The test results are as follows:
Create a new complex message
@Test
void contextLoads2(a) throws MessagingException {
// Create a complex message
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setSubject("This is my test send email 222");
helper.setText(
.true);
helper.setFrom("[email protected]");
helper.setTo("[email protected]");
/ / accessories
helper.addAttachment("01.jpg".new File("F:\\ desktop wallpaper \\01.jpg"));
mailSender.send(mimeMessage);
}
Copy the code
Note: idea method shortcut key: Ctrl+P ‘parameter prompt
Timing task
Add the @enablesCheduling annotation to the startup class
Create ScheduledService class
package com.gip.service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
public class ScheduledService {
// A Cron expression is a string separated by five or six Spaces
// second (0~59)
// min (0~59)
// hour (0~23)
// Day (day and month) (0 to 31, but you need to consider the number of days in your month)
// month (0~11)
// day (week) (1~7 1=SUN or SUN, MON, TUE, WED, THU, FRI, SAT)
// Year (1970-2099)
@Scheduled(cron = "* * * * * ?" )
public void hello(a){
System.out.println("Scheduled task enabled"); }}Copy the code
Note: Spring Task does not support up to years, so you can only cron expressions up to 6 bits
? It can only be used for dates and days. You can also use * instead
Timer function is not powerful, and after running error, the life cycle ends, can not be executed in the future. So you don’t use Java Timer, you use Quartz