There are two main types of timed tasks commonly used in Spring
1. Spring integrates Quartz Job
2. The Task that comes with Spring 3.0 after
1. Two ways to implement timing tasks
Quartz job
1. First write a task class
package com.yjf.job;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * @author yjf */public class ExampleJob { private static final Logger logger = LoggerFactory.getLogger(ExampleJob.class); public void run() { logger.info("ExampleJob runs"); }}2. Add Spring integrated Quartz configuration
<!--Integrated quartz job--> <!-- Defining the task--> <bean id="exampleJob"/> <!-- Method for specifying the bean--> <bean id="exampleJobMethod"> <property name="targetObject" ref="exampleJob"/> <property name="targetMethod" value="run"/> <property name="concurrent" value="true"/> </bean> <!-- There are two types of triggers for Quartz --> <!-- The first SimpleTriggerFactoryBean only supports calling tasks at a certain frequency, such as every 10 seconds --> <!--<bean id="simpleTrigger">--> <!--<property name="jobDetail" ref="exampleJobMethod"/>--> <!--<property name="startDelay" value="0"/>--> <!--<property name="repeatInterval" value="10"/>--> <!--</bean>--> <!-- The second CronTriggerFactoryBean can be executed in a custom way using cronExpression expression--> <bean id="exampleTrigger"> <property name="jobDetail" ref="exampleJobMethod"/> <property name="cronExpression" value="0/10 * * * * ?"/> </bean> <!-- Configure the Scheduling Factory--> <bean> <property name="triggers"> <list> <ref bean="exampleTrigger"/> </list> </property> </bean>
Spring Task
1. Write task categories
package com.yjf.job;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;/** * @author yjf44568 */@Componentpublic class SearchUserJob{ private Logger logger = LoggerFactory.getLogger(this.getClass()); @Scheduled(cron = "*/5 * * * ?") public void doIt(){ logger.info("I'm doing"); }}2. Add task configuration file
<!-- Configure with annotation--> <!-- Enable task timing tasks --> <task:annotation-driven/> <!--Scan the location of the package where the job is located --> <context:component-scan base-package="com.yjf.job"/>
2. Compare the advantages and disadvantages of the two
Quartz Job Features
1. Powerful function, but slightly cumbersome configuration
2. Quartz creates a new task object every time it executes
3. An exception is thrown during a Quartz execution task, which does not affect the execution of the next task. When the next execution time comes, the timer will execute the task again.
Spring Task Features
1. Use @Scheduled annotation and a small number of configurations to implement it, simple to use
2. Task uses the same task object each time it executes
3. Once a TimerTask exception is thrown during execution, the entire timer life cycle ends, and the timer task will never be executed again in the future.
At present, I personally use Quartz Job more
The above article "Spring's implementation method of integrating Quartz Job and Spring Task" is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.