1编写任务类和运行任务的方法
@Component("cmsContentJob")
public class CmsContentJob {
protected void cmsContentCollect(){
......
}
}
2 配置spring的定时任务的作业类(spring-schedule.xml)
<bean id="CmsContentJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="cmsContentJob" /> <!--任务的类 -->
<property name="targetMethod" value="cmsContentCollect" /> <!--任务执行的方法 -->
<property name="concurrent" value="false"/> <!-- 任务串行执行 ,不并发执行-->
</bean>
3 配置作业的调度方式-触发器(spring-schedule.xml)
<bean id="CmsContentJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="CmsContentJobDetail"></property>
<property name="cronExpression"> <value>0 0 2 * * ?</value> </property>
</bean>
4 配置调度工厂
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="CmsContentJobTrigger"/> <!--之前配置的触发器的名字 -->
</list>
</property>
</bean>
5 启动tomcat,运行程序,到指定时间点,会自动执行你的job
还有一种配置,需要作业类继承 org.springframework.scheduling.quartz.QuartzJobBean
1作业类
public class VisitorTotalJob extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
.....
}
}
2 配置作业类
<bean id="VisitorTotalJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.whty.eop.cms.rest.common.job.VisitorTotalJob"></property>
</bean>
剩下的配置和第一种一样。