zoukankan      html  css  js  c++  java
  • SpringBoot任务管理

    SpringBoot任务管理

    异步任务

    • 创建一个springboot项目,勾选web

    • 在启动类上添加@EnableAsync

      package com.sheep;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.scheduling.annotation.EnableAsync;
      
      /**
       * @EnableAsync:开启异步注解功能
       */
      @EnableAsync
      @SpringBootApplication
      public class DomeApplication {
          public static void main(String[] args) {
              SpringApplication.run(DomeApplication.class, args);
          }
      }
      
    • 创建service包在包下创建AsyncService类并添加@Async注解

      package com.sheep.service;
      
      import org.springframework.scheduling.annotation.Async;
      import org.springframework.stereotype.Service;
      
      @Service
      public class AsyncService {
      
          @Async
          public void hello(){
              try {
                  Thread.sleep(3000);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              System.out.println("数据正在处理中...");
          }
      }
      
    • controller

      package com.sheep.controller;
      
      import com.sheep.service.AsyncService;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RestController;
      
      @RestController
      public class AsyncController {
      
          @Autowired
          AsyncService asyncService;
      
          @RequestMapping("/hello")
          public String hello(){
              asyncService.hello();
              return "OK";
          }
      }
      

      执行后会发现页面立马相应OK字符串,而后台却隔了3s才打印System.out.println("数据正在处理中...");

    定时任务

    • 创建springboot项目勾选web

    • 在启动类中添加注解@EnableScheduling

      package com.sheep;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.scheduling.annotation.EnableScheduling;
      
      /**
       * @EnableScheduling:开启定时任务功能
       */
      @EnableScheduling
      @SpringBootApplication
      public class DomeApplication {
      
          public static void main(String[] args) {
              SpringApplication.run(DomeApplication.class, args);
          }
      
      }
      
    • 创建一个类使用cron表达式

      package com.sheep.service;
      
      import org.springframework.scheduling.annotation.Scheduled;
      import org.springframework.stereotype.Service;
      
      @Service
      public class ScheduledService {
      
          /*
          * @Scheduled(cron="表达式")
          * cron = "0 53 9 * * ?"
          * cron = ”秒 分 时 日 月 周“
          * 常用:
          *       0 53 9 * * ? 表示每一天的9点53分0秒执行
          *       0 0/3 9,10 * * ? 表示每天9点和10点每隔3分钟执行一次
          *       0 53 9 ? * 1-6 每个月的周一到周六10:15分执行
          * */
          @Scheduled(cron = "0/3 * * * * ?")
          public void hello(){
              System.out.println("hello你被执行了");
          }
      }
      

      运行后每个相应的时间就是执行该任务

    还历史以真诚,还生命以过程。 ——余秋雨
  • 相关阅读:
    枚举、函数关于oracle函数listagg的使用说明by小雨
    执行、Mongodb MapReduce示例1个by小雨
    事务、异常TSQL 编码时应该注意的10个问题by小雨
    源、执行GoldenGate 单向DDL同步by小雨
    Oracle中的所有权限by小雨
    数据库、版本数据库学习从此开始by小雨
    统计、案例深入理解Oracle索引(10):索引列字符类型统计信息的32位限制by小雨
    字段、数据库表三大范式及存储方式by小雨
    数据库、用户第二章Getting Start with the Oracle Server(oracle入门)by小雨
    搜索、关键字截图留念,“万能数据库查询分析器”作为关键字在百度和谷歌上的海量搜索结果by小雨
  • 原文地址:https://www.cnblogs.com/w-eye/p/14790655.html
Copyright © 2011-2022 走看看