quartz 配置运行
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了quartz 配置运行相关的知识,希望对你有一定的参考价值。
这篇文章是对quartz 2.2.1进行配置
分为spring 整合版本和QuartzInitializerServlet整合版本
首先是QuartzInitializerServlet整合版本
主要是参考下面这篇博客
http://liuzidong.iteye.com/blog/1149537
和官网教程
http://www.quartz-scheduler.org/documentation/quartz-2.2.x/tutorials/
和API文档
http://www.quartz-scheduler.org/api/2.2.1/index.html
配置文件
porm.xml
到
http://www.quartz-scheduler.org/downloads/
复制porm
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> <version>2.2.1</version> </dependency>
还要添加jta
要不会报
java.lang.NoClassDefFoundError: javax/transaction/UserTransaction
根据:http://www.cnblogs.com/panie2015/p/5625439.html
添加对应jar就好了
<dependency> <groupId>javax.transaction</groupId> <artifactId>jta</artifactId> <version>1.1</version> </dependency>
之后是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>2</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>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>
这样子可以让任务在tomcat启动时就自动启动,关闭之后自动关闭
主要参考API文档:
http://www.quartz-scheduler.org/api/2.2.1/org/quartz/ee/servlet/QuartzInitializerServlet.html
里面有示例
之后是
quartz_job.xml
quartz.properties
配置
首先是
quartz.properties配置:
参考:
http://www.quartz-scheduler.org/documentation/quartz-2.2.x/configuration/
有不懂得可以点到相关API文档里去看
org.quartz.scheduler.instanceName = DefaultQuartzScheduler org.quartz.scheduler.instanceId = one org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount = 10 org.quartz.threadPool.threadPriority = 5 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.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin org.quartz.plugin.shutdownhook.cleanShutdown =true
然后是quartz_job.xml
http://www.quartz-scheduler.org/documentation/quartz-2.x/cookbook/JobInitPlugin.html
注意这里
<group>DEFAULT</group>
要是default才可以,要不tomcat不会去执行
<?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_1_8.xsd" version="1.8"> <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>my-very-clever-job</name> <group>DEFAULT</group> <description>The job description</description> <job-class>com.xmal.dms.quatz.job</job-class> <job-data-map> <entry> <key>timeout</key> <value>5</value> </entry> </job-data-map> </job> <trigger> <cron> <name>my-trigger</name> <group>DEFAULT</group> <job-name>my-very-clever-job</job-name> <job-group>DEFAULT</job-group> <!-- trigger every night at 23:30 am --> <cron-expression>0 30 23 * * ?</cron-expression> </cron> </trigger> </schedule> </job-scheduling-data>
之后是job类
package com.xmal.dms.quatz; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class job implements Job { private int timeout; public void setTimeout(int timeout) { this.timeout = timeout; } //时间超出了就不行 @Override public void execute(JobExecutionContext context) throws JobExecutionException { // TODO Auto-generated method stub System.out.println("hello1"); for(int i=0;i<timeout;i++) System.out.println("hello"); }
}
相当于一个bean
private int timeout; public void setTimeout(int timeout) { this.timeout = timeout; }
这么写就可以直接在代码里使用
首先是QuartzInitializerServlet整合版本
以上是关于quartz 配置运行的主要内容,如果未能解决你的问题,请参考以下文章
spring boot 整合 quartz 集群环境 实现 动态定时任务配置原
java spring quartz如何配置让job只运行一次?
如何在 Windows 服务中使用 Quartz.Net 安排任务?