第五篇:SpringBoot定时任务、异步任务

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_36279318/article/details/82829311

一、定时任务简介

程序中设置定时任务的作用就是定时的执行某一任务,比如

  • 管理系统定时向用户发送短信、邮件等
  • 互联网金融项目定期批款,放款等操作
  • 天气预报系统定期更新数据

二、Spring Boot定时任务的使用

Spring Boot定时任务与Quartz定时任务的区别

SpringBoot中使用定时任务非常简单使用@EnableScheduling、@Scheduled注解即可!

操作步骤如下:

1.创建一个SpringBoot Web应用

2.@EnableScheduling开启定时任务

@SpringBootApplication
@EnableScheduling //开启定时任务
public class SpringbootTaskApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootTaskApplication.class, args);
    }
}

2.@Scheduled设置定时触发执行的时间

@Scheduled的属性

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(Schedules.class)
public @interface Scheduled {

	
	String cron() default "";

	String zone() default "";
        
        //@Scheduled(fixedDelay = 6000) :上一次执行完毕时间点之后6秒再执行
	long fixedDelay() default -1;

	String fixedDelayString() default "";

        //比如:@Scheduled(fixedRate = 6000) :上一次开始执行时间点之后6秒再执行
	long fixedRate() default -1; 

	String fixedRateString() default "";
      
	//@Scheduled(initialDelay=1000, fixedRate=6000) :第一次延迟1秒后执行,之后按fixedRate的规则每6秒执行一次
        long initialDelay() default -1;

	String initialDelayString() default "";

}

3.定时任务类必须注入spring容器,否则定时任务无效!

cron表达时的写法:

常用的cron表达式含义

每隔5秒执行一次:*/5 * * * * ? 
每隔1分钟执行一次:0 */1 * * * ? 
每天23点执行一次:0 0 23 * * ? 
每天凌晨1点执行一次:0 0 1 * * ? 
每月1号凌晨1点执行一次:0 0 1 1 * ?
每月最后一天23点执行一次:0 0 23 L * ? 
每周星期天凌晨1点实行一次:0 0 1 ? * L 
在26分、29分、33分执行一次:0 26,29,33 * * * ?
每天的0点、13点、18点、21点都执行一次:0 0 0,13,18,21 * * ?

在线cron表达式生成器 

@Service //必须向spring容器中注册,否则定时任务无效!
public class TaskService {
    //@Scheduled(cron = "0,1,2,3,4 * * * * MON-SAT")//表示前4秒都执行一次
    //@Scheduled(cron = "0-4 * * * * MON-SAT")//表示前4秒都执行一次
    @Scheduled(cron = "0/4 * * * * MON-SAT") //表示每隔4秒执行一次
    public void test01(){
        System.out.println("=====定时执行一些业务=====");
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("hh:mm:ss");
        System.out.println("当前时间:"+simpleDateFormat.format(new Date()));
        System.out.println("01.定时向用户发送短信");
        System.out.println("02.定时删除缓存中的数据");
        System.out.println("03.定时数据库备份");
    }
    
    //每隔6秒执行一次
    @Scheduled(fixedRate = 6000)
    public void test02(){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("hh:mm:ss");
        System.out.println("当前时间:"+simpleDateFormat.format(new Date()));
    }
}

三、Spring Boot异步任务的使用

异步任务的作用:减少程序运行时间、提升系统响应速度、快速响应用户!

SpringBoot异步处理使用场景

  • 发送短信
  • 发送邮件
  • APP消息推送
  • 节省运维凌晨发布任务时间提供效率

SpringBoot异步任务使用步骤如下:

1.使用注解@EnableAsync开启异步,会自动扫描

@EnableAsync //开启异步处理开关
@SpringBootApplication
public class SpringbootAsyncApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootAsyncApplication.class, args);
    }
}

2.定义@Component、@Async作为组件被容器扫描并且执行

@Component
public class AsyncTask {

    /**
     * 1.如果执行下面三个方法是同步任务耗时:1000+800+400
     * 2.开启异步任务执行下面三个方法耗时:取耗时最大的1000,
     *   也就是执行三个方法只需要1000
     */
    @Async
    public Future<Boolean> doTask01()throws Exception{
        long start=System.currentTimeMillis();
        //使用线程的目的测试方法执行时间
        Thread.sleep(1000);
        long end=System.currentTimeMillis();
        System.out.println("执行任务1耗时:"+(end - start)+"毫秒");
        return new AsyncResult<>(true);
    }
    @Async
    public Future<Boolean> doTask02()throws Exception{
        long start=System.currentTimeMillis();
        Thread.sleep(700);
        long end=System.currentTimeMillis();
        System.out.println("执行任务2耗时:"+(end - start)+"毫秒");
        return new AsyncResult<>(true);
    }
    @Async
    public Future<Boolean> doTask03()throws Exception{
        long start=System.currentTimeMillis();
        Thread.sleep(600);
        long end=System.currentTimeMillis();
        System.out.println("执行任务3耗时:"+(end - start)+"毫秒");
        return new AsyncResult<>(true);
    }
}

3.测试异步任务执行总时间

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootAsyncApplicationTests {
    @Autowired
    AsyncTask asyncTask;
    @Test
    public void test()throws Exception {
        long start=System.currentTimeMillis();
        Future<Boolean> f1=asyncTask.doTask01();
        Future<Boolean> f2=asyncTask.doTask02();
        Future<Boolean> f3=asyncTask.doTask03();

        //执行此段代码也需要耗时间
        while(!f1.isDone()||!f2.isDone()||!f3.isDone()) {
            if(f1.isDone() && f2.isDone() && f3.isDone())
                break;
        }
        long end=System.currentTimeMillis();
        System.out.println("执行以上三个任务总耗时:"+(end - start)+"毫秒");
    }

}

4.执行三个方法总耗时

执行任务3耗时:601毫秒
执行任务2耗时:701毫秒
执行任务1耗时:1000毫秒
执行以上三个任务总耗时:1028毫秒

5.如果注释@Async,那么就是同步任务。三个任务执行总耗时=600+700+1000(毫秒)

猜你喜欢

转载自blog.csdn.net/weixin_36279318/article/details/82829311