`
jsjxqjy
  • 浏览: 117799 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

spring2.0 quartz 静态定时任务

阅读更多

1.配置简单的spring 定时任务

 

配置文件中定义一个schedule的Factory

 

<bean id="scheduler"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref local="triggerJobTester" />
			</list>
		</property>
	</bean>

 这个schedule工厂bean会随着容器启动而启动.然后定义一个定时执行的规则.

<!-- 描述了一个定时处理的机制 -->
<bean id="triggerJobTester"
		class="org.springframework.scheduling.quartz.SimpleTriggerBean"
		p:jobDetail-ref="jobDetailTesterTarget" p:startDelay="1000"
		p:repeatInterval="2000">
	</bean>
<!-- 指定定时器要定时执行的类和方法 -->
<bean id="jobDetailTesterTarget"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"
		p:targetObject-ref="scheduleTarget" p:targetMethod="testMethod">
	</bean>
<!-- 在sping当中注册定时器的FactoryBean -->
	<bean id="scheduleTarget"
		class="com.lsk.spring.schedule.ScheduleTarget" />

 p:startDelay 指定容器启动后多少毫秒开始执行定时方法.
p:repeatInterval 指定隔多少毫秒再一次的执行定时方法.

<完> //作者 FutureInHands  

 

http://futureinhands.iteye.com/blog/319913 

 

这个博客里面 还有 动态配置及描述 可以参阅

========================================================

 

2.配置特定时间的任务 。

 

<!-- 指定定时器要定时执行的类和方法 -->
<bean id="schedulerJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 
        <property name="targetObject" ref="scheduleInfoAction"/> 
        <property name="targetMethod" value="simpleJobTest"/> 
        <property name="concurrent" value="false"/> 
 </bean> 
    
<!-- 描述了一个定时处理的机制 -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean" > 
         <property name="jobDetail" ref="schedulerJobDetail"/> 
         <property name="cronExpression"> 
             <value>0/10 * * * * ?</value> 
         </property> 
     </bean> 
  配置文件中定义一个schedule的Factory         
 <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 
        <property name="triggers"> 
            <list> 
                <ref local="cronTrigger"/> 
            </list> 
        </property> 
</bean> 

scheduleInfoAction 需要自己定义 ,并写个void 无参的方法

在上面的配置中设定了

① targetMethod: 指定需要定时执行scheduleInfoAction中的simpleJobTest()方法

② concurrent:对于相同的JobDetail,当指定多个Trigger时, 很可能第一个job完成之前,第二个job就开始了。指定concurrent设为false,多个job不会并发运行,第二个job将不会在第一个job完成之前开始。

③ cronExpression:0/10 * * * * ?表示每10秒执行一次,具体可参考附表。

④ triggers:通过再添加其他的ref元素可在list中放置多个触发器。

 

scheduleInfoAction 中的simpleJobTest()方法

注意:此方法没有参数,如果scheduleInfoAction有两个方法simpleJobTest()和simpleJobTest(String argument),则spring只会去执行无参的simpleJobTest().

public void simpleJobTest() { 

        log.warn("uh oh, Job is scheduled !'" + "' Success...");

    }

 

//下面具体描述 特定时间配置  cronExpression 的value设置

 

字段 	  	允许值 	  	允许的特殊字符
秒 	  	0-59 	  	, - * /
分 	  	0-59 	  	, - * /
小时 	  	0-23 	  	, - * /
日期 	  	1-31 	  	, - * ? / L W C
月份 	  	1-12 或者 JAN-DEC 	  	, - * /
星期 	  	1-7 或者 SUN-SAT 	  	, - * ? / L C #
年(可选) 	  	留空, 1970-2099 	  	, - * /

 The '*' character is used to specify all values. For example, "*" in the minute field means "every minute".
“*”字符被用来指定所有的值。如:”*“在分钟的字段域里表示“每分钟”。
The '?' character is allowed for the day-of-month and day-of-week fields. It is used to specify 'no specific value'. This is useful when you need to specify something in one of the two fileds, but not the other. See the examples below for clarification.
“?”字符只在日期域和星期域中使用。它被用来指定“非明确的值”。当你需要通过在这两个域中的一个来指定一些东西的时候,它是有用的。看下面的例子你就会明白。
月份中的日期和星期中的日期这两个元素时互斥的一起应该通过设置一个问号(?)来表明不想设置那个字段

The '-' character is used to specify ranges For example "10-12" in the hour field means "the hours 10, 11 and 12".
“-”字符被用来指定一个范围。如:“10-12”在小时域意味着“10点、11点、12点”。

The ',' character is used to specify additional values. For example "MON,WED,FRI" in the day-of-week field means "the days Monday, Wednesday, and Friday".
“,”字符被用来指定另外的值。如:“MON,WED,FRI”在星期域里表示”星期一、星期三、星期五”.

The '/' character is used to specify increments. For example "0/15" in the seconds field means "the seconds 0, 15, 30, and 45". And "5/15" in the seconds field means "the seconds 5, 20, 35, and 50". Specifying '*' before the '/' is equivalent to specifying 0 is the value to start with. Essentially, for each field in the expression, there is a set of numbers that can be turned on or off. For seconds and minutes, the numbers range from 0 to 59. For hours 0 to 23, for days of the month 0 to 31, and for months 1 to 12. The "/" character simply helps you turn on every "nth" value in the given set. Thus "7/6" in the month field only turns on month "7", it does NOT mean every 6th month, please note that subtlety.

The 'L' character is allowed for the day-of-month and day-of-week fields. This character is short-hand for "last", but it has different meaning in each of the two fields. For example, the value "L" in the day-of-month field means "the last day of the month" - day 31 for January, day 28 for February on non-leap years. If used in the day-of-week field by itself, it simply means "7" or "SAT". But if used in the day-of-week field after another value, it means "the last xxx day of the month" - for example "6L" means "the last friday of the month". When using the 'L' option, it is important not to specify lists, or ranges of values, as you'll get confusing results.

L是‘last’的省略写法可以表示day-of-month和day-of-week域,但在两个字段中的意思不同,例如day-of-month域中表示一个月的最后一天,
如果在day-of-week域表示‘7’或者‘SAT’,如果在day-of-week域中前面加上数字,它表示一个月的最后几天,例如‘6L’就表示一个月的最后一个
星期五,

The 'W' character is allowed for the day-of-month field. This character is used to specify the weekday (Monday-Friday) nearest the given day. As an example, if you were to specify "15W" as the value for the day-of-month field, the meaning is: "the nearest weekday to the 15th of the month". So if the 15th is a Saturday, the trigger will fire on Friday the 14th. If the 15th is a Sunday, the trigger will fire on Monday the 16th. If the 15th is a Tuesday, then it will fire on Tuesday the 15th. However if you specify "1W" as the value for day-of-month, and the 1st is a Saturday, the trigger will fire on Monday the 3rd, as it will not 'jump' over the boundary of a month's days. The 'W' character can only be specified when the day-of-month is a single day, not a range or list of days
.
The 'L' and 'W' characters can also be combined for the day-of-month expression to yield 'LW', which translates to "last weekday of the month".

The '#' character is allowed for the day-of-week field. This character is used to specify "the nth" XXX day of the month. For example, the value of "6#3" in the day-of-week field means the third Friday of the month (day 6 = Friday and "#3" = the 3rd one in the month). Other examples: "2#1" = the first Monday of the month and "4#5" = the fifth Wednesday of the month. Note that if you specify "#5" and there is not 5 of the given day-of-week in the month, then no firing will occur that month.

The 'C' character is allowed for the day-of-month and day-of-week fields. This character is short-hand for "calendar". This means values are calculated against the associated calendar, if any. If no calendar is associated, then it is equivalent to having an all-inclusive calendar. A value of "5C" in the day-of-month field means "the first day included by the calendar on or after the 5th". A value of "1C" in the day-of-week field means "the first day included by the calendar on or after sunday".
关于cronExpression的介绍:

 
字段 允许值 允许的特殊字符
秒 0-59 , - * /
分 0-59 , - * /
小时 0-23 , - * /
日期 1-31 , - * ? / L W C
月份 1-12 或者 JAN-DEC , - * /
星期 1-7 或者 SUN-SAT , - * ? / L C #
年(可选) 留空, 1970-2099 , - * /
表达式意义
"0 0 12 * * ?" 每天中午12点触发
"0 15 10 ? * *" 每天上午10:15触发
"0 15 10 * * ?" 每天上午10:15触发
"0 15 10 * * ? *" 每天上午10:15触发
"0 15 10 * * ? 2005" 2005年的每天上午10:15触发
"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
"0 15 10 15 * ?" 每月15日上午10:15触发
"0 15 10 L * ?" 每月最后一日的上午10:15触发
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发
每天早上6点
0 6 * * *
每两个小时
0 */2 * * *
晚上11点到早上8点之间每两个小时,早上八点
0 23-7/2,8 * * *
每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点
0 11 4 * 1-3
1月1日早上4点
0 4 1 1 *

 

quartz的高级特性不仅如此
1 数据库存储
2 集群支持
3 数据库持久化任务,trigger
4 trigger 的停止,运行
5 任务的任意添加
6 比corntrigger 更详尽的任务安排
7 线程的内部数据交换

 

//作者 dogstar

 

http://dogstar.iteye.com/blog/116130

=====================以上内容均整理来自je=================

 小结 : 以上2种配置,主要区别在 :  定时处理 的类不一样

 

 1. org.springframework.scheduling.quartz.SimpleTriggerBean

 2. org.springframework.scheduling.quartz.CronTriggerBean

 

 

 

分享到:
评论

相关推荐

    spring2.0 Quartz 执行每天定时任务 普通普是执行任务

    标题中的“spring2.0 Quartz 执行每天定时任务 普通普是执行任务”涉及到的是在Spring 2.0框架中使用Quartz库来创建并执行每天定时任务的场景。Quartz是一款强大的开源作业调度框架,它可以用来按计划执行各种任务,...

    spring2.0 使用 Quartz 必备包

    spring2.0 使用 Quartz 必备包 里面有: quartz-all-1.5.2.jar spring-support.jar 适合:SSH框架 spring2.0 strut2.0 hibernate3.2等

    spring2.0 Quartz 执行每天定时任务 建议用quartz-all-1.8.4.jar更稳定,兼容性更好

    标题中的“Spring 2.0 Quartz 执行每天定时任务”是指使用Spring框架的Quartz集成来创建一个每天自动执行的任务。Quartz是一个开源的作业调度框架,它允许开发者定义和调度任务,以便在指定的时间自动运行。在Spring...

    spring+quartz动态定时任务创建 +mybatis

    在Spring中整合Quartz,我们可以利用Spring的管理能力,如bean的生命周期管理和事务管理,来更方便地创建和管理定时任务。 **Spring+Quartz动态定时任务创建** 将Spring与Quartz结合,我们可以方便地在运行时动态...

    基于Spring的Quartz动态定时任务增删改查

    基于Spring的Quartz动态定时任务增删改查,代码简洁。后端采用SpringMvc+Spring+Mybatis+Quartz,前端采用Bootstrap框架,数据库采用MySQL;完成定时任务动态任务初始化,增删改查

    spring2.0 job Quartz 和Time一样的定时任务

    在本篇文章中,我们将聚焦于Spring 2.0中的Job以及Quartz这两款工具,探讨它们如何实现定时任务,以及它们与Java内置的`java.util.Timer`类(简称Time)的异同。 首先,让我们从Java自带的`Timer`类开始。`Timer`是...

    spring2.0学习笔记+spring定时任务

    标题中的“spring2.0学习笔记+spring定时任务”表明了这个压缩包文件包含的是关于Spring框架2.0版本的学习资料,特别是关于Spring的定时任务功能。Spring是Java开发中最广泛应用的轻量级框架之一,它提供了一整套...

    spring+springMVC+mybatis+quartz动态定时任务创建

    在IT行业中,构建高效、可扩展的Web应用是至关重要的,而"spring+springMVC+mybatis+quartz动态定时任务创建"就是一个常见的技术栈,用于实现这样的目标。这个组合充分利用了各组件的优势,提供了强大的后端服务支持...

    Spring + quartz 定时任务修改定时时间不重启服务

    Spring + quartz 定时任务修改定时时间不重启服务

    spring3配置quartz定时任务

    本文将详细介绍如何在Spring 3中配置Quartz来实现定时任务。 首先,理解定时任务的基本概念。定时任务是在指定时间点或按照一定规律自动执行的任务,这在业务系统中非常常见,例如数据清理、报表生成等。Quartz提供...

    完美解决多应用服务器负载均衡环境下spring quartz同一定时任务重复执行问题

    在多应用服务器负载均衡环境下,Spring Quartz定时任务的重复执行问题是一个常见的挑战。Spring Quartz是一个强大的、开源的作业调度框架,允许开发者定义和执行复杂的定时任务。然而,当多个服务器实例并行运行时,...

    spring整合quartz定时任务调度

    Spring框架作为Java领域广泛使用的轻量级框架,提供了与第三方库Quartz的整合,使得开发者能够轻松地在Spring应用中实现复杂的定时任务调度。Quartz是一款开源的作业调度框架,支持丰富的调度策略,可以满足各种定时...

    spring_quartz_定时任务

    《Spring与Quartz实现定时任务详解》 在Java开发中,定时任务的处理是一项重要的功能,它能够按照预设的时间规律自动执行特定的任务,比如数据同步、报表生成等。Spring框架,作为Java企业级应用的基石,提供了丰富...

    spring+quartz实现定时任务动态管理

    Spring框架结合Quartz库,提供了一种强大而灵活的方式来管理这些定时任务。本项目旨在实现对Quartz定时任务的动态管理,包括添加、停止和恢复等操作。前端采用jQuery和Bootstrap构建,提供用户友好的可视化界面,...

    spring+quartz的定时任务

    spring+quartz的定时任务,希望能帮到大家!

    基于Spring的Quartz动态定时任务增删改查源码.rar

    本资源"基于Spring的Quartz动态定时任务增删改查源码.rar"提供了一套完整的示例,帮助开发者了解如何在Spring环境中集成Quartz来实现动态管理定时任务。 首先,Quartz的核心概念包括Job(任务)、Trigger(触发器)...

    spring整合quartz动态定时任务demo-spring-quartz-demo.zip

    这个"spring-quartz-demo"项目就是一个很好的示例,它展示了如何在Spring中整合Quartz以实现动态定时任务。 首先,我们要了解Spring和Quartz的基本概念。Spring是一个全面的Java企业级应用开发框架,它提供了依赖...

    SpringBoot2.0整合Quartz定时任务,实现基于数据库如mysql的动态任务

    在Spring Boot 2.0中整合Quartz定时任务,可以实现基于数据库如MySQL的动态任务管理,这在很多需要周期性执行任务的应用场景中非常实用。以下将详细讲解这一过程。 首先,我们要在项目的`pom.xml`文件中添加Quartz...

    Spring2.0+quartz1.8定时执行任务内含Cron表达式生成器

    &lt;bean id="runTime" class="org.springframework.scheduling.quartz.CronTriggerBean"&gt; &lt;value&gt;0 0 2,17 * * ? &lt;!-- 最终启动的配置 --&gt; class="org.springframework.scheduling.quartz...

    Spring2.0技术手册_林信良PDF

    5. **Spring与其他技术的集成**:Spring 2.0支持与众多其他技术的集成,如EJB、 Quartz定时任务、邮件服务等。书中将展示如何整合这些技术,以增强应用的功能。 6. **测试支持**:Spring提供了丰富的测试工具和API...

Global site tag (gtag.js) - Google Analytics