`
lumingming1987
  • 浏览: 117428 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

转spring配置定时任务quartz2

 
阅读更多

 Quartz 是一个功能强大的作业调度工具,相当于数据库中的 Job、Windows 的计划任务、Unix/Linux 下的 Cron,但 Quartz 可以把排程控制的更精细。也许大多数人听说 Quartz 是在学习或使用 Spring 的时候,也就是 Spring 整合了Quartz。由于项目中使用了Quartz来实现定时通过接口请求数据的功能,这几天自己查阅资料学习Quartz,在此记录一下。现在分三个步骤演示一下Quartz的使用:在控制台使用Quartz、整合Spring与Quartz、将Quartz任务信息持久化到数据库中。

  我的开发环境:Eclipse3.6+jdk1.6.0_26+Tomcat6.0.20,现在Quartz最新版本是2.0.2,下载地址 http://quartz-scheduler.org/downloads/catalog。创建一个Web应用程序,将Quartz2.0.2解压后lib文件夹下的jar包(quartz依赖的包)以及quartz-2.0.2.jar和quartz-oracle-2.0.2.jar(支持Oracle的)拷贝到WEB-INF/lib下,这样即完成示例的准备工作。

一、在控制台使用Quartz

首先创建一个简单的Job,ExampleJob.java代码如下:

[java] view plaincopy
 
  1. package com.petrochina.job;  
  2. import org.quartz.Job;  
  3. import org.quartz.JobDataMap;  
  4. import org.quartz.JobExecutionContext;  
  5. import org.quartz.JobExecutionException;  
  6. import org.slf4j.Logger;  
  7. import org.slf4j.LoggerFactory;  
  8. public class ExampleJob implements Job {  
  9.     private Logger logger = LoggerFactory.getLogger(ExampleJob.class);  
  10.     @Override  
  11.     public void execute(JobExecutionContext context) throws JobExecutionException {  
  12.         System.out.print("I can count to 10 ->");  
  13.         // 输出1-10  
  14.         for (int i = 1; i <= 10; i++) {  
  15.             System.out.print(" | " + i + " ");  
  16.             try {  
  17.                 Thread.sleep(1000);  
  18.             } catch (InterruptedException ie) {  
  19.             }  
  20.         }  
  21.         System.out.println("<- See I did it.");  
  22.         JobDataMap properties = context.getJobDetail().getJobDataMap();  
  23.         System.out.println("Previous Fire Time: " + context.getPreviousFireTime());// 上次执行时间  
  24.         System.out.println("Current Fire Time: " + context.getFireTime());// 本次执行时间  
  25.         System.out.println("Next Fire Time: " + context.getNextFireTime());// 下一次执行时间  
  26.     }  
  27. }  

控制台程序如下:

[java] view plaincopy
 
  1. package com.petrochina.job;  
  2. import static org.quartz.JobBuilder.newJob;  
  3. import static org.quartz.SimpleScheduleBuilder.simpleSchedule;  
  4. import static org.quartz.TriggerBuilder.newTrigger;  
  5. import java.util.Date;  
  6. import org.quartz.JobDetail;  
  7. import org.quartz.Scheduler;  
  8. import org.quartz.SchedulerException;  
  9. import org.quartz.SchedulerFactory;  
  10. import org.quartz.SchedulerMetaData;  
  11. import org.quartz.Trigger;  
  12. import org.quartz.impl.StdSchedulerFactory;  
  13. public class Console {  
  14.     public static void main(String[] args) {  
  15.         try {  
  16.             testJob();  
  17.         } catch (Exception e) {  
  18.             e.printStackTrace();  
  19.         }  
  20.     }  
  21.     // 测试使用quartz实现的调度任务  
  22.     public static void testJob() throws SchedulerException, InterruptedException {  
  23.         // 创建调度者工厂  
  24.         SchedulerFactory sfc = new StdSchedulerFactory();  
  25.         // 通过工厂创建一个调度者  
  26.         Scheduler scheduler = sfc.getScheduler();  
  27.          /*//----------Quartz1.8.4的写法---------------// 
  28.  
  29.          // 创建一个任务,命名“myjob”,组名“group1”,对应工作类“ExampleJob” 
  30.          JobDetail myJob = new JobDetail("myjob", "group1", ExampleJob.class); 
  31.          // 使用触发器工具类创建一个每隔15秒执行一次的触发器 
  32.          Trigger trigger = TriggerUtils.makeSecondlyTrigger(15); 
  33.          trigger.setName("mytrigger"); 
  34.          trigger.setStartTime(new Date()); 
  35.          */  
  36.         //----------Quartz 2.0.2的写法---------------//  
  37.         JobDetail myJob = newJob(ExampleJob.class).withIdentity("myJob""job-group").build();  
  38.         Trigger trigger = newTrigger().withIdentity("mytrigger""trigger-group").startAt(new Date())  
  39.                 .withSchedule(simpleSchedule().withIntervalInSeconds(15).repeatForever()).build();nbsp;       // 调度任务  
  40.         Date startDate = scheduler.scheduleJob(myJob, trigger);  
  41.         System.out.println(myJob.getKey() + " will start at:" + startDate.toLocaleString());  
  42.         // 开始运行调度程序  
  43.         scheduler.start();        Thread.sleep(20000);// 等待20秒  
  44.         scheduler.shutdown();// 关闭调度程序        SchedulerMetaData metaData = scheduler.getMetaData();  
  45.         System.out.println("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");  
  46.         System.out.println("Test end------>");    }}  

执行结果如下:

job-group.myJob will start at:2011-9-2 15:15:02
2011-09-02 15:15:02,046 [main] INFO  [org.quartz.core.QuartzScheduler] - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.
I can count to 10 -> | 1  | 2  | 3  | 4  | 5  | 6  | 7  | 8  | 9  | 10 <- See I did it.
Previous Fire Time: null
Current Fire Time: Fri Sep 02 15:15:02 CST 2011
Next Fire Time: Fri Sep 02 15:15:17 CST 2011
I can count to 10 -> | 1  | 2  | 3  | 4  | 5  | 6 2011-09-02 15:15:22,046 [main] INFO  [org.quartz.core.QuartzScheduler] - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutting down. //sleep20秒后关闭调度程序
2011-09-02 15:15:22,046 [main] INFO  [org.quartz.core.QuartzScheduler] - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED paused.  
 | 7  | 8  | 9  | 10 <- See I did it.  //已经启动的Job继续执行完成
Previous Fire Time: Fri Sep 02 15:15:02 CST 2011
Current Fire Time: Fri Sep 02 15:15:17 CST 2011
Next Fire Time: Fri Sep 02 15:15:32 CST 2011
2011-09-02 15:15:27,031 [main] INFO  [org.quartz.core.QuartzScheduler] - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutdown complete.
Executed 2 jobs.
Test end------>

二、整合Spring与Quartz

    spring增加了对Quartz的支持,可以方便的在spring中配置调度程序,而不需要编写代码。首先要添加spring的支持:可以到官网http://www.springsource.org/download下载spring 的jar包,我使用的是3.0.5.RELEASE版本的,将下面的jar包拷贝到WEB-INF/lib下,同时这里的Quartz要使用1.8.5及其以下版本,而不能使用2.0.2版,原因后面讲。

commons-logging.jar

spring-core-3.0.5.RELEASE.jar

spring-beans-3.0.5.RELEASE.jar

spring-context-3.0.5.RELEASE.jar

spring-context-support-3.0.5.RELEASE.jar

spring-asm-3.0.5.RELEASE.jar

spring-expression-3.0.5.RELEASE.jar

spring.transaction-3.0.5.RELEASE.jar

spring-web-3.0.5.RELEASE.jar

添加spring配置文件applicationContext.xml

[html] view plaincopy
 
  1. <!-- 配置调度程序quartz ,其中配置JobDetail有两种方式    -->  
  2.     <!--方式一:使用JobDetailBean,任务类必须实现Job接口  
  3.     <bean id="myjob" class="org.springframework.scheduling.quartz.JobDetailBean">  
  4.      <property name="name" value="exampleJob"></property>  
  5.      <property name="group" value="group1"></property>  
  6.      <property name="jobClass" value="com.petrochina.job.ExampleJob"></property>  
  7.     </bean> -->  
  8.     <!-- 方式二:使用MethodInvokingJobDetailFactoryBean,任务类可以不实现Job接口,通过targetMethod指定调用方法-->  
  9.     <bean id="exampleJob" class="com.petrochina.job.ExampleJob2"></bean>  
  10.     <bean id="myjob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
  11.      <property name="targetObject" ref="exampleJob"/>  
  12.      <property name="targetMethod" value="execute"/>  
  13.      <property name="concurrent" value="false"/>  
  14.     </bean>   
  15.     <!-- 定义名为mytrigger的触发器 -->  
  16.     <bean id="mytrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  
  17.      <property name="jobDetail" ref="myjob"/>  
  18.      <property name="cronExpression">  
  19.       <value>0/15 * * * * ? </value>  
  20.      </property>  
  21.     </bean>  
  22.    <!-- 定义调度器 -->  
  23.     <bean id="myscheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false" autowire="no">  
  24.      <property name="triggers">  
  25.       <list>  
  26.        <ref bean="mytrigger"/>  
  27.       </list>  
  28.      </property>  
  29.      <property name="quartzProperties">  
  30.       <props>  
  31.        <prop key="org.quartz.threadPool.threadCount">1</prop>  
  32.       </props>  
  33.      </property>  
  34.     </bean>  

说明:在spring中配置JobDetail有两种方式,第一种是使用org.springframework.scheduling.quartz.JobDetailBean,这种方式ExampleJob要实现Job接口;第二种是使用org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean调用指定类的指定方法,这个比较灵活,这种方式下ExampleJob2的代码如下:

[java] view plaincopy
 
  1. package com.petrochina.job;  
  2.   
  3. public class ExampleJob2 {  
  4.   
  5.     public void execute() {  
  6.         System.out.print("I can count to 10 ->");  
  7.   
  8.         for (int i = 1; i <= 10; i++) {  
  9.             System.out.print(" | " + i + " ");  
  10.             try {  
  11.                 Thread.sleep(1000);  
  12.             } catch (InterruptedException ie) {  
  13.             }  
  14.         }  
  15.   
  16.         System.out.println("<- See I did it.");  
  17.     }  
  18. }  

这样只要启动spring容器即可启动调度程序。

1、使用方法testJob启动spring容器

[java] view plaincopy
 
  1. public static void testJob() throws InterruptedException, SchedulerException {  
  2.        // 方法一:基于spring配置job、trigger、scheduler之间的关联关系  
  3.        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  
  4.        Scheduler scheduler = (Scheduler) context.getBean("myscheduler");  
  5.   
  6.       Thread.sleep(20000);// 等待20秒  
  7.        scheduler.shutdown();// 关闭调度程序  
  8.   
  9.        SchedulerMetaData metaData = scheduler.getMetaData();  
  10.       System.out.println("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");  
  11.    }  

结果如下:

2011-09-02 16:52:27,203 [main] INFO  [org.quartz.impl.StdSchedulerFactory] - Quartz scheduler 'myscheduler' initialized from an externally provided properties instance.
2011-09-02 16:52:27,203 [main] INFO  [org.quartz.impl.StdSchedulerFactory] - Quartz scheduler version:1.8.5
2011-09-02 16:52:27,203 [main] INFO  [org.quartz.core.QuartzScheduler] - JobFactory set to:org.springframework.scheduling.quartz.AdaptableJobFactory@bf7190
2011-09-02 16:52:27,203 [main] INFO  [org.springframework.context.support.DefaultLifecycleProcessor] - Starting beans in phase 2147483647
2011-09-02 16:52:27,203 [main] INFO  [org.springframework.scheduling.quartz.SchedulerFactoryBean] - Starting Quartz Scheduler now
2011-09-02 16:52:27,203 [main] INFO  [org.quartz.core.QuartzScheduler] - Scheduler myscheduler_$_NON_CLUSTERED started.
I can count to 10 -> | 1  | 2  | 3  | 4  | 5  | 6  | 7  | 8  | 9  | 10 <- See I did it.
I can count to 10 -> | 1  | 2  | 3 2011-09-02 16:52:47,203 [main] INFO  [org.quartz.core.QuartzScheduler] - Scheduler myscheduler_$_NON_CLUSTERED shutting down.
2011-09-02 16:52:47,203 [main] INFO  [org.quartz.core.QuartzScheduler] - Scheduler myscheduler_$_NON_CLUSTERED paused.
2011-09-02 16:52:47,203 [main] INFO  [org.quartz.core.QuartzScheduler] - Scheduler myscheduler_$_NON_CLUSTERED shutdown complete.
Executed 2 jobs.
 | 4  | 5  | 6  | 7  | 8  | 9  | 10 <- See I did it.
可见和在控制台编写代码实现的效果一样。

2、在web.xml中添加listener来启动spring容器

[html] view plaincopy
 
  1. <!-- 配置启动spring容器 -->  
  2.     <listener>  
  3.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  4.     </listener>  

注意!!!在做示例的时候发现使用Quartz2.0.2+Spring3.0.5配置的applicationContext运行时会出错:

Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [org.springframework.scheduling.quartz.CronTriggerBean] for bean with name 'mytrigger' defined in class path resource [applicationContext.xml]: problem with class file or dependent class; nested exception is java.lang.IncompatibleClassChangeError: class org.springframework.scheduling.quartz.CronTriggerBean has interface org.quartz.CronTrigger as super class

查看发现spring3.0.5中org.springframework.scheduling.quartz.CronTriggerBean继承了org.quartz.CronTrigger(public class CronTriggerBeanextends CronTrigger),而在quartz2.0.2中org.quartz.CronTrigger是个接口(publicabstract interface CronTrigger extends Trigger),而在quartz1.8.5及1.8.4中org.quartz.CronTrigger是个类(publicclass CronTrigger extends Trigger),从而造成无法在applicationContext中配置触发器。这是spring3.0.5和quartz2.0.2版本不兼容的一个bug。

三、将Quartz任务信息持久化到数据库中

  Quartz默认将运行信息存放在内存中,一旦程序重启那么以前的任务信息就会丢失,最保险的方式就是将任务信息持久化到数据库中。这里还是使用Quartz2.0.2+Oracle10g来做示例

1、将解压包里的quartz-oracle-2.0.2.jar以及commons-dbcp-1.3.jar 、commons-pool-1.5.4.jar、ojdbc6-11.1.0.7.0.jar拷贝到WEB-INF/lib下。

2、创建quartz配置文件quartz.properties

[html] view plaincopy
 
  1. #============================================================================  
  2. # Configure Main Scheduler Properties    
  3. #============================================================================  
  4. org.quartz.scheduler.instanceName = My_Quartz  
  5. org.quartz.scheduler.instanceId = AUTO  
  6.   
  7. #============================================================================  
  8. # Configure ThreadPool    
  9. #============================================================================  
  10. org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool  
  11. org.quartz.threadPool.threadCount = 5  
  12. org.quartz.threadPool.threadPriority = 5  
  13. org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true  
  14.   
  15. #============================================================================  
  16. # Configure JobStore single  
  17. #============================================================================  
  18. #RAM  
  19. #org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore #这是默认的  
  20.   
  21. #============================================================================  
  22. # Configure JobStore Cluster  
  23. #============================================================================  
  24. org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX  
  25. org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.oracle.OracleDelegate  
  26. org.quartz.jobStore.tablePrefix = QRTZ_  
  27. #org.quartz.jobStore.misfireThreshold = 60000  
  28. #org.quartz.jobStore.isClustered = true  
  29. #org.quartz.jobStore.clusterCheckinInterval = 15000  
  30.   
  31. org.quartz.jobStore.dataSource = myDS  #指定数据源  
  32. #============================================================================  
  33. # Configure dataSource    
  34. #============================================================================  
  35. #dataSource--myDS  
  36. org.quartz.dataSource.myDS.driver = oracle.jdbc.driver.OracleDriver  
  37. org.quartz.dataSource.myDS.URL = jdbc:oracle:thin:@127.0.0.1:1521:cui  
  38. org.quartz.dataSource.myDS.user = cui  
  39. org.quartz.dataSource.myDS.password = cui  
  40. org.quartz.dataSource.myDS.maxConnections = 10  

3、根据quartz-2.0.2\docs\dbTables\tables_oracle.sql在数据库中创建Quartz保存任务信息需要的12张表(针对各种数据库的创建语句都有),注意quartz1.8.5和quartz2.0.2有些表的字段有些差异。

4、使用ContextLoaderListener中初始化的StdSchedulerFactory获取Scheduler来调度Job,这样Job的运行信息就会持久化到数据库。这里我创建一个Listener在程序部署时调度Job,当然你也可以在写一个页面来添加、启动、暂停一个Job。

QuartzListener.java

[java] view plaincopy
 
  1. import static org.quartz.JobBuilder.newJob;  
  2. import static org.quartz.SimpleScheduleBuilder.simpleSchedule;  
  3. import static org.quartz.TriggerBuilder.newTrigger;  
  4. import java.util.Date;  
  5. import javax.servlet.ServletContextEvent;  
  6. import javax.servlet.ServletContextListener;  
  7. import org.quartz.JobDetail;  
  8. import org.quartz.JobKey;  
  9. import org.quartz.Scheduler;  
  10. import org.quartz.SchedulerException;  
  11. import org.quartz.SimpleTrigger;  
  12. import org.quartz.ee.servlet.QuartzInitializerListener;  
  13. import org.quartz.impl.StdSchedulerFactory;  
  14. public class QuartzListener implements ServletContextListener {  
  15.     @Override  
  16.     public void contextDestroyed(ServletContextEvent arg0) {  
  17.     }  
  18.     @Override  
  19.     public void contextInitialized(ServletContextEvent arg0) {  
  20.         StdSchedulerFactory factory = (StdSchedulerFactory) arg0.getServletContext().getAttribute(QuartzInitializerListener.QUARTZ_FACTORY_KEY);  
  21.         Scheduler scheduler = null;  
  22.         try {  
  23.             scheduler = factory.getScheduler();  
  24.             // -----------Quartz2.0.2--------------//  
  25.             // 如果不存在名为“myjob”,组名为“group1”的Job,则添加进去  
  26.             if (scheduler.getJobDetail(new JobKey("myjob""group1")) == null) {  
  27.                 JobDetail myJob = newJob(ExampleJob.class).withIdentity("myjob""group1").build();  
  28.                 SimpleTrigger trigger = newTrigger().withIdentity("mytrigger""trigger-group").startAt(new Date())  
  29.                         .withSchedule(simpleSchedule().withIntervalInSeconds(15).repeatForever()).build();  
  30.                 scheduler.scheduleJob(myJob, trigger);  
  31.             }  
  32.         } catch (SchedulerException e) {  
  33.             e.printStackTrace();  
  34.         }  
  35.     }}  

配置web.xml

[html] view plaincopy
 
  1.         <!-- 配置启动spring容器 -->  
  2. <listener>  
  3.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  4. </listener>  
  5. <!-- 配置Quartz -->  
  6. <listener>  
  7.     <listener-class>org.quartz.ee.servlet.QuartzInitializerListener</listener-class>  
  8. </listener>  
  9. <!-- 使用Quartz调度Job的Listener -->  
  10. <listener>  
  11.     <listener-class>com.petrochina.job.QuartzListener</listener-class>  
  12. </listener>  

 

5、将上面applicationContext.xml中使用bean配置的调度程序去掉,因为它是使用org.springframework.scheduling.quartz.SchedulerFactoryBean创建的Scheduler,它配置的调度程序不能持久化入库。启动Tomcat,计数的Job开始工作,查看数据库。


可见,关于Job和Trigger的信息已经入库,但下次程序部署时Quartz会自动还原各个调度程序的状态。

 

另:Quartz的CronTrigger配置相当灵活,下面贴出一些Cron的资料

格式

A cron expression is a string comprised of 6 or 7 fields separated by white space. Fields can contain any of the allowed values, along with various combinations of the allowed special characters for that field. The fields are as follows:

 

Field Name Mandatory Allowed Values Allowed Special Characters
Seconds YES 0-59 , - * /
Minutes YES 0-59 , - * /
Hours YES 0-23 , - * /
Day of month YES 1-31 , - * ? / L W
Month YES 1-12 or JAN-DEC , - * /
Day of week YES 1-7 or SUN-SAT , - * ? / L #
Year NO empty, 1970-2099 , - * /

 

So cron expressions can be as simple as this: * * * * ? *
or more complex, like this: 0 0/5 14,18,3-39,52 ? JAN,MAR,SEP MON-FRI 2002-2010

样例

Here are some full examples:

 

Expression Meaning
0 0 12 * * ? Fire at 12pm (noon) every day
0 15 10 ? * * Fire at 10:15am every day
0 15 10 * * ? Fire at 10:15am every day
0 15 10 * * ? * Fire at 10:15am every day
0 15 10 * * ? 2005 Fire at 10:15am every day during the year 2005
0 * 14 * * ? Fire every minute starting at 2pm and ending at 2:59pm, every day
0 0/5 14 * * ? Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day
0 0/5 14,18 * * ? Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day
0 0-5 14 * * ? Fire every minute starting at 2pm and ending at 2:05pm, every day
0 10,44 14 ? 3 WED Fire at 2:10pm and at 2:44pm every Wednesday in the month of March.
0 15 10 ? * MON-FRI Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday
0 15 10 15 * ? Fire at 10:15am on the 15th day of every month
0 15 10 L * ? Fire at 10:15am on the last day of every month
0 15 10 ? * 6L Fire at 10:15am on the last Friday of every month
0 15 10 ? * 6L Fire at 10:15am on the last Friday of every month
0 15 10 ? * 6L 2002-2005 Fire at 10:15am on every last friday of every month during the years 2002, 2003, 2004 and 2005
0 15 10 ? * 6#3 Fire at 10:15am on the third Friday of every month
0 0 12 1/5 * ? Fire at 12pm (noon) every 5 days every month, starting on the first day of the month.
0 11 11 11 11 ? Fire every November 11th at 11:11am.

 

 Pay attention to the effects of '?' and '*' in the day-of-week and day-of-month fields!
分享到:
评论

相关推荐

    转:spring多个定时任务quartz配置

    在Spring框架中,Quartz是一个强大的任务调度库,可以用于执行定时任务。本文将深入探讨如何在Spring中配置多个Quartz定时任务,并结合`quartz.properties`文件进行详细讲解。 首先,我们需要理解Quartz的基本概念...

    spring多个定时任务quartz配置

    4. **声明JobDetail**:在Spring配置文件中声明JobDetail,这会告诉Quartz如何实例化和执行Job。例如: ```xml &lt;bean id="myJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"&gt; ...

    spring整合java quartz实现动态定时任务的前台网页配置与管理

    在实际项目应用中经常会用到定时任务,可以通过quartz和spring的简单配置即可完成,但如果要改变任务的执行时间、频率,废弃任务等就需要改变配置甚至代码需要重启服务器,这里介绍一下如何通过quartz与spring的组合...

    spring之定时任务实现(spring-task和quartz等不同方式)

    通过注解方式配置定时任务简洁明了,适用于快速开发;非注解方式则提供了更大的灵活性,可以满足更多定制化需求。结合提供的文档和源码工程,读者可以深入理解并实践这两种定时任务的实现方式,提升自己的技能水平。

    spring3配置quartz定时任务

    2. **创建作业bean**:在Spring配置文件中声明这个作业bean,并设置其类为`MyJob`: ```xml ``` 3. **定义触发器**:触发器控制作业何时执行。Quartz提供了多种类型的触发器,如SimpleTrigger、CronTrigger等。...

    springboot整合quartz定时任务yml文件配置方式

    以下将详细介绍如何在Spring Boot应用中使用YAML文件配置Quartz定时任务,以及涉及的Spring Cloud Alibaba、Dubbo和Nacos的相关知识。 首先,我们需要在`pom.xml`中引入相关的依赖。Spring Boot的`spring-boot-...

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

    首先,我们需要在Spring配置文件中声明一个SchedulerFactoryBean,然后定义JobDetail和Trigger,分别表示任务内容和触发条件。JobDetail可以绑定到一个Spring的bean,这样每次任务执行时,Spring会自动管理bean的...

    spring quartz定时任务demo

    总的来说,“spring quartz定时任务demo”提供了一个直观的教程,帮助开发者理解如何在Spring项目中配置和使用Quartz进行定时任务的创建和管理。通过这个例子,你可以快速学习到如何定义Job,如何配置Trigger,以及...

    Springboot2-Quartz 后台可动态配置的定时任务

    本项目“Springboot2-Quartz 后台可动态配置的定时任务”是基于SpringBoot 2.x版本与Quartz Scheduler整合的一个示例,它展示了如何在后台管理系统中动态地创建、更新和删除定时任务,以及监控这些任务的状态,为...

    spring quartz通过xml配置实现定时任务demo

    本示例将详细讲解如何通过XML配置文件来实现Spring与Quartz的集成,从而构建一个定时任务Demo。 首先,Quartz是开源的作业调度框架,允许应用程序在预定义的时间执行特定的任务。Spring框架则提供了强大的依赖注入...

    Spring 框架自带定时任务和Quartz定时任务

    在这两种方式中,Spring框架提供了自己的定时任务工具Spring Task,以及与专业定时任务框架Quartz集成的能力。 首先,对于Java自带的定时任务实现,我们可以使用java.util.Timer和java.util.TimerTask类。Timer类...

    Spring的定时任务开发及对Quartz和Timer支持

    在【标题】"Spring的定时任务开发及对Quartz和Timer支持"中,涉及到的是Spring在处理定时任务方面的特性,这在企业级应用中非常常见,用于执行一些周期性的后台任务,如数据同步、报表生成、清理任务等。 首先,...

    Spring配置定时任务

    在Spring框架中,定时任务是通过Spring的Task模块来实现的,这允许我们在应用程序中创建和管理定时任务,而无需依赖外部的调度器如Quartz或Cron。本例主要探讨如何在Spring中配置和执行定时任务。 首先,我们要引入...

    Spring+Quartz 从数据库中获取定时任务和定时时间,动态实现对定时任务的增删改查

    2. **从数据库中获取定时任务**:Quartz允许将任务和触发器的信息存储在数据库中,这可以通过实现`SchedulerFactoryBean`的`overwriteExistingJobs`属性为`false`来实现。这样,当Quartz启动时,它会从数据库中读取...

    springboot+quartz 动态化配置定时任务

    通过整合SpringBoot和Quartz,我们可以利用Spring的自动配置能力,方便地在Spring应用中集成定时任务功能。 二、Quartz动态配置 1. 引入依赖:首先,在`pom.xml`文件中引入SpringBoot的SpringBoot-starter-quartz...

    spring多个定时任务quartz配置 easy518网址导航

    接下来,我们将详细介绍如何通过Spring配置文件来定义多个Quartz定时任务。 #### 2.1 定义定时任务 首先,我们需要定义具体的定时任务类。例如,在给定的示例中定义了两个任务类`Job1`和`Job2`,它们分别对应不同的...

    Spring整合quartz2.2.3总结,quartz动态定时任务,Quartz定时任务集群配置

    Spring整合Quartz 2.2.3是Java开发者在实现定时任务时常用的一种技术组合。Quartz是一款开源的作业调度框架,它允许程序在特定时间执行预定的任务,而Spring则是一个强大的企业级应用开发框架,提供了丰富的依赖注入...

    spring定时任务之Quartz

    需要注意的是,`ssh2Test`这个文件名可能与本主题无关,因为它是SSH2测试相关的,而SSH2通常指的是Secure Shell 2协议,用于远程访问和管理服务器,与Spring定时任务或Quartz不直接相关。如果需要了解更多关于SSH2的...

    spring整合quartz定时任务调度

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

    Spring Quartz定时任务 jar包

    2. 配置Quartz:在Spring配置文件中定义SchedulerFactoryBean,设置Quartz的相关属性,如线程池大小、Job存储方式等。 3. 定义Job:创建实现`org.quartz.Job`接口的类,这个类将包含定时任务的具体逻辑。 4. 创建...

Global site tag (gtag.js) - Google Analytics