Spring cron vs normal cron?

后端 未结 2 1937
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 15:37

I\'m trying to get a cron job working within a legacy Java/Spring/Hibernate project, so I decided to use the spring scheduler.

I want myTask.doStuff to run at 12:00

2条回答
  •  猫巷女王i
    2020-12-02 15:41

    Taking some note from: https://www.baeldung.com/cron-expressions

    A Spring Scheduled tasks is like this:

    1 2 3 4 5 6 Index
    - - - - - -
    * * * * * * command to be executed
    - - - - - -
    | | | | | | 
    | | | | | ------- Day of week (MON - SUN)
    | | | | --------- Month (1 - 12)
    | | | ----------- Day of month (1 - 31)
    | |-------------- Hour (0 - 23)
    | --------------- Minute (0 - 59)
    ----------------- Seconds (0 - 59)
    

    From: https://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/

    A Linux Cron job is like this:

    1 2 3 4 5 Index
    - - - - -
    * * * * * command to be executed
    - - - - -
    | | | | |
    | | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
    | | | ------- Month (1 - 12)
    | | --------- Day of month (1 - 31)
    | ----------- Hour (0 - 23)
    ------------- Minute (0 - 59)
    

    Side note:

    • Some article said it is possible to have a 7 optional param which is year , I have tried using latest spring and it show error, so I don't think it is working.
    • If your Linux cron job expression is simple enough, seems like it is possible to just put an 0 in front and it will convert to the spring scheduled tasks expression
      • E.g. Every 5 minutes
        • */5 * * * * Linux cron job
        • 0 */5 * * * * Spring schedule tasks

    Bonus: Spring Schedule Cron Generator

    1. Click on Show code snippet
    2. Click on Run Code snippet
    3. Have fun!

    $('.select2').select2({
      width: '100%'
    });
    
    //// Init ////////////
    $dropdown = $("#secondsSelect");
    for (let i = 1; i < 60; i++) {
      $dropdown.append($("
    .ms-container {
      display: flex;
      flex-direction: column;
      width: 100%;
      padding-left: 3em;
      padding-right: 3em;
      background: none !important;
      padding-bottom: 5em;
    }
    
    
    
    
    
    
    
    
    
    
    

    Spring Schedule Cron Generator

    Seconds:
    Minutes:
    Hours:
    Days of month:
    Months:
    Weekday:
    Result: With a bit of seperation for better viewing:

提交回复
热议问题