Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use timer in Spring

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to use timers in Spring. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

One: based on xml configuration

1: write ordinary pojo class package com.aflyun.web.task;import org.springframework.stereotype.Component;import org.springframework.stereotype.Service;@Component//@Service can 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;@Componentpublic class WebTask {/ / execute @ Scheduled every five seconds (cron = "0go 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

Field allowed special characters 0-59 seconds,-* / minutes 0-59,-* / hour 0-23,-* / date 1-31,-*? / L W C month 1-12 or JAN-DEC -* / week 1-7 or SUN-SAT,-*? / L C # (optional) left blank, 1970-2099,-* /

Example:

The CRON expression means "0 012 * *?" Trigger "01510? *" at 12:00 every day "01510 * *?" at 10:15 every morning. Trigger "015 10 * *? *" at 10:15 every morning "0 15 10 * *? 2005" at 10:15 every morning "0 * 14 * *?" at 10:15 every morning in 2005. Every minute from 2 p.m. to 02:59 every day, the trigger will be "0 0 take 5 14 * *?" Every day from 2: 00 p.m. to 2:55, every 5 minutes, the trigger "00 Universe 5 14 18 * *?" Trigger "00-5 14 *?" every 5 minutes from 2 p.m. to 2:55 and 6 p.m. to 06:55 every day. Trigger "010 WED 44 14? 3 WED" every minute from 14:00 to 14:05 every March 14:10 and 14:44 on Wednesdays trigger "01510? * MON-FRI" trigger at 10:15 on Mondays, Tuesdays, Wednesdays, Thursdays and Fridays this is how to use timers in the Spring shared by Xiaobian. If you happen to have similar doubts, please refer to the above analysis for understanding. If you want to know more about it, you are welcome to follow the industry information channel.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report