网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

怎么在SpringBoot中实现一个异步任务

怎么在SpringBoot中实现一个异步任务?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

创新互联公司服务项目包括秭归网站建设、秭归网站制作、秭归网页制作以及秭归网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,秭归网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到秭归省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

步骤,如图所示:

怎么在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 task4() throws InterruptedException {
    long begin = System.currentTimeMillis();
    Thread.sleep(1000);
    long end = System.currentTimeMillis();
    System.out.println("任务4耗时:" + (end - begin));
    return new AsyncResult("任务4");
  }
  
  public Future task5() throws InterruptedException {
    long begin = System.currentTimeMillis();
    Thread.sleep(2000);
    long end = System.currentTimeMillis();
    System.out.println("任务5耗时:" + (end - begin));
    return new AsyncResult("任务5");
  }
  
  public Future task6() throws InterruptedException {
    long begin = System.currentTimeMillis();
    Thread.sleep(3000);
    long end = System.currentTimeMillis();
    System.out.println("任务6耗时:" + (end - begin));
    return new AsyncResult("任务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 result1 = asyncTask.task4();
    Future result2 = asyncTask.task5();
    Future 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中实现一个异步任务

看完上述内容,你们掌握怎么在SpringBoot中实现一个异步任务的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


网站栏目:怎么在SpringBoot中实现一个异步任务
网站网址:http://bjjierui.cn/article/jsehis.html

其他资讯