您的位置 首页 golang

springboot定时任务介绍和多实例部署避免重复执行设计

1. 概述

在新设计的架构中有定时任务或者说是计划任务的需求。我想从如下选项中选择

产品

使用方式

小注

自写的框架

可集成到项目,可独立

我之前开发过java版本和golang版本的,但现在在一个传统的公司,收敛一下就不用自己的了

Quartz

集成到项目

使用面比较广,看似简单,其实也有些重了

Spring @Scheduled

默认集成

Springboot项目已自带,在使用上可能更显简单和轻量级

xxl-job

独立部署

中心化,分布式。易用性好,在稍微大点的项目推荐选用

elstic-job

独立部署

无中心化,分布式,入门和使用较前几种略显复杂,据说性能稍高一些

其实现在设计的架构针对的是一些不是太大规模的应用,定时任务也就不会特别巨大。为了节省资源,所以先直接使用最简单的方案 Spring @Scheduled

2. 设计目标

  • 轻量级,直接集成到项目
  • 注解方式完成
  • 支持cron表达式
  • 支持多实例部署时不重复执行计划任务

3. Spring @Scheduled基本应用介绍

示例中都是基于Springboot2.4.3的
一旦相关类创建后,计划任务管理器会跟着程序的启动自动启动

3.1 首先是基本配置

因为引入springboot就自带了,所以不需要单独在引入依赖。本来直接建立一个config类,标注@EnableScheduling就可以开启计划任务。但考虑到计划任务使用线程池,为了保证资源使用有边界,所以在这里特意自定义了线程池。

SchedulingConfig.java

 // 省略掉了包名和引入

/**
 * spring boot 自带的计划任务
 */
@Configuration
@EnableScheduling
public class SchedulingConfig implements SchedulingConfigurer {

    private int corePoolSize = 5;
    private int maxPoolSize = 10;
    private int queueCapacity = 5;

    /**
     * 自己建立线程池防止资源使用无边界问题
     * @return
     */
    @Bean(destroyMethod="shutdown")
    public Executor taskExecutor() {
        return Executors.newScheduledThreadPool(100);
    }

    /**
     * 设置线程池
     * @param taskRegistrar
     */
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler(taskExecutor());
    }
}

  

3.2 基本使用

固定延迟时间的任务使用方法

相同方法会在上一个任务执行完后,再延时指定的时间执行

     @Scheduled(fixedDelay = 1000)
    public void scheduleFixedDelayTask() {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.now();
        String dateString = dateTimeFormatter.format(localDateTime);
        System.out.println("固定延迟时间的任务 - " + dateString);
    }
  

固定频率任务执行使用方法

默认也是在上一个任务完成的情况下才会执行的,所以下面这个例子耗时2秒,也就不能按照真正的1秒的频率执行了。解决方案是使用异步注解。稍后会有示例

     @Scheduled(fixedRate = 1000)
    public void scheduleFixedRateTask() {
        // 让我们来测试一下每次任务都超时的情况下
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(
                "固定频率的任务 - " + System.currentTimeMillis() / 1000);
    }
  

在指定延迟时间后执行的固定频率任务

     @Scheduled(fixedDelay = 1000, initialDelay = 1000)
    public void scheduleFixedRateWithInitialDelayTask() {
        long now = System.currentTimeMillis() / 1000;
        System.out.println(
                "在指定延迟时间后执行的固定频率任务 - " + now);
    }
  

根据cron表达式确定时间执行

可指定cron表达式和时区。运行下面的例子可以证明多个注解放在一个方法上,实际会启动多个任务

     @Scheduled(cron = "0 35 14 * * ?", zone = "Asia/Shanghai")
    @Scheduled(cron = "*/5 * * * * ?", zone = "Asia/Shanghai")
    @Scheduled(cron = "*/5 * * * * ?")
    public void scheduleTaskUsingCronExpression() {
        long now = System.currentTimeMillis() / 1000;
        System.out.println(
                "使用cron表达式的定时任务 - " + now);
    }
  

在配置文件中配置crontab表达式

下例子中的$是在application.yml(或者自己定义的prifile文件)中配置的。
这点是我比较喜欢的,生产环境不方便重新编译的情况下,可用

     @Scheduled(cron = "${cron.expression}")
    public void scheduleTaskUsingCronExpressionWithProperty() {
        long now = System.currentTimeMillis() / 1000;
        System.out.println(
                "使用cron表达式的定时任务(配置文件指定时间) - " + now);
    }
  

最后说真正的实现固定频率执行任务的配置方式

需要在类上加上@EnableAsync,在方法上加上@Async。
不过注意资源消耗,请谨慎使用

DemoAsyncTasks.java

 //package and import abort

/**
 * 异步执行任务,能真正实现按频率执行 关键在@EnableAsync注解的使用
 */
@EnableAsync
@Component
public class DemoAsyncTasks {

    /**
     * 此种任务适合确保间隔时间内完成的,否则会消耗大量的资源(线程资源)和出现Out of Memory exception
     * 任务执行结果类似如下
     * <pre>
     * 固定频率异步执行的任务 - 1615357318
     * 固定频率异步执行的任务 - 1615357319
     * 固定频率异步执行的任务 - 1615357320
     * 固定频率异步执行的任务 - 1615357321
     * 固定频率异步执行的任务 - 1615357322
     * </pre>
     */
    @Async
    @Scheduled(fixedRate = 1000)
    @UniqueExecutionLock(timeout = 5) // 这个将是下篇文章介绍的多实例唯一执行锁,本期测试的话可直接删除
    public void scheduleFixedRateTaskAsync() throws InterruptedException {
        System.out.println(
                "固定频率异步执行的任务 - " + System.currentTimeMillis() / 1000);
        Thread.sleep(2000);
    }

}  

4. 多实例部署防止重复执行的方案

在自写的架构中,是通过锁使整个计划任务管理器只在单个实例中运行实现的。优点是容易查问题,缺点是不能随机分布到各个节点运行。

现在使用Springboot @Scheduled的方案决定用单个任务的同步锁来实现。简单来说就是:

  • 多实例部署时,每个实例的计划任务都启动;
  • 在每个计划任务在执行前先获取锁
  • 获取到锁的执行,获取不到锁的跳过执行

而实现获取锁的方式可以用数据库mysql的记录版本控制或者redis缓存来实现。

我暂时计划用redis来实现。主要用到redis的”set if absent (SETNX)”的命令来实现锁的获取,同时兼顾超时删除。当然在每个计划任务方法重复写锁获取和锁释放的代码有点不优雅和重复劳动。所以我设计了通过切片和自写注解的方式,实现在计划任务方法上加锁和设置超时的组件框架,将在下一篇文中中介绍(直接开箱即用哦)。

参考资料:

文章来源:智云一二三科技

文章标题:springboot定时任务介绍和多实例部署避免重复执行设计

文章地址:https://www.zhihuclub.com/97298.shtml

关于作者: 智云科技

热门文章

评论已关闭

38条评论

  1. An evidence-based unified definition of lifelong and acquired premature ejaculation report of the Second International Society for Sexual Medicine Ad Hoc Committee for the Definition of Premature Ejaculation

  2. Fluoxetine is inferior to sertraline, while the effect of clomipramine does not significantly differ from fluoxetine and sertraline Psychosocial outcomes, spontaneity, and time concerns have shown significant improvement after treatment with long-acting compared with short-acting PDE5 inhibitors

  3. The p53 Arg72Pro polymorphism, HPV, and invasive squamous cell cervical cancer a population based study 247 mmol, 100 mg and 1 2 chloroethyl piperidine hydrochloride 0

  4. The defining variant in 41 is 2988G A with 2850C T and 4180G C in the haplotype, and is associated with decreased enzymatic activity

  5. Antidepressants Selective serotonin reuptake inhibitors SSRIs such as buspirone migraine medications medications such as lithium Amphetamines Some common over the counter herbal or dietary supplements such as St

  6. These data suggest that tamoxifen inhibits the UUO induced activation of TGF ОІ1 Smad signaling pathway

  7. Moreover, TRIM3 promoted the ESR1 SUMO modification and activated the transcription of ER target genes by binding to UBC9, which could be abolished by overexpression of SENP1 ADAPTING THE DRUG AND ITS DOSE TO THE PATIENT Pharmacogenetics in psychiatry There is a great variability in the way in which patients respond to drugs, in the appearance of side effects and in the symptoms caused by the toxicity of the drugs

  8. Weird scalp pain just before and during hair loss is completely normal as you are discovering 2015, San Antonio Cancer Symposium; Ahmad et al

  9. Among children receiving services for autism, children who had been conceived using ART had a lower median age of autism diagnosis 3

  10. Interestingly, certain statins modestly ameliorate cell cycle arrest and SA ОІ gal expression by mesenchymal stem cells 20 and chondrocytes 21, suggesting they might suppress certain senescent phenotypes

  11. We used a phenylhydrazine Phz hemolytic anemia model, which stimulates massive erythropoiesis

  12. There is an anticipated reduction in tetracycline hydrochloride systemic absorption due to an interaction with bismuth Phan TT, Sun L, Bay BH, Chan SY, Lee ST

  13. I m curious if these other doctors advised their patients to check Age Certain conditions including diabetes, hypertension, and obesity Excessive sunlight exposure History of eye injury or surgery Long term corticosteroid use Excessive intake of alcohol

  14. or estrogen and tamoxifen, and plated in methylcellulose culture medium 750 and APR of 3

  15. Subcutaneous infusion by means of a mini pump adhered to the skin may offer a solution for patients who otherwise do not require hospital care

  16. In addition, patients with high ER signaling and low EP category could still derive major benefit from extended endocrine therapy in spite of the lower risk

  17. Both canonical and noncanonical TGF ОІ downstream signaling were profoundly decreased in the myofibroblasts of Cre induced CryAB R120G Postn r2 hearts This ed meds and nitro small program implanted in the Xingchen system, a powerful information retrieval engine does cialis cause headaches hidden in the network, holds too many secrets

  18. A large Swedish study has not shown any such correlation 53, although it has been implicated as a causal agent in other studies 54 If the United States loses that, they ve lost, he said

  19. Our Disclosure of Your Personal Data and Other Information In case of borderline significant interactions, results specific to subgroups were reported

  20. triamcinolone cvs children s zyrtec The regulator made writing the rule a priority after tradingfirm Knight Capital Group, which has since been bought by rivalfirm Getco Holding Co and is now called KCG Holdings Inc, nearly went bust due to a software glitch that led to 461 million in losses In the fall of 2005, the FDA revised prescribing information for the copper IUD

  21. The most widely investigated antidepressant for the treatment of pure OCD is the TCA clomipramine Montgomery, 1980; Thoren et al

  22. It s a good compound to essentially have sitting around 1 Department of Radiology, Center for Biomedical Imaging, New York University School of Medicine, 660 First Ave, 4th Fl, New York, NY 10016

网站地图