定时任务和异步任务

简介: 定时任务和异步任务

公众号merlinsea


  • springboot整合定时任务

   定时任务:规定在某个时间点执行的任务称为定时任务,主要用于报表系统或者统计系统。


1、springboot的启动类开启定时任务注解

核心:@EnableScheduling


@SpringBootApplication
@EnableScheduling
public class DemoProjectApplication {
   public static void main(String[] args) {
      SpringApplication.run(DemoProjectApplication.class, args);
   }
}


2、编写定时任务类【业务逻辑】

核心:

1、@Component 标识为组件交由spring扫描

2、@Scheduled 配置什么时候执行


@Component
class VideoOrderTask {
    //每2秒执行一次
    @Scheduled(fixedDelay = 4000)
    //@Scheduled(fixedRate = 4000)
    //@Scheduled(cron = "*/1 * * * * *")
    public void sum(){
        //正常的是从数据库中查询
        System.out.println(LocalDateTime.now() + " 当前交易额="+ Math.random());
    }
}

640.png


  • springboot整合异步任务

   异步任务:就是会在主线程之外开辟一个子线程单独执行的任务,常见的发送短信,发送邮件的任务可以通过异步任务执行。


1、springboot启动类开启异步任务注解

核心:@EnableAsync


@SpringBootApplication
@EnableAsync
public class DemoProjectApplication {
   public static void main(String[] args) {
      SpringApplication.run(DemoProjectApplication.class, args);
   }
}


2、编写异步任务类【业务逻辑】

核心:

1、@Component 交给spring扫描

2、@Async 标识哪些方法是异步执行,作用在类上所有方法都是异步执行,作用在方法上只是这个方法是异步执行


/**
 * @Async标识该类的每一个方法都是异步方法
 */
@Component
@Async
public class AsyncTask {
    public void task1(){
        try {
            Thread.sleep(4000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(" task 1 ");
    }
    public void task2(){
        try {
            Thread.sleep(4000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(" task 2 ");
    }
    public void task3(){
        try {
            Thread.sleep(4000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(" task 3 ");
    }
    /**
    带返回结果的异步任务
    */
    public Future<String> task4(){
        try {
            Thread.sleep(4000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(" task 4 ");
        return new AsyncResult<String>("task4");
    }
}


3、测试

   测试结果:最终主线程很快就会结束,不会停下来等异步任务,异步任务在主线程结束时还会继续执行。


@GetMapping("async")
public JsonData testAsync(){
    long begin = System.currentTimeMillis();
    /**
     * 一下三个方法执行会异步执行,不会阻塞主线程,因此主线程很快就会结束
     */
    asyncTask.task1();
    asyncTask.task2();
    asyncTask.task3();
    //获取返回结果
    Future<String> task4 =  asyncTask.task4();
    for(;;){
        if(task4.isDone()){
            try {
                String task4Result = task4.get();
                System.out.println(task4Result);
             } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }finally {
                break;
            }
        }
    }
    long end = System.currentTimeMillis();
    return JsonData.buildSuccess( end - begin);
}


640.png

相关文章
|
5月前
|
缓存 Java UED
仅执行一次的定时任务如何设置?
【10月更文挑战第12天】仅执行一次的定时任务如何设置?
348 1
|
7月前
|
数据采集 API 索引
异步任务处理系统问题之异步任务处理系统的问题如何解决
异步任务处理系统问题之异步任务处理系统的问题如何解决
263 2
|
9月前
|
监控 安全 Java
任务调度和异步执行相关的功能
任务调度和异步执行相关的功能
45 0
|
运维 Serverless Go
详解异步任务:任务的状态及生命周期管理
任务系统中有一类很重要的概念,即任务的状态和生命周期管理。细分的状态有助于在使用时能够更清楚的了解系统发生了什么内容,便于针对性的根据业务情况进行操作。
详解异步任务:任务的状态及生命周期管理
|
Linux 数据库
定时任务
定时任务
206 0
|
关系型数据库 MySQL 调度
定时任务优化
简单描述一下定时任务的优化
165 0
|
负载均衡 Java 数据挖掘
定时任务实现的几种方式
定时任务实现的几种方式
314 1
|
Python
Python编程:Celery执行异步任务和定时任务
Python编程:Celery执行异步任务和定时任务
319 0
Python编程:Celery执行异步任务和定时任务
|
Java 开发者 Spring
异步任务|学习笔记
快速学习异步任务
|
Java Linux Apache
brpc中的定时任务使用介绍
ok,欢迎来到brpc小课堂,今天我来讲点不是很hard core的东西,大家take it easy。
575 0
brpc中的定时任务使用介绍