- public class Job2 {
- public void doJob2() {
- System.out.println("不继承QuartzJobBean方式-调度进行中...");
- }
- }
- <bean id="job2"
- class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
- <property name="targetObject">
- <bean class="com.gy.Job2" />
- </property>
- <property name="targetMethod" value="doJob2" />
- <property name="concurrent" value="false" /><!-- 作业不并发调度 -->
- </bean>
- <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
- <property name="jobDetail" ref="job2" />
- <property name="startDelay" value="0" /><!-- 调度工厂实例化后,经过0秒开始执行调度 -->
- <property name="repeatInterval" value="2000" /><!-- 每2秒调度一次 -->
- </bean>
- <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
- <property name="jobDetail" ref="job2" />
- <!—每天12:00运行一次 -->
- <property name="cronExpression" value="0 0 12 * * ?" />
- </bean>
- <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
- <property name="triggers">
- <list>
- <ref bean="cronTrigger" />
- </list>
- </property>
- </bean>
Spring-Task上节介绍了在Spring 中使用Quartz,本文介绍Spring3.0以后自主开发的定时任务工具,spring task,可以将它比作一个轻量级的Quartz,而且使用起来很简单,除spring相关的包外不需要额外的包,而且支持注解和配置文件两种形式,下面将分别介绍这两种方式。第一种:配置文件方式第一步:编写作业类即普通的pojo,如下:Java代码
- import org.springframework.stereotype.Service;
- @Service
- public class TaskJob {
- public void job1() {
- System.out.println(“任务进行中。。。”);
- }
- }
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:task="http://www.springframework.org/schema/task"
- 。。。。。。
- xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
- <task:scheduled-tasks>
- <task:scheduled ref="taskJob" method="job1" cron="0 * * * * ?"/>
- </task:scheduled-tasks>
- <context:component-scan base-package=" com.gy.mytask " />
参考文献:http://gong1208.iteye.com/blog/1773177
实例代码:<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:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.0.xsd"><!-- <task:scheduled-tasks> --><!-- <task:scheduled ref="taskJob" method="addAllBothty" cron="0 0 12 * * ?"/> --> 十二点执行<!-- <task:scheduled ref="taskJob" method="checkYj_hd" cron="0 */3 * * * ? "/> --><!-- </task:scheduled-tasks> --><context:component-scan base-package="com.sinosoft.utils" />