SpringBoot异步任务使用方法详解
作者:玉天恒 发布时间:2021-08-07 07:57:02
标签:Spring,Boot,异步,任务
步骤,如图所示:
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
结果:
来源:https://www.cnblogs.com/tianhengblogs/p/9824271.html
0
投稿
猜你喜欢
- 项目里使用了Feign进行远程调用,有时为了问题排查,需要开启请求和响应日志下面简介一下如何开启Feign日志:注:本文基于spring-b
- 一、获取接口请求的数据可以在Interceptor的afterCompletion中实现但是要重写RequestWrapper代码记录如下:
- 这篇文章主要介绍了Java方法参数传递机制原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可
- 官网教程一、翻转(镜像)头文件 quick_opencv.h:声明类与公共函数#pragma once#include <opencv
- 一、题目描述题目实现:网络通信,实现信息的发送和接收。二、解题思路创建一个服务器类:ServerSocketFrame,继承JFrame类写
- 1 概念方面List是接口,ArrayList是List接口的一个实现类2 初始化方面2.1 List2.1.1 错误写 * ist list
- 本文介绍了spring整合JMS实现同步收发消息(基于ActiveMQ的实现),分享给大家,具体如下:1. 安装ActiveMQ注意:JDK
- 一、概要1.Java虚拟机(Jvm)是什么?2.Java虚拟机是用来干什么的?3.Java虚拟机它的体系结构是什么样子的?4.Java虚拟机
- 先看下效果:两种需求场景:1.广告页3s后跳转到首页2.短信验证码60s倒计时第一种的话,根据需求我们可以知道,我们想要的效果就是3s结束做
- 一、Monkey 是什么?Monkey 就是SDK中附带的一个工具。二、Monkey 测试的目的?:该工具用于进行压力测试。 然后开发人员结
- 1.编写核心类MainApp:package com.yiidian.gson;import com.google.gson.Gson;im
- SpringBoot的web项目,在每一次修改了java文件或者是resource的时候,都必须去重启一下项目,这样的话浪费了很多的时间,实
- 下截JNative组件jnative.sourceforge.net/ 到这里下载JNative开源项目,我下载的是1.3.2解压JNati
- 引言设计: 嗯? 这个图片点击跳转进详情再返回图片怎么变白闪一下呢?产品: 是啊是啊! 一定是个bug开发: 囧囧囧在开发过程中, 也许你也
- 很多核心 Java 面试题来源于多线程(Multi-Threading)和集合框架(Collections Framework),理解核心线
- 本文提纲版本约定JDK:8Servlet:4.xtomcat:9.x✍正文什么样的答案终身难忘?学生时代关于记忆经常能听见两种论调:死记硬背
- 项目背景我们开发过程中会碰到这样一类问题,就是数据层或三方接口返回的Bean对象需要转换重新装换一下我们需要的对象。我们通常的做法就是通过g
- 本文实例讲述了Java编程实现获取当前代码行行号的方法。分享给大家供大家参考,具体如下:最近的项目中,为了实现自定义的log类,能够输出具体
- 在java中我们常常使用加锁机制来确保线程安全,但是如果过度使用加锁,则可能导致锁顺序死锁。同样,我们使用线程池和信号量来限制对资源的使用,
- 1.启动项目的时候报错1.Error starting ApplicationContext. To display the auto-co