Scheduled Tasks
Spring task, which can be likened to a lightweight quartz, is simple to use, requires no additional packages in addition to spring-related packages, and supports annotations and configuration files in two types
Demo
Use the simplest example here
XML mode
package com.lee.spring_test.task;import java.util.Date;publicclass TaskXml { publicvoidxmlCronTask() { System.out.println(new": 敌军还有30秒到达!"); }}
Applicationcontext-task.xml
<?xml version= "1.0" encoding= "UTF-8"?><Beans xmlns="Http://www.springframework.org/schema/beans" Xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance" Xmlns:task="Http://www.springframework.org/schema/task" Xmlns:context="Http://www.springframework.org/schema/context" xsi:schemalocation="Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd/HTTP Www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd " > <!--cronexpression configuration instructions, specific use and parameters please Baidu Google field allow the special characters allowed by the number of seconds 0-5 9,-*/minute 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 # Year (optional) leave blank, 1970-2099,-*/-interval * wildcard? You don't want to set that field below just a few expressions of the cron expression meaning "0 0 12 * *?" Trigger "0 15 10" At 12 o'clock noon every day? * * "10:15 per day" 0 15 10 * *? " Trigger "0 15 10 * * * per day 10:15? * "Trigger every day 10:15" 0 15 10 * *? 2005 "0 * 14 * * For 2005 Year 10:15 Daily Every day from 2 o'clock in the afternoon to 2:59 every minute trigger "0 0/5 14 *?" Daily from 2 o'clock in the afternoon to 2:55 minutes, every 5 minutes trigger "0 0/5 14,18 * *?" Every 5 minutes from 2 o'clock in the afternoon to 2:55 and 6 to 6:55 in each day "0 0-5 14 * *?" Daily from 14:00 to 14:05 "0 10,44 14 per minute"? 3 WED "March 14:10 and 14:44 triggers per Wednesday" 0 15 10? * MON-FRI "10:15 trigger--> per Monday, Tuesday, Wednesday, Thursday, Friday; <bean id="XmlTask" class="Com.lee.spring_test.task.TaskXml "/ > <task:scheduled-tasks> <task:scheduled ref= "xmlTask" method="Xmlcrontask" cron="0/30 * * * * *"/> </task:scheduled-tasks></Beans>
Test.java
package com.lee.spring_test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;publicclass Test { publicstaticvoidmain(String[] args) { new ClassPathXmlApplicationContext("applicationContext.xml"); }}
Annotation method
package com.lee.spring_ Test.task; import java.util.Date; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component ( "task" ) public class taskannotation { @Scheduled (cron= "0/30 * * * * *" )
public
void
xmlcrontask () {System.out.println (
new Date () +
": the enemy has 30 seconds to Up! ");}}
Applicationcontext-task.xml
...<context:component-scan base-package="com.lee.spring_test" /><task:annotation-driven/>...
Spring's task