Spring Boot 知识笔记(定时任务与异步)

摘要:
一、定时任务1、启动类里面增加注入@SpringBootApplication//@SpringBootApplication=@Configuration+@EnableAutoConfiguration+@ComponentScan@Configuration@ServletComponentScan//扫描过滤器等servlet、filter注解@MapperScan("net.Eleven

一、定时任务

1、启动类里面增加注入

@SpringBootApplication    //@SpringBootApplication = @Configuration+@EnableAutoConfiguration+@ComponentScan
@Configuration
@ServletComponentScan  //扫描过滤器等servlet、filter注解
@MapperScan("net.Eleven.demo.Mapper") //扫描对应的Mapper文件

@EnableScheduling   //定时任务注解,扫描包里面所有子类中的定时任务
@EnableAsync  //开启异步任务
public class XdclassApplication extendsSpringBootServletInitializer {
    @Override
    protectedSpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(XdclassApplication.class);
    }
    public static voidmain(String[] args){
        SpringApplication.run(XdclassApplication.class,args);
    }
}

2、新建一个定时任务类

packagenet.Eleven.demo.task;
importorg.springframework.scheduling.annotation.Scheduled;
importorg.springframework.stereotype.Component;
importjava.util.Date;
/**
 * 功能描述:定时任务业务类
 *
 */
@Component
public classTestTask {
//@Scheduled(fixedRate = 2000)  //每两秒执行一次
    @Scheduled(cron = "*/3 * * * * *") //每三秒执行一次
    public voidsendEmail(){
        System.out.println("当前时间:"+newDate());
    }
}

3、定时任务的几种配置方法

3.1、cron 定时任务表达式 @Scheduled(cron="*/1 * * * * *") 表示每秒
3.2、fixedRate: 定时多久执行一次(上一次开始执行时间点后xx秒再次执行;)
3.3、fixedDelay: 上一次执行结束时间点后xx秒再次执行
3.4、fixedDelayString: 字符串形式,可以通过配置文件指定

二、异步任务

1、启动类增加注解(@EnableAsync //开启异步任务)

2、新建一个异步任务类

packagenet.Eleven.demo.task;
importorg.springframework.scheduling.annotation.Async;
importorg.springframework.stereotype.Component;
@Component
@Async  //类中所有的方法都是异步
public classAsyncTask {
    @Async //被标注的方法是异步
    public void task1() throwsInterruptedException{
        long begin =System.currentTimeMillis();
        Thread.sleep(1000L);
        long end =System.currentTimeMillis();
        System.out.println("任务1耗时:"+(end-begin));
    }
    public void task2() throwsInterruptedException{
        long begin =System.currentTimeMillis();
        Thread.sleep(2000L);
        long end =System.currentTimeMillis();
        System.out.println("任务2耗时:"+(end-begin));
    }
    public void task3() throwsInterruptedException{
        long begin =System.currentTimeMillis();
        Thread.sleep(3000L);
        long end =System.currentTimeMillis();
        System.out.println("任务3耗时:"+(end-begin));
    }
}

3、在controller里面调用这个任务

@Autowired
    privateAsyncTask  asyncTask;
    @GetMapping("/api/async")
    public JsonData doTask() throwsInterruptedException{
        long begin =System.currentTimeMillis();
        asyncTask.task1();
        asyncTask.task2();
        asyncTask.task3();
        long end =System.currentTimeMillis();
        long totalTime = end-begin;
        System.out.println("执行耗时:"+totalTime);
        returnJsonData.buildSuccess(totalTime);
    }

4、执行结果

Spring Boot 知识笔记(定时任务与异步)第1张

免责声明:文章转载自《Spring Boot 知识笔记(定时任务与异步)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇《大话数据结构》第一章 数据结构绪论SpringBoot整合Mybatis-Plus报错org.apache.ibatis.binding.BindingException下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

linux任务计划cron chkconfig工具 systemd管理服务 unit介绍 target介绍

[root@lizhipenglinux01 ~]# crontab -e  编写任务计划no crontab for root - using an empty one 20 11 29 01 1 echo "OK" > /root/cron.log      表示在1月29日(星期一)的11点20分执行:echo  "OK" > /ro...

go语言 robfig/cron包 实现定时 调用

package main import ( "github.com/robfig/cron" "time" "fmt" "os" log "github.com/cihub/seelog" ) var ( ttt int ) const ( logFilePath = "hard/log/test/t...

注解式Schedule配置定时任务

@Component public class ScheduledTasks { @Autowired private ActivityService activityService; // 1000即1s @Scheduled(fixedRate = 1000) public void reportCurrentTi...

Linux中计划任务执行脚本crontab-简洁版

我使用的是ubuntu16,所以在ubuntu中一切正常,在其他linux系统中应该都差不多。   1 计划任务,crontab命令选项:     -u指定一个用户,     -l列出某个用户的任务计划,     -r删除某个用户的任务,     -e编辑某个用户的任务   2 cron文件语法:     分     小时   日       月    ...

linux 利用 cron 实现 程序开机启动/cron任务的多种实现方法/cron重启/cron日志开启

方法一、登录服务器,直接修改:crontab -e然后添加:@reboot [nohup] {命令} ...# 或者定时任务指令保存退出: ctrl + O ctrl + x方法二、指定用户进行修改:sudo crontab -u {username} -e然后添加:@reboot [nohup] {命令}...# 或者定时任务指令保存退出: ctrl...

Linux系统定时任务启动

 分类: linux,shell,python cron是一个linux下的定时执行工具,可以在无需人工干预的情况下运行作业。由于Cron 是Linux的内置服务,但它不自动起来,可以用以下的方法启动、关闭这个服务: /sbin/service crond start //启动服务 /sbin/service crond stop //关闭服务 /sbin...