{"id":19557435,"url":"https://github.com/ramostear/spring-boot-schedule-tutorial","last_synced_at":"2025-07-14T00:37:41.330Z","repository":{"id":105269278,"uuid":"175030976","full_name":"ramostear/spring-boot-schedule-tutorial","owner":"ramostear","description":"spring boot schedule tutorial","archived":false,"fork":false,"pushed_at":"2019-03-11T15:48:47.000Z","size":53,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-26T08:15:39.915Z","etag":null,"topics":["spring-boot-tutorial"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ramostear.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-03-11T15:36:25.000Z","updated_at":"2019-03-11T15:50:29.000Z","dependencies_parsed_at":"2023-05-22T04:30:19.330Z","dependency_job_id":null,"html_url":"https://github.com/ramostear/spring-boot-schedule-tutorial","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ramostear/spring-boot-schedule-tutorial","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ramostear%2Fspring-boot-schedule-tutorial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ramostear%2Fspring-boot-schedule-tutorial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ramostear%2Fspring-boot-schedule-tutorial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ramostear%2Fspring-boot-schedule-tutorial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ramostear","download_url":"https://codeload.github.com/ramostear/spring-boot-schedule-tutorial/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ramostear%2Fspring-boot-schedule-tutorial/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265228134,"owners_count":23731067,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["spring-boot-tutorial"],"created_at":"2024-11-11T04:42:31.049Z","updated_at":"2025-07-14T00:37:41.307Z","avatar_url":"https://github.com/ramostear.png","language":"Java","readme":"# Spring Boot（十五）— 任务调度\n\n任务调度（也可以称为定时任务）是指在特定的时间段去执行一个规定的任务过程。Spring Boot为开发者提供了一个更优雅的方式创建任务调度程序。在本章节中，我们将学习使用Spring Boot来创建任务调度程序。\n\n任务调度分为两种类型，一种是间隔时间执行的任务，如每隔3秒执行一次任务程序；另外一种时指定具体时间的任务，如在每天的凌晨整点备份数据库。\n\n\n\n## Cron表达式\n\n在开始讲解定时任务之前，先来看一下定时任务中的Cron表达式的相关内容。Cron表达式用于配置CronTrigger实例，它是**org.quartz.Trigger**的子类。Cron表达式被放置在**@Scheduled** 注释标签中，下面的代码给出了一个cron表达式的样例：\n\n```java\n@Scheduled(cron = \"0/5 * 22 * * ?\")\npublic void cronJobSchedule(){\n    SimpleDateFormat sdf = new SimpleDateFormat(\"yyyy-MM-dd HH:mm:ss.SSS\");\n    Date now = new Date();\n    logger.info(\"Java cron job expression scheduler::\"+sdf.format(now));\n}\n```\n\n在cron表达式中，一共有七位表达式参数，我们将使用一张表格来了解各个参数的用途：\n\n| 位数   | 说明               | 范围                |\n| ------ | ------------------ | ------------------- |\n| 第一位 | 表示秒             | 取值范围：0-59      |\n| 第二位 | 表示分钟           | 取值范围：0-59      |\n| 第三位 | 表示小时           | 取值范围：0-23      |\n| 第四位 | 表示日期           | 取值范围：1-31      |\n| 第五位 | 表示月份           | 取值范围：1-12      |\n| 第六位 | 表示星期           | 取值范围：1-7       |\n| 第七位 | 表示年份，通常置空 | 取值范围：1970-2099 |\n\n\u003e 说明，在第六位星期参数中，1表示的是星期日，除使用数字表示外，还可以使用表示星期的英文缩写来设置\n\n了解了cron表达式的语法规则后，我们再来了解一下表达式中各种占位符的含义。cron表达式中一共可以使用的占位符有5个，如下表所示：\n\n| 占位符     | 说明                           | 示例                                               |\n| ---------- | ------------------------------ | -------------------------------------------------- |\n| （星号）*  | 可以理解为一个周期             | 每秒、没分、每小时等                               |\n| （问好）？ | 只能出现在日期和星期两个位置中 | 表示时间不确定                                     |\n| （横线）-  | 表示一个时间范围               | 如在小时中10-11，表示从上午10点到上午11点          |\n| （逗号）， | 表示一个列表值                 | 如在星期中使用：1,3,5 表示星期一、星期三和星期五   |\n| （斜杠）/  | 表示一个开始时间和间隔时间周期 | 在分钟中使用：0/15 表示从0分开始，每15分钟运行一次 |\n\n下面将列举一些示例来说明cron表达式和占位符：\n\n| 表达式          | 说明                                                         |\n| --------------- | ------------------------------------------------------------ |\n| 0 0 0 * * ？    | 每天00:00:00执行任务                                         |\n| 0 30 10 * * ？  | 每天上午10:30:00执行任务                                     |\n| 0 30 10 ？ * *  | 每天上午10:30:00执行任务                                     |\n| 0 0/15 10 * * ? | 每天上午10:00:00、10:15:00、10:30:00和10:45:00这四个时间点执行任务 |\n| 0 0 0 ? * 1     | 每个星期天的凌晨整点执行任务                                 |\n| 0 0 0 ？ * 1#3  | 每个月的第三个星期天的凌晨整点执行任务                       |\n\n你可以访问[RT社圈](https://www.ramostear.com)阅读关于Spring Boot更多的教程信息。你可以访问[https://www.ramostear.com/archive/spring-boot/post/schedule.html](https://www.ramostear.com/archive/spring-boot/post/schedule.html)查看原文信息。\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Framostear%2Fspring-boot-schedule-tutorial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Framostear%2Fspring-boot-schedule-tutorial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Framostear%2Fspring-boot-schedule-tutorial/lists"}