Used in the timing of project tasks, write an essay about the record.
First, add the beans label Spring configuration file ApplicationContext.xml file
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.1.xsd
Currently I understand there are two ways to use scheduled tasks:
1. Declare call regular tasks in the configuration file
In the first statement ApplicationContext.xml file called bean
<bean id="quartzController" class="com.bizdata.common.socket.web.resource.QuartzTaskController"></bean>
java code to call the following methods and
import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.stereotype.Controller; @Controller public class QuartzTaskController { public void sysTime(){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.format(new Date())); } }
Then declare the object method call and call the object
<! - definition of the call object and the call object methods -> < the bean ID = "myJobDetailA" class = "org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" > < Property name = "the targetObject" REF = "quartzController" > < ! - execution timing class -> </ Property > < Property name = "the targetMethod" value = "sysTime" > </ Property > <-! execution timing of the process -> <property name="concurrent" value="false" /> <! - whether to allow concurrent task execution. When it is false, meaning a thread must wait until after the pre-processed and then start a new thread -> </ bean > <-! Define the trigger time -> < bean the above mentioned id = "myTriggersA" class = "ORG .springframework.scheduling.quartz.CronTriggerFactoryBean " > < Property name =" jobDetail " ref =" myJobDetailA " > </ Property > < Property name =" cronExpression " > < value > 0 00 6 * *? </ value > <! - time size small to large order, beginning from the second, the order of seconds, minutes, hours, days, months, years * any? Unlimited -> </ Property > </ bean >
Set the timing to complete the above tasks, 6:00 every morning calls.
2. Notes calling (recommended method will save a lot of configuration, ease of use)
ApplicationContext.xml file automatically scan bean declared:
<-! Open comments inject bean support -> < context: Annotation-config /> < context: the Component-Scan Base-Package Penalty for = "com.bizdata" />
Meanwhile annotations add configuration call
<Task: Annotation-Driven /> <-! timer comment ->
java code
@Scheduled(cron="0 0 22 * * ?") public boolean getDataCenterData(){ Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DATE, -1); return getDataCenterData(calendar.getTime()); }
The timing of completion of tasks, 22:00 every day calls.
Notes manner using the above configuration will print out an exception when you call scheduled task log, but does not affect the use, you can add it in the log4j.properties file log level filtering can be.
# Timers log level
log4j.logger.org.springframework.scheduling = INFO