Spring Boot入门系列(九)如何实现异步执行任务

前面介绍了Spring Boot 如何整合定时任务,不清楚的朋友能够看看以前的文章:https://www.cnblogs.com/zhangweizhong/category/1657780.htmlhtml

今天主要讲解Spring Boot中的另一个任务:异步任务。所谓异步任务,其实就是异步执行程序,有些时候遇到一些耗时的的任务,若是一直卡等待,确定会影响其余程序的执行,因此就让这些程序须要以异步的方式去执行。那么下面就来介绍Spring Boot 如何实现异步任务。java

 

1、使用注解@EnableAsync 开启异步调用方法

在application启动类中,加上@EnableAsync注解,Spring Boot 会自动扫描异步任务。web

复制代码
package com.weiz;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication
//扫描 mybatis mapper 包路径
@MapperScan(basePackages = "com.weiz.mapper")
//扫描 全部须要的包, 包含一些自用的工具类包 所在的路径
@ComponentScan(basePackages = {"com.weiz","org.n3r.idworker"})
//开启定时任务
@EnableScheduling
//开启异步调用方法
@EnableAsync
public class SpringBootStarterApplication {
public static void main(String[] args) { SpringApplication.run(SpringBootStarterApplication.class, args); } }
复制代码

 

2、建立异步执行类,定义@Component及@Async组件

建立com.weiz.tasks包,在tasks包里增长AsyncTask 异步任务类,加上@Component 注解,而后在须要异步执行的方法前面加上@Async注解,这样Spring Boot容器扫描到相关异步方法以后,调用时就会将这些方法异步执行。spring

package com.weiz.tasks;

import java.util.concurrent.Future;

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

@Component
public class AsyncTask {
    
    @Async
    public Future<Boolean> doTask11() throws Exception {
        long start = System.currentTimeMillis();
        Thread.sleep(1000);
        long end = System.currentTimeMillis();
        System.out.println("任务1耗时:" + (end - start) + "毫秒");
        return new AsyncResult<>(true);
    }
    
    @Async
    public Future<Boolean> doTask22() throws Exception {
        long start = System.currentTimeMillis();
        Thread.sleep(700);
        long end = System.currentTimeMillis();
        System.out.println("任务2耗时:" + (end - start) + "毫秒");
        return new AsyncResult<>(true);
    }
    
    @Async
    public Future<Boolean> doTask33() throws Exception {
        long start = System.currentTimeMillis();
        Thread.sleep(600);
        long end = System.currentTimeMillis();
        System.out.println("任务3耗时:" + (end - start) + "毫秒");
        return new AsyncResult<>(true); 
    }
}

说明:@Async 加上这个注解,就表示该方法是异步执行方法。浏览器

 

3、调用

建立一个DoTask调用类,咱们看看这几个方法,是怎么执行的:springboot

package com.weiz.tasks;

import java.util.concurrent.Future;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("tasks")
public class DoTask {

    @Autowired
    private AsyncTask asyncTask;

    @RequestMapping("test1")
    public String test1() throws Exception {

        long start = System.currentTimeMillis();

        Future<Boolean> a = asyncTask.doTask11();
        Future<Boolean> b = asyncTask.doTask22();
        Future<Boolean> c = asyncTask.doTask33();

        while (!a.isDone() || !b.isDone() || !c.isDone()) {
            if (a.isDone() && b.isDone() && c.isDone()) {
                break;
            }
        }

        long end = System.currentTimeMillis();

        String times = "任务所有完成,总耗时:" + (end - start) + "毫秒";
        System.out.println(times);

        return times;
    }
}

 

4、测试

启动程序以后,在浏览器输入:http://localhost:8080/tasks/test1  。微信

 

从这个总耗时能够看出,三个方法确实是异步执行的。耗时接近时间最长的doTask11方法。mybatis

 

最后

以上,就把Spring Boot 建立异步任务的方法简单介绍完了,是否是特别简单。架构

这个系列课程的完整源码,也会提供给你们。你们关注个人微信公众号(架构师精进),回复:springboot源码。获取这个系列课程的完整源码。app

相关文章
相关标签/搜索