170719、springboot編程之異步調用@Async


1、在pom.xml中增加依賴

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

2、在主類上開啟注解

package com.rick;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@EnableAsync
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

3、新建任務測試類

package com.rick.task;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.concurrent.Future;

/**
 * Desc :  任務類
 * User : RICK
 * Time : 2017/8/29 9:56
  */

@Component
public class Task1 {

    public Random random = new Random();

    /**
     * Desc :  @Async所修飾的函數不要定義為static類型,否則異步調用不會生效
     *  這里通過返回Future<T>來返回異步調用的結果,實現異步回調
     * User : RICK
     * Time : 2017/8/29 10:30
      */

    //任務一
    @Async
    public Future<String> doTaskOne() throws Exception{
        System.out.println("開始做任務一");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("完成任務一,耗時:" + (end - start) + "毫秒");
        return new AsyncResult<>("test1 is done!");
    }

    //任務二;
    @Async
    public Future<String> doTaskTwo() throws Exception {
        System.out.println("開始做任務二");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("完成任務二,耗時:" + (end - start) + "毫秒");
        return new AsyncResult<>("test2 is done!");
    }

    //任務3;
    @Async
    public Future<String> doTaskThree() throws Exception {
        System.out.println("開始做任務三");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("完成任務三,耗時:" + (end - start) + "毫秒");
        return new AsyncResult<>("test3 is done!");
    }


}

4、創建測試控制器

package com.rick.controller;

import com.rick.task.Task1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Future;

@RestController
public class TaskController {

    @Autowired
    private Task1 task1;

    @RequestMapping("/task1")
    public String task1() throws  Exception{
        Future<String> test1 = task1.doTaskOne();
        Future<String> test2 = task1.doTaskTwo();
        Future<String> test3 = task1.doTaskThree();

        while (true){
            if(test1.isDone()){
                System.out.println("====================test1 is done=========================");
            }

            if(test2.isDone()){
                System.out.println("====================test2 is done=========================");
            }

            if(test3.isDone()){
                System.out.println("====================test3 is done=========================");
            }

            if(test1.isDone() && test2.isDone() && test3.isDone()){
                break;
            }
            Thread.sleep(1000);
        }

        return "task1";
    }



}

5、啟動項目測試http://localhost:8080/task1

項目代碼:https://github.com/zrbfree/spring-boot-async.git


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM