Master Java Task Scheduling: From Crontab to Quartz, Spring and Distributed Solutions
This comprehensive guide explains the fundamentals and practical implementations of task scheduling in Java, covering Linux crontab, JDK Timer, ScheduledExecutorService, Quartz, Spring Schedule, and popular distributed schedulers like SchedulerX, elastic‑job, XXL‑Job and LTS, with code examples and configuration details.
Background
Scheduled tasks are ubiquitous in real‑world applications, such as starting flash sales at midnight on Double 11, automatically pulling payroll data on the first of each month, or sending heartbeat requests over TCP connections.
Typical characteristics of scheduled tasks include triggering at a specific moment, periodic execution at a fixed frequency, and starting at a designated time with recurring intervals.
1. Crontab Scheduler
1.1 Introduction
Crontab is a Linux utility that periodically executes shell scripts or commands. It is widely used by operations engineers and shares a similar expression syntax with Quartz and Spring Schedule.
Command format:
.---------------- minute (0 - 59)
| .------------- hour (0 - 23)
| | .---------- day of month (1 - 31)
| | | .------- month (1 - 12)
| | | | .---- day of week (0 - 6) (Sunday=0 or 7)
| | | | |
* * * * * commandParameters:
minute: 0‑59
hour: 0‑23
day of month: 1‑31
month: 1‑12
day of week: 0‑7 (0 and 7 both represent Sunday)
command: the command to run
Example expressions:
# Every 5 minutes
*/5 * * * * Command
# At the 5th minute of every hour
5 * * * * Command
# Every day at 18:30
30 18 * * * Command
# On the 8th of each month at 07:30
30 7 8 * * Command
# On June 8th each year at 05:30
30 5 8 6 * Command
# Every Sunday at 06:30
30 6 * * 0 Command1.2 Practical Example on CentOS
Install crond: yum -y install cronie yum-cron Create a shell script that logs the current time:
# Create test.sh script
vim /root/shell/test.sh
# Script content
#!/bin/bash
echo `date '+%Y-%m-%d %H:%M:%S'` >> /root/shell/file.logTest the script: sh /root/shell/test.sh Check the log: cat /root/shell/file.log If the log shows timestamps, the script runs correctly. Add the task to crontab:
crontab -e
# Run test.sh every minute
*/1 * * * * sh /root/shell/test.shVerify the task list: crontab -l Reload and restart the crond service:
systemctl reload crond
systemctl restart crondMonitor the log in real time:
tail -f /root/shell/file.logSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
