创建需要调度的业务类:
package com.jeecms.common.quartzTask; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimerTask; public class MyTimeTask extends TimerTask { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override public void run() { System.out.println("hello:"+df.format(new Date())); } }
SpringBean配置文件:
<bean id="myTimeTask" class="com.jeecms.common.quartzTask.MyTimeTask"/> <bean id="myTimeTaskJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="myTimeTask" /> </property> <property name="targetMethod"> <value>run</value> </property> <property name="concurrent"> <value>false</value> </property> </bean> <bean id="myTimeTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"><!--如果项目报错,把CronTriggerBean换成CronTriggerFactoryBean --> <property name="jobDetail"> <ref local="myTimeTaskJobDetail" /> </property> <property name="cronExpression"> <!-- 每隔一分钟执行一次 --> <!-- 0 */1 * * * ? --> <value>0 0/1 * * * ? </value> </property> </bean> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false" autowire="no"> <property name="triggers"> <list> <ref local="myTimeTaskTrigger" /> </list> </property> <property name="autoStartup"> <value>true</value> </property> </bean>