Spring定时任务的功能很强大,上次简单应用一下,给大家分享下,希望大家多多交流!
这是一个定时打印时间控制台,这是一个简单定时任务!
请看程序的运行原码:
首先新建一个类:TellingTheTimeJob这个类是继承于Spring重写executeInternal这个方法。
package jobs;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import service.ITellingTheTimeService;
/**
* @ProjectName:报时Demo
* @ClassName:TellingTheTimeJob
* @Description:
* @author:Sheep
* @date:2012-4-19 下午03:58:11
* @Modifier:
* @Modify Date:
* @Modify Note:
* @version
*/
public class TellingTheTimeJob extends QuartzJobBean {
private ITellingTheTimeService tellingTheTimeService = null;
@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
//调用报时方法
this.tellingTheTimeService.tellingTheTime();
}
public ITellingTheTimeService getTellingTheTimeService() {
return tellingTheTimeService;
}
public void setTellingTheTimeService(
ITellingTheTimeService tellingTheTimeService) {
this.tellingTheTimeService = tellingTheTimeService;
}
}
建立一个接口ITellingTheTimeService,功能为了实现定时调用
package service;
/**
* @ProjectName:报时Demo
* @ClassName:ITellingTheTimeService
* @Description:
* @author:Sheep
* @date:2012-4-19 下午03:59:37
* @Modifier:
* @Modify Date:
* @Modify Note:
* @version
*/
public interface ITellingTheTimeService {
/**
* @Title: tellingTheTime
* @Description: 报当前时间
* @throws
*/
void tellingTheTime();
}
实现类
package service.impl;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import service.ITellingTheTimeService;
/**
* @ProjectName:报时Demo
* @ClassName:TellingTheTimeServiceImpl
* @Description:
* @author:Sheep
* @date:2012-4-19 下午03:59:06
* @Modifier:
* @Modify Date:
* @Modify Note:
* @version
*/
public class TellingTheTimeServiceImpl implements ITellingTheTimeService {
/**@Description: 报时
* @see service.ITellingTheTimeService#tellingTheTime()
*/
public void tellingTheTime() {
// TODO Auto-generated method stub
Calendar now = Calendar.getInstance();
System.out.println("现在是北京时间:"+this.format(now.getTime()));
}
public String format(Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(date);
}
}
applicationContext 配置文件
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="tellingTheTimeService" class="service.impl.TellingTheTimeServiceImpl"/>
<!-- 配置一个Job -->
<bean id="tellTheTimeJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="jobs.TellingTheTimeJob"/>
<property name="jobDataAsMap">
<map>
<entry key="tellingTheTimeService" value-ref="tellingTheTimeService"></entry>
</map>
</property>
</bean>
<!-- 简单的触发器 -->
<bean id="simpleTellTheTimeTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail">
<ref bean="tellTheTimeJob" />
</property>
<!-- 以毫秒为单位,启动后一分钟触发 -->
<property name="startDelay">
<value>60000</value>
</property>
<!-- 每间隔一分钟触发一次 -->
<property name="repeatInterval">
<value>60000</value>
</property>
</bean>
<!-- 复杂的触发器 -->
<bean id="complexTellTheTimeTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="tellTheTimeJob"/>
</property>
<property name="cronExpression">
<!-- 这里是重点,可以自定义表达式实现定时触发。以下含义是每分钟触发一次 -->
<value>0 0/1 * * * ?</value>
</property>
</bean>
<!-- Spring触发工厂 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="complexTellTheTimeTrigger"/>
<!-- ....下面可以继续添加其他触发器 -->
</list>
</property>
</bean>
</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">
<!-- 配置Spring XML上下文路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
这样就可以实现spring定时任务了,所引用的jar是:commons-collections.jar,commons-logging-1.0.4.jar,log4j-1.2.15.jar,quartz-1.6.1-RC1.jar和spring.jar(2.5) 。