Spring timing task implementation code in Java
- 2020-04-01 01:34:29
- OfStack
import org.apache.log4j.*;
public class TaskJob {
public static Logger log = Logger
.getLogger(TaskJob.class);
public void SayHello() {
// TODO Auto-generated method stub
try {
log.info(" Task starting >........");
//Business logic code call
System.out.println(" time [" + new java.util.Date().toLocaleString()
+ "]-----> Hello everyone! ");
log.info(" End of task !");
} catch (Exception e) {
log.error(" An exception occurred in the processing task ", e);
}
}
}
2. The next configuration in spring:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean name="taskJob" class="util.TaskJob" />
<bean id="methodInvokingJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="taskJob" />
</property>
<property name="targetMethod">
<value>SayHello</value>
</property>
</bean>
<!-- Configure trigger -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!-- You can't go directly to properties here jobDetail Referenced in the taskJob Because he's asking for one jobDetail Type of object, so we have to pass MethodInvokingJobDetailFactoryBean To turn the -->
<property name="jobDetail">
<ref bean="methodInvokingJobDetail" />
</property>
<!-- Every day, 8 Point to the 21 At every 1 Minute trigger, see the appendix for details -->
<property name="cronExpression">
<value>0 * 08-21 * * ?</value>
</property>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- Add trigger -->
<property name="triggers">
<list>
<ref local="cronTrigger" />
</list>
</property>
</bean>
</beans>
3. Test the executed class, and once the spring configuration file is loaded, you can see the timing task run.
package util;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestApp {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(" loading spring The configuration file ....");
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println(" The configuration file is loaded !");
// ApplicationContext context2 = new ClassPathXmlApplicationContext("test/timerTask/quartzTimer.xml");
}
}
To run in a web project, add the following code to web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Here are some instructions copied online:
The special character allowed by the field allowed value
Minus 59 seconds, minus PI times PI over PI
0 minus 59, minus PI times PI over PI
Zero minus 23 hours, minus * /
Date 1-31, - *? W/L C
January 1-12 or jan-dec, minus * /
Week 1-7 or SUN-SAT - *? L/C #
Year (optional) left blank, 1970-2099, minus * /
Expression meaning
"0, 0, 12 * *?" Triggered every day at 12 noon
"0 15 10? * *" triggered at 10:15 am every day
"0, 15, 10 * *?" Triggered at 10:15 am each day
"0, 15, 10 * *? *" triggered at 10:15 am every day
"0, 15, 10 * *? Trigger every day at 10:15 am in 2005
"0 * 14 * *?" Triggered every minute between 2 p.m. and 2:59 p.m
"0, 0, 5, 14 * *?" Trigger every 5 minutes between 2 p.m. and 2:55 p.m
"0, 0, 5, 14,18 * *?" Trigger every 5 minutes between 2 p.m. and 2:55 p.m. and 6 p.m. and 6:55 p.m
"0, 0, minus 5, 14 * *?" Trigger every minute between 2 p.m. and 2:05 p.m
"0 10 44 14? 3 WED" on wednesdays in March every year at 2:10 PM and 2:44 PM
"0 15 10? * mon-fri "triggers at 10:15 am Monday through Friday
"0, 15, 10, 15 times?" Triggered at 10:15 am on the 15th of each month
"0, 15, 10 L *?" Triggered at 10:15 am on the last day of each month
"0 15 10? * 6L" triggers at 10:15 am on the last Friday of each month
"0 15 10? * 6L 2002-2005" triggered at 10:15 am on the last Friday of each month from 2002 to 2005
"0 15 10? * 6#3" triggers at 10:15 am on the third Friday of the month