當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot高级-任务-异步任务
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot高级-任务-异步任务
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
SpringBoot與任務,SpringBoot中如何來執行異步任務,包括定時任務,還有發送郵件的任務,我們先來測試異步任務,創建一個Service,異步任務我們開發中還是非常常見的,比如我們在發送郵件,或者處理一些數據,我們不想讓他阻塞下面的線程,那我們就可以用多線程的方式進行異步處理,比如我這里有一個hello方法,如果我們在同步等待的情況下,我們給他睡上3秒,那我們要發一個請求,來調用這個方法,我們就需要比較漫長的等待,我們來寫一個AsyncController,@RestController,我們來發一個hello請求,@GetMapping來映射上一個hello請求,我們來調用這個service,我們來自動注入,調用這個service,由于它會睡3秒的時間,那么我們要return出去的success,用戶3秒以后才會響應到,我們來運行一下,而我們不想讓用戶在3秒以后得到響應,這個方法可以寫成多線程的方式給他運行,但是如果自己來編寫,太麻煩了,我們先來看同步的效果\localhost:8080/hello我們發現他在這里轉,大約3秒以后才會有響應
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn</groupId><artifactId>springboot-04-task</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>springboot-04-task</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.12.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
package com.learn.service;import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;@Service
public class AsyncService {// 告訴Spring這是一個異步方法@Asyncpublic void hello() {try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("處理數據中....");}
}
package com.learn.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import com.learn.service.AsyncService;@RestController
public class AsyncController {@AutowiredAsyncService asyncService;@GetMapping("/hello")public String helloa() {asyncService.hello();return "success";}
}
package com.learn;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class SpringBootTaskApplication {public static void main(String[] args) {// Spring應用啟動起來SpringApplication.run(SpringBootTaskApplication.class,args);}}
如果我們手動的改成多線程調用,每次編碼太麻煩,我們只需要一個簡單的辦法,我們在這個方法上標上一個注解,@Async,說明這個方法是一個異步的,告訴Spring這是一個異步方法,那么Spring就會開一個線程池,進行調用,但這個注解要能起作用,我們就得再來加一個注解@Asyncpublic void hello() {try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("處理數據中....");}叫@EnableAsync,開啟異步注解,功能,有了他以后,我們再來重啟,我們來重新啟動,我們看一下是不是還用等3秒的時間,我們發現這里會有立即響應,處理數據也會被調用,這就是我們的異步任務,非常方便,只需要一個注解
package com.learn;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;// 開啟異步注解功能
@EnableAsync
@SpringBootApplication
public class SpringBootTaskApplication {public static void main(String[] args) {// Spring應用啟動起來SpringApplication.run(SpringBootTaskApplication.class,args);}}
?
總結
以上是生活随笔為你收集整理的SpringBoot高级-任务-异步任务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot-高级-检索-整合S
- 下一篇: SpringBoot高级-任务-定时任务