springschemaSpring定时任务
本文纯属个人见解,是对前面学习的总结,如有描述不正确的地方还请高手指正~
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) 。
文章结束给大家分享下程序员的一些笑话语录:
刹车失灵
有一个物理学家,工程师和一个程序员驾驶着一辆汽车行驶在阿尔卑斯山脉 上,在下山的时候,忽然,汽车的刹车失灵了,汽车无法控制地向下冲去, 眼看前面就是一个悬崖峭壁,但是很幸运的是在这个悬崖的前面有一些小树 让他们的汽车停了下来, 而没有掉下山去。 三个惊魂未定地从车里爬了出来。
物理学家说, “我觉得我们应该建立一个模型来模拟在下山过程中刹车片在高 温情况下失灵的情形”。
工程师说, “我在车的后备厢来有个扳手, 要不我们把车拆开看看到底是什么 原因”。
程序员说,“为什么我们不找个相同的车再来一次以重现这个问题呢?”
---------------------------------
原创文章 By
spring和schema
---------------------------------

浙公网安备 33010602011771号