您的位置:首页 > 编程语言 > Java开发

atititt.java定时任务框架选型Spring Quartz 注解总结

2015-08-19 11:01 816 查看
http://blog.csdn.net/attilax/article/details/38047745

atititt.java定时任务框架选型Spring Quartz 总结  

 

1. 。Spring Quartz  (ati recomm) 1

2. Spring Quartz具体配置 2

2.1. 增加context,task命名空间xml: 2

2.2. 增加xsi:schemaLocation valide 2

2.3. 我们的task任务扫描注解in spr.xml
2

2.4. 设置运行方法 3

2.5. 设置输出日志 3

3. 运行测试sprX走ok兰. 4

4. Quartz Scheduler,定时任务 4

5. java.util.Timer 4

6. 2) ServletContextListener 4

7. org.springframework.scheduling.timer.ScheduledTimerTask 5

8. problem 5

9. Cron表达式 5

10. 参考 6

 

 

1. 。Spring Quartz  (ati recomm)

·       Spring的scheduling。 quartz包中对Quartz的框架进行封装,使得开发时不用写任何Quartz和Spring的代码就可以实现定时任务。Spring通过 JobDetailBean,MethodInvokingJobDetailFactoryBean实现Job的定义,后者更加使用,只需指定要运行的 类,和该类中运行的方法即可,Spring将自动生成符合Quartz要求的JobDetail。例如:  

· .web.xml :  

·             加入  

·            <listener>  

·              <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  

·            </listener>  

Spring3中加强了注解的使用,其中计划任务也得到了增强,现在创建一个计划任务只需要两步就完成了:

作者:: 老哇的爪子 Attilax 艾龙,  EMAIL:1466519819@qq.com

转载请注明来源: http://blog.csdn.net/attilax

2.  

3. Spring Quartz具体配置

 

3.1. 增加context,task命名空间xml: 

xmlns:context="http://www.springframework.org/schema/context"   

xmlns:task="http://www.springframework.org/schema/task"  

 

3.2. 增加xsi:schemaLocation valide

     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd      

   http://www.springframework.org/schema/task      http://www.springframework.org/schema/task/spring-task-3.1.xsd

3.3. 我们的task任务扫描注解in spr.xml 

 

<!--  o723 ati task add -->

 <task:annotation-driven/>

<context:annotation-config/>

<context:component-scan base-package="com.attilax.time.**"/>

 

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

 

 

<!--  iha ssh recomm

<task:executor id="executor" pool-size="5" />

<task:scheduler id="scheduler" pool-size="10" />

<task:annotation-driven executor="executor" scheduler="scheduler" />

 -->

 

</beans> 

 

3.4. 设置运行方法

 */@Component  

public class sysncTimeX {

 

public static Logger logger = Logger.getLogger("AtiTimerSyncer");

 @Scheduled(cron="0 0 8 * * ?")  

private static void sync() {

Date date =DateUtil. getDateFrmNet();

 dateTimeX.  setSystime(date);

 logger.info("--o7k1 sync time:ok");

}

 

///@Scheduled(fixedDelay=2000)//每2秒执行一次

3.5.  设置输出日志

#----------ati o7k..

log4j.logger.AtiTimerSyncer= info,AtiTimerSyncerAppender 

log4j.appender.AtiTimerSyncerAppender = org.apache.log4j.DailyRollingFileAppender

log4j.appender.AtiTimerSyncerAppender.Encoding=UTF-8

log4j.appender.AtiTimerSyncerAppender.File=logs/AtiTimerSyncer.log

log4j.appender.AtiTimerSyncerAppender.MaxFileSize= 20MB

log4j.appender.AtiTimerSyncerAppender.Threshold=debug

log4j.appender.AtiTimerSyncerAppender.ImmediateFlush=true

log4j.appender.AtiTimerSyncerAppender.DatePattern='_'yyyy-MM-dd'.log'

log4j.appender.AtiTimerSyncerAppender.layout=org.apache.log4j.PatternLayout

log4j.appender.AtiTimerSyncerAppender.layout.ConversionPattern=%-4r %-5p [%d{yyyy-MM-dd HH:mm:ss}]   %m   %p  [thrd:%t]    loger:%c   (%C.%M.%L) %n

log4j.appender.AtiTimerSyncerAppender.MaxBackupIndex=10

#-----------------

 

4. 运行测试sprX走ok兰.

public static void main(String[] args) {

// attilax 老哇的爪子  ml38   o7k 
//   sync();

System.out.println(SpringUtil.getBean("sysncTimeX")); 

 

}

 Quartz使用-入门使用(java定时任务实现)

5. Quartz Scheduler,定时任务 

Quartz是一个作业调度系统(a job scheduling system),负责在约定的时间到达时执行(或通知)其他软件控制。是一个Java的定时任务框架,使用它可以方便的实现计划任务,即在某个时间或每隔 一定时间运行一个任务。Quartz的核心是Job/JobDetail,Trigger和Scheduler。 

6. java.util.Timer

1) 

这个方法应该是最常用的,不过这个方法需要手工启动你的任务:

如果要执行一些简单的定时器任务,无须做复杂的控制,也无须保存状态,那么可以考虑使用JDK 入门级的定期器Timer来执行重复任务。

2) 

7.  2) ServletContextListener

这个方法在web容器环境比较方便,这样,在web server启动后就可以

自动运行该任务,不需要手工操作。

 
 

8. org.springframework.scheduling.timer.ScheduledTimerTask

3)

如果你用spring,那么你不需要写Timer类了,在schedulingContext-timer

.xml中加入下面的内容就可以了:

 

9. problem

Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 115 in XML document from file [D:\workspace\vodx\WebRoot\WEB-INF\classes\applicationContext-common.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'task:annotation-driven'.

 

10. Cron表达式

被用来配置CronTrigger实例。 Cron表达式是一个由 7个子表达式组成的字

符串。每个子表达式都描述了一个单独的日程细节。这些子表达式用空格分隔,分别表示: 

1. Seconds 秒 

2. Minutes 分钟 

3. Hours 小时 

4. Day-of-Month 月中的天 

5. Month 月 

6. Day-of-Week 周中的天 

7. Year (optional field) 年(可选的域)

剩下的就是corn表达式了、具体使用以及参数请百度google、

【秒】   【分】  【时】   【日】  【月】   【周】  【年】  

下面只例出几个式子

CRON表达式    含义 

"0 0 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分每分钟一次触发 

"0 0/5 14 * * ?"    每天从下午2点开始到2:55分结束每5分钟一次触发 

"0 0/5 14,18 * * ?"    每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发 

"0 0-5 14 * * ?"    每天14:00至14:05每分钟一次触发 

"0 10,44 14 ? 3 WED"    三月的每周三的14:10和14:44触发 

"0 15 10 ? * MON-FRI"    每个周一、周二、周三、周四、周五的10:15触发 

 

11. 参考

JAVA定时执行任务的三种方法 - gembin - BlogJava.htm

Quartz使用-入门使用(java定时任务实现) - lotusyangjun的专栏 - 博客频道 - CSDN.NET.htm

java定时任务管理 - 塵风的blog - ITeye技术网站.htm

quartz 框架定时任务,使用spring @Scheduled注解执行定时任务 - 萧若寒 - 博客频道 - CSDN.NET.htm

quartz 框架定时任务,使用spring @Scheduled注解执行定时任务 - 萧若寒 - 博客频道 - CSDN.NET.htm
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: