- 浏览: 2272133 次
- 性别:
- 来自: 成都
文章分类
- 全部博客 (357)
- J2EE (49)
- JavaScript (40)
- Spring (19)
- Struts (5)
- CSS (8)
- Hibernate (16)
- Java (67)
- DWR (4)
- JSON (3)
- XFIRE (1)
- Tomcat (1)
- Ant (2)
- 设计模式 (2)
- 经典收藏 (2)
- JSP (10)
- Linux (0)
- WebLogic (11)
- myeclipse (13)
- Buffalo (4)
- 文件上传相关 (1)
- oracle (33)
- html (6)
- JSTL (3)
- SVN (2)
- GIT (1)
- 孙卫琴(Java网络编程精解) (1)
- DOM4J (2)
- Swing (1)
- AJAX (1)
- Eclipse (5)
- 日志组件 (3)
- PowerDesigner (1)
- Jquery (22)
- IT技术开发相关网址 (1)
- Nutz (1)
- 其它 (1)
- Velocity (3)
- WebService (1)
- MySql (2)
- Android (1)
- Maven (2)
- Quartz (11)
- Lucene (1)
- springsource (1)
- Junit (1)
- Activiti (0)
最新评论
-
yzlseu:
拼凑,没有营养
Activiti进阶—分配组任务 -
zhangsenhao:
非常赞!代码很清楚
SpringMVC3.0+MyIbatis3.0(分页示例) -
xiamw2000:
分页写得不对,应该是 : order by ${orderNa ...
SpringMVC3.0+MyIbatis3.0(分页示例) -
sheertewtw:
...
SpringMVC:上传与下载 -
kingtoon:
...
XSS之xssprotect
问题:我想在应用程序启动之后去执行任务怎么办呢!
Quartz:使用QuartzInitializerServlet可满足需要
参考资料
1 Quartz调度框架应用总结
http://java.chinaitlab.com/advance/752064_3.html
2 Integrating quartz in a web application
http://www.oreillynet.com/cs/user/view/cs_msg/52725
3 基于Quartz的开源项目
myschedule
4 Adding multiple jobs via the quartz_jobs.xml skips jobs
http://jira.opensymphony.com/browse/QRTZNET-250
5 Quartz with Apache ServiceMix
http://forums.terracotta.org/forums/posts/list/5427.page
6 Web应用中使用Quartz进行任务调度
http://www.hujun.me/post?id=12001
7 Quartz的XML定义说明:New in Quartz.Net 2.0–New Job File Format
http://jvilalta.blogspot.com/2011/03/new-in-quartznet-20new-job-file-format.html
来自于Quartz的文档说明:
A Servlet that can be used to initialize Quartz, if configured as a load-on-startup servlet in a web application.Using this start-up servlet may be preferred to using the QuartzInitializerListener in some situations - namely when you want to initialize more than one scheduler in the same application.You'll want to add something like this to your WEB-INF/web.xml file:
相应参数说明:
The init parameter 'config-file' can be used to specify the path (and filename) of your Quartz properties file. If you leave out this parameter, the default ("quartz.properties") will be used.
The init parameter 'shutdown-on-unload' can be used to specify whether you want scheduler.shutdown() called when the servlet is unloaded (usually when the application server is being shutdown). Possible values are "true" or "false". The default is "true".
The init parameter 'wait-on-shutdown' has effect when 'shutdown-on-unload' is specified "true", and indicates whether you want scheduler.shutdown(true) called when the listener is unloaded (usually when the application server is being shutdown). Passing "true" to the shutdown() call causes the scheduler to wait for existing jobs to complete. Possible values are "true" or "false". The default is "false".
The init parameter 'start-scheduler-on-load' can be used to specify whether you want the scheduler.start() method called when the servlet is first loaded. If set to false, your application will need to call the start() method before the scheduler begins to run and process jobs. Possible values are "true" or "false". The default is "true", which means the scheduler is started.
A StdSchedulerFactory instance is stored into the ServletContext. You can gain access to the factory from a ServletContext instance like this:
StdSchedulerFactory factory = (StdSchedulerFactory) ctx
.getAttribute(QuartzFactoryServlet.QUARTZ_FACTORY_KEY);
The init parameter 'servlet-context-factory-key' can be used to override the name under which the StdSchedulerFactory is stored into the ServletContext, in which case you will want to use this name rather than QuartzFactoryServlet.QUARTZ_FACTORY_KEY in the above example.
The init parameter 'scheduler-context-servlet-context-key' if set, the ServletContext will be stored in the SchedulerContext under the given key name (and will therefore be available to jobs during execution).
The init parameter 'start-delay-seconds' can be used to specify the amount of time to wait after initializing the scheduler before scheduler.start() is called.
Once you have the factory instance, you can retrieve the Scheduler instance by calling getScheduler() on the factory.
所要的jar文件包含:
quartz-all-2.0.1.jar, jta-1.1.jar,log4j-1.2.14.jar,slf4j-api-1.6.1.jar,slf4j-log4j12-1.6.1.jar
具体代码如下:
web.xml
位于src下:quartz.properties
有些时候其实可按需配置,如下:
位于src下的:quartz_job.xml
src下的log4j.xml
二个任务类:
在WEB应用启动时控制台输出结果如下:
Quartz:使用QuartzInitializerServlet可满足需要
参考资料
1 Quartz调度框架应用总结
http://java.chinaitlab.com/advance/752064_3.html
2 Integrating quartz in a web application
http://www.oreillynet.com/cs/user/view/cs_msg/52725
3 基于Quartz的开源项目
myschedule
4 Adding multiple jobs via the quartz_jobs.xml skips jobs
http://jira.opensymphony.com/browse/QRTZNET-250
5 Quartz with Apache ServiceMix
http://forums.terracotta.org/forums/posts/list/5427.page
6 Web应用中使用Quartz进行任务调度
http://www.hujun.me/post?id=12001
7 Quartz的XML定义说明:New in Quartz.Net 2.0–New Job File Format
http://jvilalta.blogspot.com/2011/03/new-in-quartznet-20new-job-file-format.html
来自于Quartz的文档说明:
A Servlet that can be used to initialize Quartz, if configured as a load-on-startup servlet in a web application.Using this start-up servlet may be preferred to using the QuartzInitializerListener in some situations - namely when you want to initialize more than one scheduler in the same application.You'll want to add something like this to your WEB-INF/web.xml file:
<servlet> <servlet-name> QuartzInitializer </servlet-name> <display-name> Quartz Initializer Servlet </display-name> <servlet-class> org.quartz.ee.servlet.QuartzInitializerServlet </servlet-class> <load-on-startup> 1 </load-on-startup> <init-param> <param-name>config-file</param-name> <param-value>/some/path/my_quartz.properties</param-value> </init-param> <init-param> <param-name>shutdown-on-unload</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>wait-on-shutdown</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>start-scheduler-on-load</param-name> <param-value>true</param-value> </init-param> </servlet>
相应参数说明:
The init parameter 'config-file' can be used to specify the path (and filename) of your Quartz properties file. If you leave out this parameter, the default ("quartz.properties") will be used.
The init parameter 'shutdown-on-unload' can be used to specify whether you want scheduler.shutdown() called when the servlet is unloaded (usually when the application server is being shutdown). Possible values are "true" or "false". The default is "true".
The init parameter 'wait-on-shutdown' has effect when 'shutdown-on-unload' is specified "true", and indicates whether you want scheduler.shutdown(true) called when the listener is unloaded (usually when the application server is being shutdown). Passing "true" to the shutdown() call causes the scheduler to wait for existing jobs to complete. Possible values are "true" or "false". The default is "false".
The init parameter 'start-scheduler-on-load' can be used to specify whether you want the scheduler.start() method called when the servlet is first loaded. If set to false, your application will need to call the start() method before the scheduler begins to run and process jobs. Possible values are "true" or "false". The default is "true", which means the scheduler is started.
A StdSchedulerFactory instance is stored into the ServletContext. You can gain access to the factory from a ServletContext instance like this:
StdSchedulerFactory factory = (StdSchedulerFactory) ctx
.getAttribute(QuartzFactoryServlet.QUARTZ_FACTORY_KEY);
The init parameter 'servlet-context-factory-key' can be used to override the name under which the StdSchedulerFactory is stored into the ServletContext, in which case you will want to use this name rather than QuartzFactoryServlet.QUARTZ_FACTORY_KEY in the above example.
The init parameter 'scheduler-context-servlet-context-key' if set, the ServletContext will be stored in the SchedulerContext under the given key name (and will therefore be available to jobs during execution).
The init parameter 'start-delay-seconds' can be used to specify the amount of time to wait after initializing the scheduler before scheduler.start() is called.
Once you have the factory instance, you can retrieve the Scheduler instance by calling getScheduler() on the factory.
所要的jar文件包含:
quartz-all-2.0.1.jar, jta-1.1.jar,log4j-1.2.14.jar,slf4j-api-1.6.1.jar,slf4j-log4j12-1.6.1.jar
具体代码如下:
web.xml
<servlet> <servlet-name>QuartzInitializer</servlet-name> <display-name>Quartz Initializer Servlet</display-name> <servlet-class> org.quartz.ee.servlet.QuartzInitializerServlet </servlet-class> <load-on-startup>1</load-on-startup> <init-param> <param-name>config-file</param-name> <param-value>/quartz.properties</param-value> </init-param> <init-param> <param-name>shutdown-on-unload</param-name> <param-value>true</param-value> </init-param> <!-- <init-param> <param-name>start-scheduler-on-load</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>wait-on-shutdown</param-name> <param-value>true</param-value> </init-param> --> </servlet>
位于src下:quartz.properties
org.quartz.scheduler.threadsInheritContextClassLoaderOfInitializer=true org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true # Configure Main Scheduler Properties org.quartz.scheduler.instanceName=QuartzScheduler org.quartz.scheduler.instanceId=AUTO org.quartz.scheduler.skipUpdateCheck=true org.quartz.scheduler.threadsInheritContextClassLoaderOfInitializer=true # Configure ThreadPool org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount=3 org.quartz.threadPool.threadPriority=5 org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true # Configure JobStore org.quartz.jobStore.misfireThreshold=60000 org.quartz.jobStore.class=org.quartz.simpl.RAMJobStore #org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX #org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.PostgreSQLDelegate #org.quartz.jobStore.useProperties=false #org.quartz.jobStore.dataSource=myDS #org.quartz.jobStore.tablePrefix=QRTZ_ #org.quartz.jobStore.isClustered=false # Configure Datasources #org.quartz.dataSource.myDS.driver=org.postgresql.Driver #org.quartz.dataSource.myDS.URL=jdbc:postgresql://localhost/dev #org.quartz.dataSource.myDS.user=jhouse #org.quartz.dataSource.myDS.password= #org.quartz.dataSource.myDS.maxConnections=5 # Configure Plugins org.quartz.plugin.triggHistory.class=org.quartz.plugins.history.LoggingJobHistoryPlugin org.quartz.plugin.jobInitializer.class=org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin org.quartz.plugin.jobInitializer.fileNames=quartz_job.xml org.quartz.plugin.jobInitializer.failOnFileNotFound=true org.quartz.plugin.jobInitializer.scanInterval=120 org.quartz.plugin.jobInitializer.wrapInUserTransaction=false
有些时候其实可按需配置,如下:
org.quartz.scheduler.instanceName= MyQuartzScheduler org.quartz.scheduler.rmi.export= false org.quartz.scheduler.rmi.proxy= false org.quartz.scheduler.wrapJobExecutionInUserTransaction= false org.quartz.threadPool.class= org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount= 10 org.quartz.threadPool.threadPriority= 5 org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread= true org.quartz.jobStore.misfireThreshold= 60000 org.quartz.jobStore.class= org.quartz.simpl.RAMJobStore org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin #job and trigger configuration file org.quartz.plugin.jobInitializer.fileNames =quartz_job.xml
位于src下的:quartz_job.xml
<?xml version="1.0" encoding="UTF-8"?> <job-scheduling-data xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_2_0.xsd" version="2.0"> <pre-processing-commands> <delete-jobs-in-group>*</delete-jobs-in-group> <!-- clear all jobs in scheduler --> <delete-triggers-in-group>*</delete-triggers-in-group> <!-- clear all triggers in scheduler --> </pre-processing-commands> <processing-directives> <!-- if there are any jobs/trigger in scheduler of same name (as in this file), overwrite them --> <overwrite-existing-data>true</overwrite-existing-data> <!-- if there are any jobs/trigger in scheduler of same name (as in this file), and over-write is false, ignore them rather then generating an error --> <ignore-duplicates>false</ignore-duplicates> </processing-directives> <schedule> <job> <name>job1</name> <job-class>net.liuzd.tools.quartz.servlet.Job1</job-class> </job> <trigger> <cron> <name>t1</name> <job-name>job1</job-name> <cron-expression>0/10 * * * * ?</cron-expression> </cron> </trigger> <job> <name>job2</name> <job-class>net.liuzd.tools.quartz.servlet.Job2</job-class> </job> <trigger> <cron> <name>t2</name> <job-name>job2</job-name> <cron-expression>0/20 * * * * ?</cron-expression> </cron> </trigger> </schedule> </job-scheduling-data>
src下的log4j.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="default" class="org.apache.log4j.ConsoleAppender"> <param name="target" value="System.out"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="[%p] %d{yyyy-MM-dd hh:mm:ss.SSS aa} %t [%c]%n%m%n%n"/> </layout> </appender> <logger name="org.quartz"> <level value="info" /> </logger> <root> <level value="info" /> <appender-ref ref="default" /> </root> </log4j:configuration>
二个任务类:
import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Job1 implements Job { private static Logger _log = LoggerFactory.getLogger(Job1.class); public Job1() { } public void execute(JobExecutionContext context) throws JobExecutionException { _log.info("执行天涯的第一个任务: " + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date())); } } import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Job2 implements Job { private static Logger _log = LoggerFactory.getLogger(Job2.class); public Job2() { } public void execute(JobExecutionContext context) throws JobExecutionException { _log.info("执行天涯的第二个任务: " + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date())); } }
在WEB应用启动时控制台输出结果如下:
[INFO] 2011-08-15 02:18:50.015 下午 QuartzScheduler_Worker-2 [org.quartz.plugins.history.LoggingJobHistoryPlugin] Job DEFAULT.job1 fired (by trigger DEFAULT.t1) at: 14:18:50 08/15/2011 [INFO] 2011-08-15 02:18:50.015 下午 QuartzScheduler_Worker-2 [net.liuzd.tools.quartz.servlet.Job1] 执行天涯的第一个任务: 2011-08-15 02:18:50 [INFO] 2011-08-15 02:18:50.015 下午 QuartzScheduler_Worker-2 [org.quartz.plugins.history.LoggingJobHistoryPlugin] Job DEFAULT.job1 execution complete at 14:18:50 08/15/2011 and reports: null [INFO] 2011-08-15 02:19:00.000 下午 QuartzScheduler_Worker-3 [org.quartz.plugins.history.LoggingJobHistoryPlugin] Job DEFAULT.job1 fired (by trigger DEFAULT.t1) at: 14:19:00 08/15/2011 [INFO] 2011-08-15 02:19:00.000 下午 QuartzScheduler_Worker-3 [net.liuzd.tools.quartz.servlet.Job1] 执行天涯的第一个任务: 2011-08-15 02:19:00 [INFO] 2011-08-15 02:19:00.000 下午 QuartzScheduler_Worker-3 [org.quartz.plugins.history.LoggingJobHistoryPlugin] Job DEFAULT.job1 execution complete at 14:19:00 08/15/2011 and reports: null [INFO] 2011-08-15 02:19:00.000 下午 QuartzScheduler_Worker-1 [org.quartz.plugins.history.LoggingJobHistoryPlugin] Job DEFAULT.job2 fired (by trigger DEFAULT.t2) at: 14:19:00 08/15/2011 [INFO] 2011-08-15 02:19:00.000 下午 QuartzScheduler_Worker-1 [net.liuzd.tools.quartz.servlet.Job2] 执行天涯的第二个任务: 2011-08-15 02:19:00
发表评论
-
【转载】Eclipse Class Decompiler——Java反编译插件
2018-06-24 11:09 1317参见:jd-eclipse 的安装和使用(最新版的) h ... -
Java多线程序源码
2016-06-24 15:04 9871、《Java多线程编程核心技术》源代码(高洪岩) 2、Jav ... -
Spring & Junit
2016-05-27 13:18 7161 测试基类(BaseJunit4Test) import ... -
XSS之xssprotect
2012-12-07 23:03 29353参考资料 1 跨网站脚本 ... -
myeclispe之图解weblogic81配置
2011-11-07 10:11 1559一 创建Weblogic81域(如下图) 注意事项:一般 ... -
Cannot forward a response that is already committed
2011-09-07 09:43 5538参考资料 1 Cannot forward a respons ... -
WebLogic之Cannot parse POST parameters of request解决方法
2011-09-06 15:31 11302参考资料 1 关于Cannot parse POST para ... -
WebLogic之weblogic.xml.jaxp.RegistrySAXTransformerFactory
2011-08-16 16:46 8685参考资料 1 Thread: java.lang.ClassC ... -
Quartz之一个任务绑定多个触发器
2011-08-15 16:27 4894参见官方示例: SchedulerFactory sf = ... -
Quartz之QuartzInitializerListener
2011-08-15 15:12 7806问题:我想在WEB容器启动时就执行任务怎么办呢 Quartz: ... -
Quartz之AnnualCalendar
2011-08-11 17:14 5767问题1 我想排除一年中 ... -
Quartz之InterruptableJob
2011-08-11 11:03 11732问题1 由于业务需要,停止Quartz中正在执行的任务 Qua ... -
Quartz之JobExecutionException
2011-08-11 10:06 13306问题1 如果你的任务执 ... -
Quartz之JobDataMap,PersistJobDataAfterExecution,DisallowConcurrentExecution
2011-08-10 15:21 14883参考资料 http://stackoverflow.com/q ... -
jQuery与Java实现图片的剪切
2011-08-05 15:10 30415一 参考资料 1 jquery Jcrop 头像,logo截图 ... -
WebLogic之Session
2011-07-25 13:15 23980参考资料 1 关于WebLogic的Session丢失的问题 ... -
Frameset导致Cookies和Session丢失的原因及解决办法
2011-07-25 12:28 4294参考资料 1 Frameset导致Cookies和Sessio ... -
java,javascript对18位身份证格式的验证算法
2011-07-12 14:15 7043参考资料 1 java实现的18位身份证格式验证算法 http ... -
Quartz之CronTrigger
2011-07-11 15:01 14667参考资料 1 quartz 学习笔记 http://china ... -
Quartz之CronExpression
2011-07-08 16:15 33687参考资料 1 Spring中任务调度(Quartz篇)-续-c ...
相关推荐
赠送jar包:quartz-2.3.2.jar; 赠送原API文档:quartz-2.3.2-javadoc.jar; 赠送源代码:quartz-2.3.2-sources.jar; 赠送Maven依赖信息文件:quartz-2.3.2.pom; 包含翻译后的API文档:quartz-2.3.2-javadoc-API...
文件里面包括 1:Quartz开发指南.pdf 2:Quartz从入门到进阶.pdf 3:QuartzBeginnerExample一个附带的工程例子 4:quartz-1.6.1.zip Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它...
该压缩包内包含两个quartz的jar包, 分别是quartz-1.6.0.jar和quartz-all-1.6.0.jar
赠送jar包:quartz-2.3.0.jar; 赠送原API文档:quartz-2.3.0-javadoc.jar; 赠送源代码:quartz-2.3.0-sources.jar; 赠送Maven依赖信息文件:quartz-2.3.0.pom; 包含翻译后的API文档:quartz-2.3.0-javadoc-API...
quartz-2.2.3版本的quartz初始化sql语句
quartz简单实例quartz简单实例quartz简单实例quartz简单实例
Autofac.Extras.Quartz, Quartz.Net的Autofac集成 Autofac.Extras.Quartz用于 Quartz.Net的Autofac集成包。Autofac.Extras.Quartz 为每个石英作业创建嵌套的litefime作用域。 完成作业执行后释放嵌套作用域。这允许...
网上能找到的Quartz Web管理的资料都是使用的一个国外人写的Quartz WebApp的东东,功能也很全面。但是作为自己的应用其实用不了那么多功能,一般我们只要可以定义一个job,指定一个Cron表达式完成工作即可,附带的...
quartz quartz-1.8.6 dbTables quartz动态任务调度需要的数据库脚本。
quartz内部表.sql。
Quartz1.5,Quartz1.6,Quartz1.8。Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用。Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个...
postgres quatrz初始化sql脚本文件、pg、quartz、qrtz_开头的表 配置文件需求修改 #org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate org.quartz.jobStore....
Quartz.NET框架的核心是调度器。调度器负责管理Quartz.NET应用运行时环境。Quartz不仅仅是线程和线程管理。为确保可伸缩性,Quartz.NET采用了基于多线程的架构。启动时,框架初始化一套worker线程,这套线程被调度器...
Quartz原理及实例,spring4.x+Quartz.2.2.1结合的开发,静态和动态实例
lucene quartz 例子lucene quartz 例子lucene quartz 例子lucene quartz 例子lucene quartz 例子lucene quartz 例子lucene quartz 例子lucene quartz 例子lucene quartz 例子lucene quartz 例子
关于spring中quartz的配置
深入解读Quartz的原理,定时任务框架是web开发过程中使用很多的框架之一
Quartz框架的核心是调度器。调度器负责管理Quartz应用运行时环境。调度器不是靠自己做所有的工作,而是依赖框架内一些非常重要的部件。 Quartz不仅仅是线程和线程管理。为确保可伸缩性,Quartz采用了基于多线程的...
详细介绍CronExpression表达式设定定时任务的规则
quartz自动调度的3种调度形式+传参 1.单纯的quartz调度 2.spring+quartz调度