Home Java javaTutorial Spring uses Quartz task scheduling timer in Java

Spring uses Quartz task scheduling timer in Java

Feb 07, 2017 pm 03:09 PM

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秒了,真令人伤感...");
 }
}
Copy after login

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"/>
Copy after login

It’s actually that simple and done! It's so easy to use that I can't stand it. A prerequisite for configuring this is that the beans declaration in your xml file must have:

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"
Copy after login

Run to view the results

Spring uses Quartz task scheduling timer in Java

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? Apr 19, 2025 pm 09:51 PM

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...

See all articles