Spring整合Quartz案例使用RAM存储方式

spring整合quartz有两种任务存储方式,RAM存储方式和JDBC存储方式,企业开发使用的JDBC存储方式。

先从RAM存储方式开始说起 

目录

Jar包依赖

quartz-config.xml

创建jobDetail

依赖org.springframework.scheduling.quartz.JobDetailFactoryBean

依赖org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean

创建触发器

创建SimpleTrigger

创建CronTrigger

定义调度器scheduler

测试


Jar包依赖

因为整合spring过程中对quartz和spring都有版本上的要求,便于管理使用maven


    
         org.quartz-scheduler
         quartz
         2.2.1
    
    
        org.quartz-scheduler
        quartz-jobs
        2.2.1
    
    
        
            org.springframework
            spring-context
            4.0.5.RELEASE
        
        
            org.springframework
            spring-context-support
            4.0.5.RELEASE
        
        
            org.springframework
            spring-jdbc
            4.0.5.RELEASE
        
        
            org.springframework
            spring-orm
            4.0.5.RELEASE
        
        
            org.springframework
            spring-tx
            4.0.5.RELEASE
        
        
            org.springframework
            spring-aspects
            3.2.3.RELEASE
        
        
            org.springframework
            spring-test
            4.0.5.RELEASE
        
        
            org.springframework
            spring-oxm
            4.0.5.RELEASE
        

    
    
        org.slf4j
        slf4j-api
        1.7.5
    
    
        org.slf4j
        jcl-over-slf4j
        1.7.5
    
    
        ch.qos.logback
        logback-classic
        1.0.13
    
    
    
        mysql
        mysql-connector-java
        5.1.31
    

quartz-config.xml

将quartz的创建交给spring,整合的步骤

创建jobDetail

spring中有两种方式:

依赖org.springframework.scheduling.quartz.JobDetailFactoryBean

要求任务类必须实现job接口,而且需要将durability属性配置为true,否则容器初始化时候,因为没有触发器和任务关联会报错。

工作类:

public class HelloJob  implements Job{

	public void execute(JobExecutionContext paramJobExecutionContext)
			throws JobExecutionException {
		System.out.println("触发simpleScheduler的任务.....");
	}
}
  
        
        	
        	
        	
        	
        	
        	
        	
        	
        	
        

依赖org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean

不要求任务类必须实现job接口

工作类:

public class TestJob {
	public void work(){
		System.out.println("北京时间:"+new SimpleDateFormat("yyyyMMdd HH:mm:ss").format(new Date())+"执行cron触发的任务");
	}
}
 
           	
            
				
            
            
            
                work
            
        

创建触发器

创建两种常见的触发器simpleTrigger和cronTrigger,分别注册到上面两种方式生成的jobDetail

创建SimpleTrigger

  
        
        	
        	
        	
        	
        	
        	
        	
        	
        	
        	
        

创建CronTrigger


         
    
        
        
        
        
        
        
        
        
    

定义调度器scheduler

将触发器注册到任务中

 
        
        	
        		
					
					
        		
        	
        

测试

public class JunitTest {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
	}
}

spring容器创建,就会启动quartz,触发任务执行

执行结果:

北京时间:20180802 14:55:25:  执行cron触发的任务
14:55:25.004 [scheduler1_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
14:55:25.540 [scheduler1_Worker-4] DEBUG org.quartz.core.JobRunShell - Calling execute on job group1.myjob1
14:55:25.541 [scheduler1_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
北京时间:20180802 14:55:25:   执行simpleScheduler触发的任务.....
14:55:26.001 [scheduler1_Worker-5] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.jobDetail2
北京时间:20180802 14:55:26:  执行cron触发的任务
14:55:26.003 [scheduler1_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
14:55:27.003 [scheduler1_Worker-6] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.jobDetail2
14:55:27.003 [scheduler1_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
北京时间:20180802 14:55:27:  执行cron触发的任务

参考博客:https://blog.csdn.net/u010648555/article/details/54891264

源码下载:https://download.csdn.net/download/ditto_zhou/10579402

你可能感兴趣的:(quartz)