崛起于Springboot2.X + 100秒掌握异步任务(55)

《SpringBoot2.X心法总纲》app

一、应用场景

      若是以为消息队列太繁琐了,对于异步发送短信、邮件、app消息推送,等等均可以使用简单的异步来解决。异步

二、异步配置

      Springboot启动类添加注解以下,表示开启,会自动进行扫描。async

@EnableAsync

      异步任务类添加注解测试

@Async
@Component

三、编写异步Task

@Async
@Component
public class AsyncTask {

    public Future<String> task1() throws InterruptedException{
        long begin = System.currentTimeMillis();
        Thread.sleep(1000L);
        long end = System.currentTimeMillis();

        System.out.println("异步任务async1:耗时="+(end-begin));

        return new AsyncResult<String>("任务1");
    }

    public Future<String> task2() throws InterruptedException{
        long begin = System.currentTimeMillis();
        Thread.sleep(2000L);
        long end = System.currentTimeMillis();

        System.out.println("异步任务async2:耗时="+(end-begin));

        return new AsyncResult<String>("任务2");
    }

    public Future<String> task3() throws InterruptedException{
        long begin = System.currentTimeMillis();
        Thread.sleep(3000L);
        long end = System.currentTimeMillis();

        System.out.println("异步任务async3:耗时="+(end-begin));

        return new AsyncResult<String>("任务3");
    }
}

四、编写同步Task

@Component
public class SyncTask {

    public Boolean task1() throws InterruptedException{
        long begin = System.currentTimeMillis();
        Thread.sleep(1000L);
        long end = System.currentTimeMillis();

        System.out.println("同步任务sync1:耗时="+(end-begin));

        return true;
    }

    public Boolean task2() throws InterruptedException{
        long begin = System.currentTimeMillis();
        Thread.sleep(2000L);
        long end = System.currentTimeMillis();

        System.out.println("同步任务sync2:耗时="+(end-begin));

        return true;
    }

    public boolean task3() throws InterruptedException {
        long begin = System.currentTimeMillis();
        Thread.sleep(3000L);
        long end = System.currentTimeMillis();

        System.out.println("同步任务sync3:耗时="+(end-begin));

        return true;
    }
}

五、controller

@RestController
public class TestController {

    @Autowired
    AsyncTask asyncTask;

    @Autowired
    SyncTask syncTask;

    @GetMapping(value = "/testTask")
    public String testTask(int type) throws InterruptedException {

        // type:一、异步任务测试 二、同步任务测试
        long begin = System.currentTimeMillis();
        if (type == 1) {
            Future<String> async1 = asyncTask.task1();
            Future<String> async2 = asyncTask.task2();
            Future<String> async3 = asyncTask.task3();

            //若是都执行往就能够跳出循环,isDone方法若是此任务完成,true
            while (true){
                if (async1.isDone() && async2.isDone() && async3.isDone()) {
                    break;
                }
            }
        } else {
            boolean sync1 = syncTask.task1();
            boolean sync2 = syncTask.task2();
            boolean sync3 = syncTask.task3();
            //若是都执行往就能够跳出循环,isDone方法若是此任务完成,true
            while (true){
                if (sync1 && sync2 && sync3) {
                    break;
                }
            }
        }
        long end = System.currentTimeMillis();

        long total = end-begin;
        return "执行总耗时="+total;

    }
}

六、测试接口

异步任务测试:http://localhost:8080/testTask?type=1 spa

结果:.net

同步任务测试:http://localhost:8080/testTask?type=2blog

结果:接口

七、总结

      综上所看,异步为3秒左右,同步为6秒,故此异步任务成功。异步任务须要用Future<String>返回结果AsyncResult<String>("")。队列

相关文章
相关标签/搜索