1.添加pom.xml依赖
spring 依赖包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.13.RELEASE</version>
</dependency>
quartz依赖包
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>1.8.4</version>
</dependency>
2.接口设计
package ceilometer.timeTask;
import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class NovaVmTask extends QuartzJobBean {
private static final Logger logger = Logger.getLogger(NovaVmTask.class);
/**
* Time Task:time update virtualhost!
*
* @param context
* job
* @throws JobExecutionException
* JobExecutionException
*/
@Override
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
long start = System.currentTimeMillis();
try {
System.out.println("________________hello time world!!!!!!!!!!________________________");
} catch (Exception e) {
logger.error("PerforTask Error:", e);
}
}
}
3.applicationContext.xml配置定时任务,主要配置由定义调度工作任务,出发任务调节,启动定时任务组成
<!-- 1.定义调度工作任务 ,可以有好几种实现方式,这里只是其中有一种-->
<bean id="novaVmTask" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>ceilometer.timeTask.NovaVmTask</value>
</property>
<property name="jobDataAsMap">
<map>
<entry key="novaVmService">
<ref bean="novaVmService" />
</entry>
</map>
</property>
</bean>
<!-- 2. 触发任务条件 -->
<bean id="novaVmTaskTrigger" lazy-init="false"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<!-- 要调用的业务 -->
<ref bean="novaVmTask" />
</property>
<property name="cronExpression">
<!-- 每5分钟触发一次 -->
<!-- <value>0 0/1 * * * ?</value> -->
<value>0 0/2 * * * ?</value>
</property>
</bean>
<!-- 3.启动定时器 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
autowire="no">
<property name="quartzProperties">
<props>
<prop key="org.quartz.scheduler.skipUpdateCheck">true</prop>
</props>
</property>
<property name="triggers">
<list>
<ref bean="novaVmTaskTrigger" />
</list>
</property>
</bean>
</beans>