WeChat public number: an excellent waster
If you have any questions or suggestions, please leave a message in the background. I will try my best to solve your problems.
Preface
I haven't seen you for a long time, because I've been busy with chasing girls for a month. It's really a sin. Needless to say, recently, I met the need of using quartz to implement timing tasks in my work. Write it out and share it with you. Take notes.
Spring + quartz to implement timed tasks
Because it's a non Maven old project in development, introduce this approach first. This implementation is more about xml configuration.
1. First, add the two jar s of quartz in the lib directory
2. Configuration of the scheduler
Add the Scheduler configuration in applicationContext.xml
<bean id="MyScheduler"> <property name="triggers"> <list> <ref bean="MyTriggers"></ref> </list> </property> <property name="autoStartup" value="true"></property> </bean>
3.Trigger configuration, where multiple execution every 5 minutes is set
<bean id="MyTriggers"> <property name="jobDetail" ref="MyJobDetail"></property> <property name="cronExpression"> <!--<value>0 */1 * * * ?</value>--> <!--<value>0 */5 * * * ?</value>--> <!-- Executed at 8 a.m --> <!--<value>0 0 8 * * ?</value>--> <!-- Once every 5 minutes --> <value>0 */5 * * * ?</value> </property> </bean>
4. Configuration of JobDetail
<bean id="MyJobDetail"> <!-- Execution class --> <property name="targetObject" ref="MyJobService"></property> <!-- Methods in class --> <property name="targetMethod" value="doSomething"></property> <property name="concurrent" value="false"/> <!-- Whether to allow concurrent execution of tasks. Duty is false Indicates that a new thread cannot be started until the previous thread finishes processing --> </bean>
5. Configuration of business class
<bean id="oltJobService"> <property name="MyDao" ref="MyDao" /> <!-- Injection attribute --> </bean>
6. Business realization
import java.util.Date; public class MyJobService { public void doSomething() { System.out.println("date: " + new Date().getTime()); } }
7. Start the project and you can see that the console prints the current time every five minutes
Posterior language
The above is my understanding of Spring + quartz. I hope it can help you. Finally, if you are interested in Python and Java, please press QR code for a long time and pay attention to the wave. I will try to bring you value. If you think this article can help you even a little, please give me a compliment.