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("处理任务开始>........"); // 业务逻辑代码调用 System.out.println("时间[" + new java.util.Date().toLocaleString() + "]----->大家好啊!"); log.info("处理任务结束!"); } catch (Exception e) { log.error("处理任务出现异常", e); } } }
2. Next, configure it 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> <!-- 配置触发器 --> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <!-- 这里不可以直接在属性jobDetail中引用taskJob,因为他要求的是一个jobDetail类型的对象,所以我们得通过MethodInvokingJobDetailFactoryBean来转一下 --> <property name="jobDetail"> <ref bean="methodInvokingJobDetail" /> </property> <!-- 每天的8点到21点每隔1分钟触发,具体说明见附录 --> <property name="cronExpression"> <value>0 * 08-21 * * ?</value> </property> </bean> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!-- 添加触发器 --> <property name="triggers"> <list> <ref local="cronTrigger" /> </list> </property> </bean> </beans>
3. To test the execution class, you can see the scheduled tasks running as long as you load the spring configuration file.
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("加载spring配置文件...."); ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); System.out.println("加载配置文件完毕!"); // ApplicationContext context2 = new ClassPathXmlApplicationContext("test/timerTask/quartzTimer.xml"); } }
If you want to run it in a web project, you also need to add the following code to web. Characters
seconds 0-59, - * / minutes 0-59, - * /
hours 0-23, - * /
dates 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, - * /
Expression meaning
"0 0 12 * * ?" Triggered every day at 12 noon
"0 15 10 ? * *" Triggered every day at 10:15 am
"0 15 10 * * ?" Every morning Triggered at 10:15
"0 15 10 * * ? *" Triggered every day at 10:15 AM
"0 15 10 * * ? 2005" Triggered every day at 10:15 AM in 2005
"0 * 14 * * ?" Triggers every 1 minute from 2 pm to 2:59 pm every day
"0 0/5 14 * * ?" Triggers every 5 minutes from 2 pm to 2:55 pm every day
"0 0/5 14,18 * * ?" Triggers every 5 minutes from 2pm to 2:55pm and every 5 minutes from 6pm to 6:55pm
"0 0-5 14 * * ?" " Triggers every 1 minute from 2pm to 2:05pm every day
"0 10,44 14 ? 3 WED" Triggers every Wednesday in March at 2:10pm and 2:44pm
"0 15 10 ? * MON-FRI" Triggered at 10:15 AM from Monday to Friday
"0 15 10 15 * ?" Triggered at 10:15 AM on the 15th of every month
"0 15 10 L * ?" Every Triggered at 10:15 am on the last day of the month
"0 15 10 ? * 6L" Triggered at 10:15 am on the last Friday of the month
"0 15 10 ? * 6L 2002-2005" 2002 to 2005 Triggered at 10:15 am on the last Friday of each month of the year
"0 15 10 ? * 6#3" Triggered at 10:15 am on the third Friday of each month
More spring timing in java For articles related to task implementation code, please pay attention to the PHP Chinese website!