【Spring中使用SimplerTrigger】
【QuartzTask.java】
package com.higgin.task; import java.text.SimpleDateFormat; import java.util.Date; public class QuartzTask { public QuartzTask() { System.out.println("QuartzTask 构造方法---"); } //这个就是要被SimpleTrigger定时触发执行的方法 public void doSimpleTriggerTask(){ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("doSimpleTriggerTask()定时执行方法:"+sdf.format(new Date())); } //这个就是要被CronTrigger定时触发执行的方法 public void doCronTriggerTask(){ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("doCronTriggerTask()定时执行方法:"+sdf.format(new Date())); } }
【simpleTriggerTask-spring.xml】
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 注册一个普通的bean --> <bean id="quartzTask" class="com.higgin.task.QuartzTask"></bean> <!-- 1.制定任务信息 --> <bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 设置执行对象 --> <property name="targetObject" ref="quartzTask"></property> <!-- 设置执行对象中对应的执行方法 --> <property name="targetMethod" value="doSimpleTriggerTask"></property> <!-- 是否可以同步执行:不可同步执行 --> <property name="concurrent" value="false"></property> </bean> <!-- 2.制定任务执行时机(任务执行触发器) --> <bean id="simplerTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <!-- 设置任务详细 --> <property name="jobDetail" ref="myJobDetail"></property> <!-- 设置任务延迟执行时间 :延迟2秒后执行(每隔2秒执行一次)--> <property name="repeatInterval" value="2000"></property> </bean> <!-- 3.设置调度工厂 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!-- 可以通过该属性注册多个Trigger --> <property name="triggers"> <list> <ref bean="simplerTrigger"/> </list> </property> </bean> </beans>
【CronTriggerTest.java】
package com.higgin.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class CronTriggerTest { public static void main(String[] args) { ApplicationContext context =new ClassPathXmlApplicationContext("cronTriggerTask-spring.xml"); } }
【运行结果】
【Spring中使用CronTrigger】
【QuartzTask.java,同上】
package com.higgin.task; import java.text.SimpleDateFormat; import java.util.Date; public class QuartzTask { public QuartzTask() { System.out.println("QuartzTask 构造方法---"); } //这个就是要被SimpleTrigger定时触发执行的方法 public void doSimpleTriggerTask(){ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("doSimpleTriggerTask()定时执行方法:"+sdf.format(new Date())); } //这个就是要被CronTrigger定时触发执行的方法 public void doCronTriggerTask(){ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("doCronTriggerTask()定时执行方法:"+sdf.format(new Date())); } }
【cronTriggerTask-spring.xml】
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 注册一个普通的bean --> <bean id="quartzTask" class="com.higgin.task.QuartzTask"></bean> <!-- 1.制定任务信息 --> <bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 设置执行对象 --> <property name="targetObject" ref="quartzTask"></property> <!-- 设置执行对象中对应的执行方法 --> <property name="targetMethod" value="doCronTriggerTask"></property> <!-- 是否可以同步执行:不可同步执行 --> <property name="concurrent" value="false"></property> </bean> <!-- 2.制定任务执行时机(任务执行触发器) --> <bean id="cronTrigger1" class="org.springframework.scheduling.quartz.CronTriggerBean"> <!-- 设置任务详细 --> <property name="jobDetail" ref="myJobDetail"></property> <!-- 设置任务执行时机,cron表达式--> <property name="cronExpression" value="0/3 * * * * ?"></property> </bean> <bean id="cronTrigger2" class="org.springframework.scheduling.quartz.CronTriggerBean"> <!-- 设置任务详细 --> <property name="jobDetail" ref="myJobDetail"></property> <!-- 设置任务执行时机,cron表达式 :秒 分 时 日 月 周 年(可选) --> <property name="cronExpression" value="0 21 16 20C * ?"></property> </bean> <!-- 3.设置调度工厂 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronTrigger1"/> <!-- <ref bean="cronTrigger2"/> --> </list> </property> </bean> </beans>
【CronTriggerTest.java】
package com.higgin.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class CronTriggerTest { public static void main(String[] args) { ApplicationContext context =new ClassPathXmlApplicationContext("cronTriggerTask-spring.xml"); } }
【运行结果】