springboot2.x整合定时任务和异步任务

1.定时任务schedule

  • springboot使用注解方式开启定时任务

    • 启动类加上@EnableScheduling开启定时任务自动扫描
    • 定时任务业务类加注解@Component被容器扫描
    • 定时执行的方法加上注解@Scheduled(fixedRate = 10000)
  • 定时规则

    • @Scheduled(cron = “* * * * * *”):定时任务表达式,分别表示秒分时日月周
    • @Scheduled(fixedRate = 1000):定时1秒执行一次
    • @Scheduled(fixedDelay = 1000):上一次代码执行结束后1秒再执行;假如代码需要执行1秒,也就是说每2秒执行一次
  • 代码示例

    package com.gen.schedule;
    
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    import java.time.LocalDateTime;
    
    @Component
    public class CustomSchedule {
    
        @Scheduled(cron = "* * * * * *")
        public void testSchedule() {
            System.out.println(LocalDateTime.now());
        }
    
    }
    

2.异步任务

  • springboot使用注解方式开启异步任务

    • 启动类加上@EnableAsync注解开启功能自动扫描
    • 定义异步任务类并使用@Component被容器扫描,异步方法加上@Async
    • 如果需要异步类返回结果,增加Future返回结果AsyncResult,调用处拿返回结果时,判断是否完成isDone()
  • 代码示例

    • 异步类

      package com.gen.async;
      
      import org.springframework.scheduling.annotation.Async;
      import org.springframework.scheduling.annotation.AsyncResult;
      import org.springframework.stereotype.Component;
      
      import java.util.concurrent.Future;
      
      @Component
      public class CustomAsync {
      
          @Async
          public void test1() {
              try {
                  Thread.sleep(1000L);
                  System.out.println("异步任务test1");
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
      
          @Async
          public Future<String> test2() {
              try {
                  Thread.sleep(2000L);
                  System.out.println("异步任务test2");
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              return new AsyncResult("任务返回值");
          }
      
      }
      
    • controller调用

      @Autowired
      private CustomAsync customAsync;
      
      @GetMapping("test")
      public void test() {
          this.customAsync.test1();
          Future<String> stringFuture = this.customAsync.test2();
          while (true) {
              if (stringFuture.isDone()) {
                  try {
                      System.out.println(stringFuture.get());
                  } catch (Exception e) {
                      e.printStackTrace();
                  } finally {
                      break;
                  }
              }
          }
      }
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水宝的滚动歌词

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值