zoukankan      html  css  js  c++  java
  • 框架 Spring Boot 技术入门到整合 11-1 Springboot整合异步任务以及使用场景

    0    课程地址

    https://www.imooc.com/video/16793/0

    1    课程重点
    1.0  异步任务和同步对比

    异步取异步任务时间最大值,同步取同步任务累计值

    1.1  异步任务使用场景

    ◆发送短信

    ◆发送邮件

    ◆App消息推送

    ◆节省运维凌晨发布任务时间提供效率

    1.2  异步任务使用注意事项

    顶类开启异步任务开关@EnableAsync

    任务类使用@Component @Async作为组件被扫描执行

    1.3  提高效率(和异步任务类似操作)

    多线程、消息队列、kfka、*MQ等等

    1    课程重点
    2.1  使用同步任务demo对比

    顶类:(同下)

    触发类:(同下)

    任务类:

    package com.example.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
    public class AsyncTask {
        
        //@Async
        public Future<Boolean> doTask11() throws Exception {
            long start = System.currentTimeMillis();
            Thread.sleep(1000);
            long end = System.currentTimeMillis();
            System.out.println("任务1耗时:" + (end - start) + "毫秒");
            return new AsyncResult<>(true);
        }
        
        //@Async
        public Future<Boolean> doTask22() throws Exception {
            long start = System.currentTimeMillis();
            Thread.sleep(700);
            long end = System.currentTimeMillis();
            System.out.println("任务2耗时:" + (end - start) + "毫秒");
            return new AsyncResult<>(true);
        }
        
        //@Async
        public Future<Boolean> doTask33() throws Exception {
            long start = System.currentTimeMillis();
            Thread.sleep(600);
            long end = System.currentTimeMillis();
            System.out.println("任务3耗时:" + (end - start) + "毫秒");
            return new AsyncResult<>(true); 
        }
    }

    测试结果:

    2.2  使用异步任务demo对比

    顶类:

    package com.example.demo;
    
    import org.springframework.scheduling.annotation.EnableAsync;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import tk.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.ComponentScan;
    
    @SpringBootApplication
    
    //扫描mybaties mapper包路径
    @MapperScan(basePackages = "com.example.demo.mapper")
    
    //扫描 所需要的包,包含自用的工具类包所在路径
    @ComponentScan(basePackages={"com.example.demo","org.n3r.idworker"})
    
    //开启定时任务
    //@EnableScheduling
    
    //开启异步调用方法
    @EnableAsync
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
    }

    触发类:

    package com.example.demo.task;
    
    import java.util.concurrent.Future;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("tasks")
    public class DoTask {
        
        @Autowired
        private AsyncTask asyncTask;
        
        @RequestMapping("test1")
        public String test1() throws Exception {
            
            long start = System.currentTimeMillis();
            
            Future<Boolean> a = asyncTask.doTask11();
            Future<Boolean> b = asyncTask.doTask22();
            Future<Boolean> c = asyncTask.doTask33();
            
            while (!a.isDone() || !b.isDone() || !c.isDone()) {
                if (a.isDone() && b.isDone() && c.isDone()) {
                    break;
                }
            }
            
            long end = System.currentTimeMillis();
            
            String times = "任务全部完成,总耗时:" + (end - start) + "毫秒";
            System.out.println(times);
            
            return times;
        }
    }

    任务类:

    package com.example.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
    public class AsyncTask {
        
        @Async
        public Future<Boolean> doTask11() throws Exception {
            long start = System.currentTimeMillis();
            Thread.sleep(1000);
            long end = System.currentTimeMillis();
            System.out.println("任务1耗时:" + (end - start) + "毫秒");
            return new AsyncResult<>(true);
        }
        
        @Async
        public Future<Boolean> doTask22() throws Exception {
            long start = System.currentTimeMillis();
            Thread.sleep(700);
            long end = System.currentTimeMillis();
            System.out.println("任务2耗时:" + (end - start) + "毫秒");
            return new AsyncResult<>(true);
        }
        
        @Async
        public Future<Boolean> doTask33() throws Exception {
            long start = System.currentTimeMillis();
            Thread.sleep(600);
            long end = System.currentTimeMillis();
            System.out.println("任务3耗时:" + (end - start) + "毫秒");
            return new AsyncResult<>(true); 
        }
    }

    测试结果:

  • 相关阅读:
    Android内存优化5 了解java GC 垃圾回收机制3
    一起刑事案件法庭辩护 z
    辩护技巧总结——律师在刑事辩护中应注意的几个问题 z
    证据对抗、证据链标准 z
    里德九步审讯法 z
    WCF服务在高并发情况下报目标积极拒绝的异常处理 z
    C#EasyHook例子C# Hook 指定进程C#注入指定进程 z
    玄机论坛Socket类库源码 当前版本 2.6.3 更新日期:10-09/2015 z
    庭审全程文字实录 z
    庭审精彩语录整理 z
  • 原文地址:https://www.cnblogs.com/1446358788-qq/p/14195482.html
Copyright © 2011-2022 走看看