SpringBoot异步任务使用方法详解

作者:玉天恒 时间:2021-08-07 07:57:02 

步骤,如图所示:

SpringBoot异步任务使用方法详解

1.添加异步任务业务类


package top.ytheng.demo.task;

import java.util.concurrent.Future;

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

//异步任务业务类
@Component
//标记此类是异步类,也可在方法中标记
//不加,则类里面的方法为同步执行
@Async
public class AsyncTask {

public void task1() throws InterruptedException {
   long begin = System.currentTimeMillis();
   Thread.sleep(1000);
   long end = System.currentTimeMillis();
   System.out.println("任务1耗时:" + (end - begin));
 }

public void task2() throws InterruptedException {
   long begin = System.currentTimeMillis();
   Thread.sleep(2000);
   long end = System.currentTimeMillis();
   System.out.println("任务2耗时:" + (end - begin));
 }

public void task3() throws InterruptedException {
   long begin = System.currentTimeMillis();
   Thread.sleep(3000);
   long end = System.currentTimeMillis();
   System.out.println("任务3耗时:" + (end - begin));
 }

//测试拿到返回结果
 public Future<String> task4() throws InterruptedException {
   long begin = System.currentTimeMillis();
   Thread.sleep(1000);
   long end = System.currentTimeMillis();
   System.out.println("任务4耗时:" + (end - begin));
   return new AsyncResult<String>("任务4");
 }

public Future<String> task5() throws InterruptedException {
   long begin = System.currentTimeMillis();
   Thread.sleep(2000);
   long end = System.currentTimeMillis();
   System.out.println("任务5耗时:" + (end - begin));
   return new AsyncResult<String>("任务5");
 }

public Future<String> task6() throws InterruptedException {
   long begin = System.currentTimeMillis();
   Thread.sleep(3000);
   long end = System.currentTimeMillis();
   System.out.println("任务6耗时:" + (end - begin));
   return new AsyncResult<String>("任务6");
 }
}

2.添加测试控制器


package top.ytheng.demo.controller;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

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

import top.ytheng.demo.task.AsyncTask;

@RestController
@RequestMapping("api/v1/async")
public class TaskController {

@Autowired
 private AsyncTask asyncTask;

@GetMapping("/test")
 public Object test() throws InterruptedException, ExecutionException {
   long begin = System.currentTimeMillis();
   //asyncTask.task1();
   //asyncTask.task2();
   //asyncTask.task3();
   Future<String> result1 = asyncTask.task4();
   Future<String> result2 = asyncTask.task5();
   Future<String> result3 = asyncTask.task6();
   System.out.println("返回结果:" + result1.get() + "," + result2.get() + "," + result3.get());
   for(;;) {
     if(result1.isDone() && result2.isDone() && result3.isDone()) {
       break;
     }
   }
   long end = System.currentTimeMillis();
   long total = end - begin;
   System.out.println("总耗时:" + total);
   return "总耗时:" + total;
 }
}

3.添加启动类


package top.ytheng.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication //等于下面3个
//@SpringBootConfiguration
//@EnableAutoConfiguration
//@ComponentScan
// * 用到
@ServletComponentScan
//MyBatis用到
@MapperScan("top.ytheng.demo.mapper")
//定时使用(开启定时任务)
@EnableScheduling
//开启异步任务
@EnableAsync
public class DemoApplication {

public static void main(String[] args) {
   SpringApplication.run(DemoApplication.class, args);
 }
}

4.右键项目Run As启动,访问url


http://localhost:8080/api/v1/async/test

结果:

SpringBoot异步任务使用方法详解

来源:https://www.cnblogs.com/tianhengblogs/p/9824271.html

标签:Spring,Boot,异步,任务
0
投稿

猜你喜欢

  • SpringBoot如何使用RateLimiter通过AOP方式进行限流

    2023-09-16 18:18:44
  • Java使用sftp定时下载文件的示例代码

    2022-11-14 06:11:47
  • Springboot WebJar打包及使用实现流程解析

    2023-06-21 22:08:00
  • 解析Java内存分配和回收策略以及MinorGC、MajorGC、FullGC

    2023-02-06 08:22:19
  • Java基础之二叉搜索树的基本操作

    2023-07-08 10:07:07
  • Java字节码ByteBuddy使用及原理解析上

    2023-08-23 19:33:05
  • Mybatis与Jpa的区别和性能对比总结

    2022-09-17 16:08:45
  • Java进阶:Struts多模块的技巧

    2023-06-18 09:40:47
  • Java日期与时间类原理解析

    2021-07-20 14:00:36
  • JSON.toJSONString()空字段不忽略修改的问题

    2023-06-16 03:12:37
  • idea的spring boot项目实现更改端口号操作

    2023-11-23 03:21:17
  • SpringMVC的执行过程浅析

    2021-05-31 20:51:11
  • Java实现去除文档阴影的示例代码

    2023-08-31 11:45:48
  • Java实现的Base64加密算法示例

    2023-10-29 00:37:08
  • 基于jni调用时,jvm报错问题的深入分析

    2022-08-08 17:21:55
  • Android Studio多渠道打包的配置方法

    2023-06-15 23:19:48
  • Java如何提供给第三方使用接口方法详解

    2022-07-22 19:59:19
  • 深入理解Java8新特性之接口中的默认方法和静态方法

    2023-11-24 01:44:25
  • SpringBoot之如何指定配置文件启动

    2023-11-17 15:17:48
  • java去除字符串中的空格、回车、换行符、制表符的小例子

    2022-04-13 12:33:44
  • asp之家 软件编程 m.aspxhome.com