這裏只是做簡單的記錄如何實現。
一、基於配置文件的實現
①編寫需要調度的類
package com.study; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; //@Component public class QuartzJob { public QuartzJob(){ System.out.println("Quartzjob創建成功"); } //@Scheduled(cron = "0/1 * * * * ? ") public void run(){ System.out.println("Quartz執行的任務調度"); } }
注:裏面的註解是後面的是註解的實現中用到的
②設置配置文件spring-quartz.xml
<!--?xml version="1.0" encoding="UTF-8"?--> <beans> <bean id="quartzJob" class="com.study.QuartzJob"></bean> <!-- 定義調用對象和調用對象的方法 --> <bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 調用的類 --> <property name="targetObject"> <ref bean="quartzJob"> </ref></property> <!-- 調用類中的方法 --> <property name="targetMethod"> <value>run</value> </property> </bean> <!-- 定義觸發時間 --> <bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="jobtask"> </ref></property> <!-- cron表達式 --> <property name="cronExpression"> <!--<value>10,15,20,25,30,35,40,45,50,55 * * * * ?</value>--> <value>0/1 * * * * ?</value> </property> </bean> <!-- 總管理類 如果將lazy-init='false'那麼容器啓動就會執行調度程序 --> <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="doTime"> </ref></list> </property> </bean> </beans>
注意cron表達式,這裏配置的是每隔1s執行。
③啓動spring容器
package com.study; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { System.out.println("啓動spring容器"); ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring-quartz.xml"); } }
二、基於註解的實現
①配置需要調度的類,並添加註解
package com.study; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class QuartzJob { public QuartzJob(){ System.out.println("Quartzjob創建成功"); } @Scheduled(cron = "0/1 * * * * ? ") public void run(){ System.out.println("Quartz執行的任務調度"); } }
②添加配置文件
<!--?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:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd "> <task:annotation-driven> <context:component-scan base-package="com.study"> <context:annotation-config> </context:annotation-config></context:component-scan></task:annotation-driven></beans>
③啓動容器,這裏通過配置web.xml啓動
<!--?xml version="1.0" encoding="UTF-8"?--> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-quartz2.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>