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

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

步骤,如图所示:

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

1.添加异步任务业务类

packagetop.ytheng.demo.task;

importjava.util.concurrent.Future;

importorg.springframework.scheduling.annotation.Async;
importorg.springframework.scheduling.annotation.AsyncResult;
importorg.springframework.stereotype.Component;

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

publicvoidtask1()throwsInterruptedException{
longbegin=System.currentTimeMillis();
Thread.sleep(1000);
longend=System.currentTimeMillis();
System.out.println("任务1耗时:"+(end-begin));
}

publicvoidtask2()throwsInterruptedException{
longbegin=System.currentTimeMillis();
Thread.sleep(2000);
longend=System.currentTimeMillis();
System.out.println("任务2耗时:"+(end-begin));
}

publicvoidtask3()throwsInterruptedException{
longbegin=System.currentTimeMillis();
Thread.sleep(3000);
longend=System.currentTimeMillis();
System.out.println("任务3耗时:"+(end-begin));
}

//测试拿到返回结果
publicFuture<String>task4()throwsInterruptedException{
longbegin=System.currentTimeMillis();
Thread.sleep(1000);
longend=System.currentTimeMillis();
System.out.println("任务4耗时:"+(end-begin));
returnnewAsyncResult<String>("任务4");
}

publicFuture<String>task5()throwsInterruptedException{
longbegin=System.currentTimeMillis();
Thread.sleep(2000);
longend=System.currentTimeMillis();
System.out.println("任务5耗时:"+(end-begin));
returnnewAsyncResult<String>("任务5");
}

publicFuture<String>task6()throwsInterruptedException{
longbegin=System.currentTimeMillis();
Thread.sleep(3000);
longend=System.currentTimeMillis();
System.out.println("任务6耗时:"+(end-begin));
returnnewAsyncResult<String>("任务6");
}
}

2.添加测试控制器

packagetop.ytheng.demo.controller;

importjava.util.concurrent.ExecutionException;
importjava.util.concurrent.Future;

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

importtop.ytheng.demo.task.AsyncTask;

@RestController
@RequestMapping("api/v1/async")
publicclassTaskController{

@Autowired
privateAsyncTaskasyncTask;

@GetMapping("/test")
publicObjecttest()throwsInterruptedException,ExecutionException{
longbegin=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;
}
}
longend=System.currentTimeMillis();
longtotal=end-begin;
System.out.println("总耗时:"+total);
return"总耗时:"+total;
}
}

3.添加启动类

packagetop.ytheng.demo;

importorg.mybatis.spring.annotation.MapperScan;
importorg.springframework.scheduling.annotation.EnableAsync;
importorg.springframework.scheduling.annotation.EnableScheduling;

importorg.springframework.boot.autoconfigure.SpringBootApplication;
importorg.springframework.boot.web.servlet.ServletComponentScan;

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

publicstaticvoidmain(String[]args){
SpringApplication.run(DemoApplication.class,args);
}
}

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

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

结果:

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

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

发布于 2021-03-21 22:39:06
收藏
分享
海报
0 条评论
164
上一篇:如何正确的使用django-csrf 下一篇:怎么在SpringBoot中使用logback日志框架
目录

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码