zoukankan      html  css  js  c++  java
  • 【转】 springBoot(9)---定时任务,异步任务

    【转】 springBoot(9)---定时任务,异步任务

    定时任务,异步任务

    一、定时任务

    1、步骤:

             1:在启动类上写@EnableScheduling注解

             2:在要定时任务的类上写@component 

             3:在要定时执行的方法上写@Scheduled(fixedRate=毫秒数)。

    2、示例

        主类

    @SpringBootApplication
    @EnableScheduling //开启定时任务
    public class MainApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(MainApplication.class, args);
        }
    }

         定时任务类

    import java.util.Date;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    @Component
    public class Jobs {
      
        //表示方法执行完成后5秒
        @Scheduled(fixedDelay=5000)
        public void fixedDelayJob() throws InterruptedException{  
            System.out.println("fixedDelay 每隔5秒"+new Date());
        }
        
        //表示每隔3秒
        @Scheduled(fixedRate=3000)
        public void fixedRateJob(){
            
            System.out.println("fixedRate 每隔3秒"+new Date());
        }
    
        //表示每天8时30分0秒执行
        @Scheduled(cron="0 0,30 0,8 ? * ? ")
    public void cronJob(){ System.out.println(new Date()+" ...>>cron...."); } }

    效果:

     3.总结

      1.fixedDelayfixedRate,单位是毫秒,这里这里就是5秒和3秒,它们的区别就是

    fixedRate就是每多次分钟一次,不论你业务执行花费了多少时间。我都是1分钟执行1次,而fixedDelay是当任务执行完毕后1分钟在执行。所以根据实际业务不同,我们会选择不同的方式。

      2.cron表达式:比如你要设置每天什么时候执行,就可以用它

         cron表达式,有专门的语法,而且感觉有点绕人,不过简单来说,大家记住一些常用的用法即可,特殊的语法可以单独去查。
         cron一共有7位,但是最后一位是年,可以留空,所以我们可以写6位:

    * 第一位,表示秒,取值0-59
    * 第二位,表示分,取值0-59
    * 第三位,表示小时,取值0-23
    * 第四位,日期天/日,取值1-31
    * 第五位,日期月份,取值1-12
    * 第六位,星期,取值1-7,星期一,星期二...,注:不是第1周,第二周的意思
              另外:1表示星期天,2表示星期一。
    * 第7为,年份,可以留空,取值1970-2099

         cron中,还有一些特殊的符号,含义如下:

    (*)星号:可以理解为每的意思,每秒,每分,每天,每月,每年...
    (?)问号:问号只能出现在日期和星期这两个位置。
    (-)减号:表达一个范围,如在小时字段中使用“10-12”,则表示从10到12点,即10,11,12
    (,)逗号:表达一个列表值,如在星期字段中使用“1,2,4”,则表示星期一,星期二,星期四
    (/)斜杠:如:x/y,x是开始值,y是步长,比如在第一位(秒) 0/15就是,从0秒开始,每15秒,最后就是0,15,30,45,60    另:*/y,等同于0/y

          下面列举几个例子供大家来验证:

    0 0 3 * * ?     每天3点执行
    0 5 3 * * ?     每天3点5分执行
    0 5 3 ? * *     每天3点5分执行,与上面作用相同
    0 5/10 3 * * ?  每天3点的 5分,15分,25分,35分,45分,55分这几个时间点执行
    0 10 3 ? * 1    每周星期天,3点10分 执行,注:1表示星期天    
    0 10 3 ? * 1#3  每个月的第三个星期,星期天 执行,#号只能出现在星期的位置

    二、异步任务

       1、步骤

                 1: 启动类里面使用@EnableAsync注解开启功能,自动扫描

                 2:在要异步任务的类上写@component 

                 3:在定义异步任务类写@Async(写在类上代表整个类都是异步,在方法加上代表该类异步执行)

      2、示例

       主类

    @SpringBootApplication
    @EnableAsync   //开启异步任务
    public class MainApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(MainApplication.class, args);
        }
    }

      异步类

    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 Future<String> task4() throws InterruptedException{
            long begin = System.currentTimeMillis();
            Thread.sleep(2000L);
            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(3000L);
            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(1000L);
            long end = System.currentTimeMillis();
            System.out.println("任务6耗时="+(end-begin));
            return new AsyncResult<String>("任务6");
        }
            
    }

      controller类

    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 com.jincou.task.AsyncTask;
    import com.jincou.util.JsonData;
    
    @RestController
    @RequestMapping("/api/v1")
    public class UserController {
        
        @Autowired
        private AsyncTask task;
        
        @GetMapping("async_task")
        public JsonData exeTask() throws InterruptedException{
            
            long begin = System.currentTimeMillis();
            
            Future<String> task4 = task.task4();
            Future<String> task5 = task.task5();
            Future<String> task6 = task.task6();
            
            //如果都执行往就可以跳出循环,isDone方法如果此任务完成,true
            for(;;){
                if (task4.isDone() && task5.isDone() && task6.isDone()) {
                    break;
                }
            }
                    
            long end = System.currentTimeMillis();    
            long total = end-begin;
            System.out.println("执行总耗时="+total);
            return JsonData.buildSuccess(total);
        }    
    }

    结果:

    页面:

    后台:

     3、总结

     从上面示例我们可以看出:如果同步方法,那我们需要6秒,而异步执行,我们只需要3秒左右,这就是异步的作用。

         1)要把异步任务封装到类里面,不能直接写到Controller
         2)增加Future<String> 返回结果 AsyncResult<String>("task执行完成");
         3)如果需要拿到结果 需要判断全部的 task.isDone()

     

     想太多,做太少,中间的落差就是烦恼。想没有烦恼,要么别想,要么多做。上尉【10】

        

  • 相关阅读:
    How to build Linux system from kernel to UI layer
    Writing USB driver for Android
    Xposed Framework for Android 8.x Oreo is released (in beta)
    Linux Smartphone Operating Systems You Can Install Today
    Librem 5 Leads New Wave of Open Source Mobile Linux Contenders
    GUADEC: porting GNOME to Android
    Librem 5 – A Security and Privacy Focused Phone
    GNOME and KDE Join Librem 5 Linux Smartphone Party
    Purism计划推出安全开源的Linux Librem 5智能手机
    国产系统之殇:你知道的这些系统都是国外的
  • 原文地址:https://www.cnblogs.com/Javastudy-note/p/13818040.html
Copyright © 2011-2022 走看看