Spring Boot实现定时任务单线程多线程的方法

发布时间:2020-08-10 10:43:18 作者:小新
来源:亿速云 阅读:232

小编给大家分享一下Spring Boot实现定时任务单线程多线程的方法,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!

1、创建定时任务:

@Component
public class AutoNotifyController {


  /**
   * 获取RedisUtils注入的bean
   *
   * @return
   */
  private ThreadUtil getThreadUtil() {
    ThreadUtil threadUtil = SpringContextUtil.getBean("threadUtil");
    return threadUtil;
  }


  /**
   * @描述: 推送启动充电结果的自动获取和处理分发方法
   * @输入值: void
   * @返回值: void
   */
  @Scheduled(cron = "*/5 * * * * ?")
  public void AutoNotifyStartChargeResult() {
    getThreadUtil().AutoNotifyStartChargeResult();
  }


  /**
   * @描述: 推送充电状态的自动获取和处理分发方法
   * @输入值: void
   * @返回值: void
   */
  @Scheduled(cron = "*/50 * * * * ?")
  public void AutoNotifyChargeStatus() {
    getThreadUtil().AutoNotifyChargeStatus();
  }


  /**
   * @描述: 推送停止充电结果的自动获取和处理分发方法
   * @输入值: void
   * @返回值: void
   */
  @Scheduled(cron = "*/5 * * * * ?")
  public void AutoNotifyStopChargeResult() {
    getThreadUtil().AutoNotifyStopChargeResult();
  }


  /**
   * @描述: 推送订单信息的自动获取和处理分发方法
   * @输入值: void
   * @返回值: void
   */
  @Scheduled(cron = "*/5 * * * * ?")
  public void AutoNotifyOrderInfo() {
    getThreadUtil().AutoNotifyOrderInfo();
  }


  /**
   * @描述: 公共信息部分的设备状态变化推送接口的自动获取和处理分发方法
   * @输入值: void
   * @返回值: void
   */
  @Scheduled(fixedRate = 200)
  public void checkGunStatus() {
    getThreadUtil().checkGunStatus();
  }


  /**
   * @描述: 对于Redis中的活跃订单增加和删除的轮询执行方法
   */
  @Scheduled(cron = "*/5 * * * * ?")
  public void ActiveOrderAddAndDelete() {
    getThreadUtil().ActiveOrderAddAndDelete();
  }


  /**
   * @描述: 对于Redis中的结束订单订单增加和删除的轮询执行方法
   */
  @Scheduled(cron = "*/5 * * * * ?")
  public void EndOrderAddAndDelete() {
    getThreadUtil().EndOrderAddAndDelete();
  }


}

使用 @Scheduled来创建定时任务 这个注解用来标注一个定时任务方法。

通过看 @Scheduled源码可以看出它支持多种参数:

(1)cron:cron表达式,指定任务在特定时间执行;

(2)fixedDelay:表示上一次任务执行完成后多久再次执行,参数类型为long,单位ms;

(3)fixedDelayString:与fixedDelay含义一样,只是参数类型变为String;

(4)fixedRate:表示按一定的频率执行任务,参数类型为long,单位ms;

(5)fixedRateString: 与fixedRate的含义一样,只是将参数类型变为String;

(6)initialDelay:表示延迟多久再第一次执行任务,参数类型为long,单位ms;

(7)initialDelayString:与initialDelay的含义一样,只是将参数类型变为String;

(8)zone:时区,默认为当前时区,一般没有用到。

2、开启定时任务:

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

注:这里的 @EnableScheduling 注解,它的作用是发现注解 @Scheduled的任务并由后台执行。没有它的话将无法执行定时任务。

引用官方文档原文:

@EnableScheduling ensures that a background task executor is created. Without it, nothing gets scheduled.

3、执行结果(单线程)

就完成了一个简单的定时任务模型,下面执行springBoot观察执行结果:

Spring Boot实现定时任务单线程多线程的方法

从控制台输入的结果中我们可以看出所有的定时任务都是在同一个线程池用同一个线程来处理的,那么我们如何来并发的处理各定时任务呢,请继续向下看。

4、多线程处理定时任务:

1.开启多线程

@SpringBootApplication
@EnableScheduling
@EnableAsync
public class PositivebuttjointApplication extends SpringBootServletInitializer
{

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

  }

加入@EnableAsync开启多线程

2.使用多线程

@Async
  public void AutoNotifyStartChargeResult() {
  }

   调用的方法上加上@Async使用多线程

  3.配置连接池  

@Configuration
public class ScheduleConfiguration implements SchedulingConfigurer {


  @Override
  public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
    taskRegistrar.setScheduler(this.getTaskScheduler());
  }

  private ThreadPoolTaskScheduler getTaskScheduler() {
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.setPoolSize(20);
    taskScheduler.setThreadNamePrefix("schedule-pool-");
    taskScheduler.initialize();
    return taskScheduler;
  }
}

看完了这篇文章,相信你对Spring Boot实现定时任务单线程多线程的方法有了一定的了解,想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. Spring Boot定时+多线程执行过程解析
  2. Spring Boot中如何配置定时任务、线程池与多线程池执行

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring boot 定时

上一篇:如何修改php文件的创建时间

下一篇:Python学习教程:前后端分离开发入门

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》