目錄
一、異步任務
在Java應用中,絕大多數情況下都是通過同步
的方式來實現交互處理的;但是在處理與第三方系統交互的時候,容易造成響應遲緩的情況,之前大部分都是使用多線程
來完成此類任務,其實,在Spring 3.x之後,就已經內置了@Async
來完美解決這個問題。
兩個註解:
@EnableAysnc
、@Aysnc
@RestController
public class AsyncController {
@Resource
private AsyncService asyncService;
@GetMapping("/hello")
public String hello(){
asyncService.hello();
return "success";
}
}
要開啓異步任務註解的功能,使用 @EnableAsync
@EnableAsync // 開啓異步註解功能
@SpringBootApplication
public class Springboot05TastApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot05TastApplication.class, args);
}
}
@Service
public class AsyncService {
@Async // 表示這是一個異步方法,再線程池中獲取一個線程來單獨執行該方法
public void hello() {
try {
Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("處理數據中..");
}
}
二、定時任務
跳轉到目錄
項目開發中經常需要執行一些定時任務,比如需要在每天凌晨時候,分析一次前一天的日誌信息。Spring爲我們提供了異步執行任務調度的方式,提供TaskExecutor
、TaskScheduler
接口。
兩個註解:
@EnableScheduling
、@Scheduled
1、cron表達式
second(秒) minute(分) hour(時) day of month(日) month(月) day of week(周幾)
下面的符號對應上面六個
0 * * * * MON-FRI 表示週五到週一之間,每月的週一到週五每時每分每0秒的時候會執行任務
例子:
【0 0/5 14,18 * * ?】 每天14點整,和18點整,每隔5分鐘執行一次
【0 15 10 ? * 1-6】 每個月的週一至週六10:15分執行一次
【0 0 2 ? * 6L】每個月的最後一個週六凌晨2點執行一次
【0 0 2 LW * ?】每個月的最後一個工作日凌晨2點執行一次
【0 0 2-4 ? * 1#1】每個月的第一個週一凌晨2點到4點期間,每個整點都執行一次;
2、測試
@EnableScheduling
: 開啓基於註解的定時任務功能
@EnableScheduling // 開啓基於註解的定時任務功能
@SpringBootApplication
public class Springboot05TastApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot05TastApplication.class, args);
}
}
@Service
public class ScheduledService {
//定時註解中需要傳入 cron表達式
//@Scheduled(cron = "0 * * * * MON-FRI")
//@Scheduled(cron = "1,2,3,4,5 * * * * MON-FRI") // 第1,2,3,4,5秒執行(範圍:週一到週五,每分鐘的前五秒)
//@Scheduled(cron = "1-5 * * * * MON-FRI") // 同上
@Scheduled(cron = "0/10 * * * * MON-SAT") // 每4秒執行一次
public void hello(){
System.out.println("hello..");
}
}
三、郵件任務
跳轉到目錄
使用步驟:
- 郵件發送需要引入spring-boot-starter-mail
- Spring Boot 自動配置
MailSenderAutoConfiguration
- 定義MailProperties內容,配置在application.properties中
- 自動裝配JavaMailSender
- 測試郵件發送
- 在全局配置文件application.properties中配置
其中spring.mail.password=xxx
是在QQ郵箱的設置->賬戶中生成的授權碼
;
- 測試郵件發送
@SpringBootTest
class Springboot05TastApplicationTests {
@Autowired
JavaMailSenderImpl mailSender;
@Test
void contextLoads() {
// 創建一個簡單的郵箱信息對象
SimpleMailMessage message = new SimpleMailMessage();
// 郵件設置
message.setSubject("通知-今晚開會"); //標題
message.setText("今晚7:30開會!"); //郵件內容
message.setTo("[email protected]"); //發給誰
message.setFrom("[email protected]"); //誰發的
mailSender.send(message);
}
@Test
void test2() throws Exception{
// 創建一個複雜的消息郵件(可以有樣式、附件等內容)
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
// 郵件設置
helper.setSubject("通知-今晚開會"); //標題
helper.setText("<b style='color:red'>今晚7:30開會!</b>", true); //郵件內容
helper.setTo("[email protected]"); //發給誰
helper.setFrom("[email protected]"); //誰發的
// 上傳文件
helper.addAttachment("1.jpg", new File("/Users/qw/Desktop/1.jpg"));
mailSender.send(mimeMessage);
}
}