`
ll_feng
  • 浏览: 384335 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring任务调度器的学习笔记

阅读更多
一、基本框架
任务调度器是很多应用系统的幕后功臣,spring的任务调度器是基于对著名的quartz的封装。其基本框架是:


二、加入支持
主要是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
  • 大小: 11 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics