註冊自定義做業類java
<bean id="myJob" class="me.zhenggg.jobs.MyJob"> <property name="string" value="I am quantz job"/> </bean>
配置JobDetailspring
<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 注入目標對象 --> <property name="targetObject" ref="myJob"/> <!-- 注入目標方法 --> <property name="targetMethod" value="execute"/> </bean>
配置觸發器this
<bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <!-- 注入任務詳情對象 --> <property name="jobDetail" ref="jobDetail"/> <!-- 注入cron表達式,經過這個表達式指定觸發的時間點 --> <property name="cronExpression"> <value>0/5 * * * * ?</value> </property> </bean>
配置調度工廠spa
<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!-- 注入觸發器 --> <property name="triggers"> <list> <ref bean="myTrigger"/> </list> </property> </bean>
package me.zhenggg.jobs; import java.util.Date; /** * @author zhenggg on 2017/5/29. */ public class MyJob { private String string; public String getString() { return string; } public void setString(String string) { this.string = string; } /** * 定時任務具體的業務邏輯 */ public void execute() { System.out.println(this.getString()+new Date()); } }