spring任务调度器的学习笔记

一、基本框架
任务调度器是很多应用系统的幕后功臣,spring的任务调度器是基于对著名的quartz的封装。其基本框架是:
[img]http://dl.iteye.com/upload/attachment/370431/fabec3cf-53c9-3d3c-bfc6-fe2150275a8b.png[/img]

二、加入支持
主要是quartz-xxx.jar包,及相关的包

三、编写第一个程序
需要特别这此编写的代码并不多,只需要写工作(job)类,trigger和scheduler都由spring封装,配置一下即可。
举一个job类的例

package org.examples.mini.task;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class MyJob extends QuartzJobBean {

private MyService service;

public void setService(MyService service) {
this.service = service;
}


public MyService getService() {
return service;
}


@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
service.doSomthing("做点什么事……");
}

}


四、配置,
applicationContext-scheduler.xml

<?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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-lazy-init="true">

<description>Spring任务调度器 </description>

<bean id="myJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>org.examples.mini.task.MyJob</value>
</property>
<property name="jobDataAsMap">
<map>
<entry key="service">
<ref bean="service"/>
</entry>
</map>
</property>
</bean>

<bean id="service" class="org.examples.mini.task.MyServiceImpl"></bean>

<!-- 简单任务触发器 -->
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail">
<ref bean="myJob" />
</property>
<property name="startDelay">
<value>50000</value>
</property>
<property name="repeatInterval">
<value>10000</value>
</property>
</bean>

<bean id="cronTrigger1" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="myJob" />
</property>
<property name="cronExpression">
<value>1 * * * * ? 2010</value><!-- 0 3 17 * * ? 2010 -->
</property>
</bean>

<bean id="cronTrigger2" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="myJob" />
</property>
<property name="cronExpression">
<value>2 * * * * ? 2010</value>
</property>
</bean>

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!-- <ref bean="simpleTrigger" />
<ref bean="cronTrigger1" />-->
<ref bean="cronTrigger2" />
</list>
</property>
</bean>
</beans>


五、启动web应用,即可实现真实的任务调度

六、表示式
CronException完整配置格式为: [秒] [分] [小时] [日] [月] [周] [年]
可参考网络资源:http://blog.csdn.net/feiyun72/article/details/6921366
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值