Home >Java >javaTutorial >Spring uses Quartz task scheduling timer in Java
What is Quartz task scheduling
Quartz is another open source project of the OpenSymphony open source organization in the field of Job scheduling. It can be combined with J2EE and J2SE applications or used alone. Quartz is an open source job scheduling framework written entirely in Java. Don't let the term job scheduling scare you. Even though the Quartz framework incorporates a lot of extra functionality, in its simple form you'll find that it's almost unbearably easy to use!
Actually, he still didn’t explain it clearly. Let me simply say: Quartz job scheduling can implement scheduled tasks. It can implement task schedules similar to Windows, scheduled tasks implemented by our Windows services under .Net, etc. Moreover, it is extremely simple to use when combined with the Spring framework, and it is unbearable, except that the time setting is a bit obscure... It's not important, I'll tell you the solution later.
Now there is a requirement: when a user completes an operation in our system, we reward the user with gold coins, but it is not an instant recharge to the user. Considering performance issues, we use asynchronous or we plan to recharge the user uniformly at one o'clock in the morning. account, because there are relatively few users during this time period. what will you do?
1. Add a GoldQuartz.java file
Of course, you can do the same as me and add a cn.mayongfa.quartz Package specifically for executing scheduled tasks. kind.
The purpose of this class is to automatically add gold coins to users at regular intervals.
@Component public class GlodQuartz { /** * 用户自动加金币 * 每天凌晨一点执行一次 */ @Scheduled(cron = "0 0 1 * * ? ") public void addUserGold() { System.out.println("凌晨一点了,你睡了么?"); } /** * 每隔5秒定时清理缓存 */ @Scheduled(cron = "*/5 * * * * ? ") public void cacheClear() { System.out.println("时间又过去5秒了,真令人伤感..."); } }
Is it done? Well, yes, it's that simple. It mainly involves what @Scheduled's cron means. I will talk about how to write it and how to automatically generate it below, because you can't understand it at all now.
2. Configure the springMVC-servlet.xml file
<!-- 扫描定时作业调度包 --> <task:annotation-driven /> <context:component-scan base-package="cn.mayongfa.quartz"/>
It’s actually that simple and done! It's so easy to use that I can't stand it. A prerequisite for configuring this
xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"
Run to view the results
At this point you can complete the scheduled tasks you want to perform according to your own needs. Then let me explain in detail what @Scheduled's cron means?
Cron expression includes the following 7 fields: seconds, minutes, hours, day of month, month, day of week, year (optional fields).
Cron triggers also make use of a series of special characters:
The backslash (/) character represents an increment value. For example, "5/15" in the seconds field means every 15 seconds starting at second 5.
The question mark (?) character and the letter L character are only available in the day-of-month and day-of-week fields. The question mark indicates that this field does not contain a specific value. So, if you specify a day within the month, you can insert a "?" in the day of the week field to indicate that the day of the week value does not matter. The letter L character is short for last. Put it in the date-in-month field to schedule execution on the last day of the month. In a day-of-the-week field, "L" equals "7" if present by itself, otherwise it represents the last instance of a day-of-the-week within the month. So "0L" means it is scheduled to be executed on the last Sunday of the month.
The letter (W) character in the day-of-month field schedules execution on the weekday closest to the specified value. Putting "1W" in the month date field means scheduling execution on the first working day of the month.
The pound (#) character specifies a specific working day instance for a given month. Put "MON#2" in the day-of-week field to schedule the task on the second Monday of the month.
The asterisk (*) character is a wildcard character, indicating that the field can accept any possible value.
Summary
When you need to execute some code regularly, you can use job scheduling. Quartz was born for this, and it is very convenient to combine with Spring. Quartz allows you to write code quickly. A colleague from our project team told me about it a few days ago. I used it and I was impressed by him.
For more articles related to Spring using Quartz task scheduling timer in Java, please pay attention to the PHP Chinese website!