spring quartz定时器

来源:互联网 发布:淘宝卖家寄快递的软件 编辑:程序博客网 时间:2024/04/28 17:59



第一种使用标签形式,非常简单

此方法必须在spring3.0以上使用

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/task 
http://www.springframework.org/schema/task/spring-task-3.0.xsd">


<!--


这里加入了
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task 
http://www.springframework.org/schema/task/spring-task-3.0.xsd


-->


<task:annotation-driven /> <!-- 定时器开关-->




<bean id="taskTest" class="com.jungle.test.TaskTest"></bean>


<task:scheduled-tasks>
<!-- 
这里表示的是从第五秒开始 ,每三秒执行一次 (而不是 三分之五 秒执行一次哦~~) 
-->
<task:scheduled ref="taskTest" method="say" cron="5/3 * * * * ?" />
<task:scheduled ref="taskTest" method="hello" cron="5/3 * * * * ?"/>

</task:scheduled-tasks>


package com.jungle.test;


import java.util.Date;


public class TaskTest {


public void say() {
System.out.println("这个真好用!!!" + new Date());
}


public void hello(){
System.out.println("hello!!!");
}
}

 


第二种非标签,需要引入quartz  jar包

 <!-- 定时任务 -->
<!-- 定义调用对象和调用对象的方法 -->
<bean id="assetCheckTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="assetCheckScheduler" />
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>assetCheck</value>
</property>
</bean>
<!-- 定义触发时间 -->
<bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="assetCheckTask" />
</property>
<!-- cron表达式 -->
<property name="cronExpression">
<!--  每月1号凌晨1点执行 -->
<value>0 0 1 1 * ?</value>
<!--  每月最后一天23:59:59点执行 -->
<!-- <value>59 59 23 L * ?</value> -->
</property>
</bean>
<!-- 总管理类如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuertz" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="doTime" />
</list>
</property>
</bean>

0 0
原创粉丝点击