当前位置: 移动技术网 > IT编程>开发语言>Java > 详解SpringBoot Schedule配置

详解SpringBoot Schedule配置

2019年07月22日  | 移动技术网IT编程  | 我要评论

三生三世十里桃花45集,美姑县人民政府,aolikes

1. 定时任务实现方式

定时任务实现方式:

  1. java自带的java.util.timer类,这个类允许你调度一个java.util.timertask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。一般用的较少,这篇文章将不做详细介绍。
  2. 使用quartz,这是一个功能比较强大的的调度器,可以让你的程序在指定时间执行,也可以按照某一个频度执行,配置起来稍显复杂,有空介绍。
  3. springboot自带的scheduled,可以将它看成一个轻量级的quartz,而且使用起来比quartz简单许多,本文主要介绍。

定时任务执行方式:

  1. 单线程(串行)
  2. 多线程(并行)

2. 创建定时任务

package com.autonavi.task.test;

import org.slf4j.logger;

import org.slf4j.loggerfactory;

import org.springframework.scheduling.annotation.scheduled;

import org.springframework.stereotype.component;

import com.autonavi.task.scheduledtasks;

@component

public class scheduledtest {

  private static final logger logger = loggerfactory.getlogger(scheduledtasks.class); 

  @scheduled(cron="0 0/2 * * * ?") 

  public void executefiledownloadtask() { 

    // 间隔2分钟,执行任务   

    thread current = thread.currentthread(); 

    system.out.println("定时任务1:"+current.getid());

    logger.info("scheduledtest.executefiledownloadtask 定时任务1:"+current.getid()+ ",name:"+current.getname());

  }

} 

@scheduled 注解用于标注这个方法是一个定时任务的方法,有多种配置可选。cron支持cron表达式,指定任务在特定时间执行;fixedrate以特定频率执行任务;fixedratestring以string的形式配置执行频率。

3. 启动定时任务

@springbootapplication

@enablescheduling

public class app { 

  private static final logger logger = loggerfactory.getlogger(app.class); 

  public static void main(string[] args) {

    springapplication.run(app.class, args);   

    logger.info("start");            

  }  

} 

其中 @enablescheduling 注解的作用是发现注解@scheduled的任务并后台执行。

springboot本身默认的执行方式是串行执行,也就是说无论有多少task,都是一个线程串行执行,并行需手动配置

4. 并行任务

继承schedulingconfigurer类并重写其方法即可,如下:

@configuration

@enablescheduling

public class scheduleconfig implements schedulingconfigurer {

  @override

  public void configuretasks(scheduledtaskregistrar taskregistrar) {

    taskregistrar.setscheduler(taskexecutor());

  } 

  @bean(destroymethod="shutdown")

  public executor taskexecutor() {

    return executors.newscheduledthreadpool(100);

  }

} 

再次执行之前的那个demo,会欣然发现已经是并行执行了!   

5. 异步并行任务

import org.springframework.scheduling.taskscheduler;

import org.springframework.scheduling.annotation.asyncconfigurer;

import org.springframework.scheduling.annotation.enableasync;

import org.springframework.scheduling.annotation.enablescheduling;

import org.springframework.scheduling.annotation.schedulingconfigurer;

import org.springframework.scheduling.concurrent.threadpooltaskscheduler;

import org.springframework.scheduling.config.scheduledtaskregistrar;

 

@configuration

@enablescheduling

@enableasync(

mode = advicemode.proxy, proxytargetclass = false,

order = ordered.highest_precedence

)

@componentscan(

basepackages = "hello"

)

public class rootcontextconfiguration implements

asyncconfigurer, schedulingconfigurer {

@bean

public threadpooltaskscheduler taskscheduler()

{

threadpooltaskscheduler scheduler = new threadpooltaskscheduler();

scheduler.setpoolsize(20);

scheduler.setthreadnameprefix("task-");

scheduler.setawaitterminationseconds(60);

scheduler.setwaitfortaskstocompleteonshutdown(true);

return scheduler;

}

 

@override

public executor getasyncexecutor()

{

executor executor = this.taskscheduler();

return executor;

}

 

@override

public void configuretasks(scheduledtaskregistrar registrar)

{

taskscheduler scheduler = this.taskscheduler();

registrar.settaskscheduler(scheduler);

}

} 

在启动的main方法加入额外配置:

@springbootapplication

public class application { 

  public static void main(string[] args) throws exception {

   annotationconfigapplicationcontext rootcontext =

   new annotationconfigapplicationcontext();

 

  rootcontext.register(rootcontextconfiguration.class);

  rootcontext.refresh();

  }

} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网