In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
In some work need to use the timer, Spring very good integration of the timer function!
Using Quartz in Spring, this article introduces Spring3.0 after independent development of the timing task tool, spring task, it can be compared to a lightweight Quartz, and easy to use, in addition to spring-related packages do not require additional packages, the following describes two ways to achieve Spring timer function, one is based on xml configuration, the other is based on annotations, we choose suitable for your own project.
One: based on xml configuration
1: write ordinary pojo classes
Package com.aflyun.web.task
Import org.springframework.stereotype.Component
Import org.springframework.stereotype.Service
@ Component
/ / @ Service is fine
Public class TaskCool {
/ * *
The first timer test method
, /
Public void testJob () {
System.out.println ("test first taskJob....")
}
}
2: configure the xml file
Note: the namespace and schema of task must be included in the main configuration files above.
The above ref= "taskCool" defaults to the lowercase value of the TaskCool class. If you need to modify it, you can modify it in @ Component, such as the following
@ Component ("taskCoolJob") then ref= "taskCoolJon" at this time.
So far, the configuration is based on xml, and you can see the effect of running it!
Second, based on annotation.
The use of annotations does not need to be configured in the xml file for each task class written, which is much more convenient. Using @ Scheduled of Spring, let's first take a look at the definition of @ Scheduled in the source file:
@ Target ({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.ANNOTATION_TYPE})
@ Retention (RetentionPolicy.RUNTIME)
@ Documented
Public @ interface Scheduled
{
Public abstract String cron ()
Public abstract long fixedDelay ()
Public abstract long fixedRate ()
}
Cron: indicates the specified cron expression. (the cron type indicates that the specified time trigger triggers the task execution! )
FixedDelay: represents the interval, in milliseconds, between the completion of the previous task and the start of the next task.
FixedRate: represents the interval, in milliseconds, from the start of the previous task to the start of the next task.
The following is a specific configuration process:
1: write the pojo class
Package com.tclshop.cms.center.web.task
Import org.springframework.scheduling.annotation.Scheduled
Import org.springframework.stereotype.Component
@ Component
Public class WebTask {
/ / execute every five seconds
@ Scheduled (cron = "0ram 5?")
Public void TaskJob () {
System.out.println ("test second annotation style...")
}
}
2: configure the xml file
The contents of the relevant configuration file are posted below:
Note: in theory, you only need to add this configuration, and other parameters are not required.
When the configuration is complete, you can see the effect by running it.
Summary: the use of this kind of timer, there is no need to integrate other parent timers, easy to use! It's also very powerful!
Attached: configuration description of cronExpression
Special characters allowed for field allowable values
Seconds 0-59,-/
Score 0-59,-/
0-23 hours,-/
Date 1-31, -? / L W C
Month 1-12 or JAN-DEC,-/
Monday-7 or SUN-SAT, -? / L C #
Year (optional) left blank, 1970-2099,-* /
Example:
Meaning of CRON expression
"0.012?" Triggered at 12:00 every day
"0 15 10?" triggered at 10:15 every morning
"0 15 10?" Triggered at 10:15 every morning
"0 15 10?" triggered at 10:15 every morning
"0 15 10? 2005" triggered at 10:15 every morning in 2005
"0 14?" Trigger every minute from 2pm to 02:59 every day
"0 0 take 5 14?" Trigger every 5 minutes from 2pm to 2:55 every day
"0 010 5 14 14 18?" Triggered every 5 minutes between 2pm to 2:55 and 6pm to 06:55 every day
"0-5-14?" Triggered every minute from 14:00 to 14:05 every day
010 WED 44 14? 3 trigger at 14:10 and 14:44 on every Wednesday in March
"0 15 10? * MON-FRI" triggers at 10:15 every Monday, Tuesday, Wednesday, Thursday and Friday
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.