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

z1340954953發表於2018-08-02

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

<!-- quartz 的jar -->
    <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>
    <!-- spring相關jar -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>3.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>

    <!-- 日誌相關jar包 -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.5</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>1.7.5</version>
    </dependency>
    <dependency><!--   Failed to load class "org.slf4j.impl.StaticLoggerBinder". -->
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.0.13</version>
    </dependency>
    <!-- MySql的包 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.31</version>
    </dependency>

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的任務.....");
	}
}
  <!-- 定義任務1-->
        <bean id="jobDetail1" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        	<!-- 定義job名稱,group名稱 -->
        	<property name="name" value="myjob1"></property>
        	<property name="group" value="group1"></property>
        	<!-- 指定job類 -->
        	<property name="jobClass" value="cn.bing.quartz.HelloJob"></property>
        	<!-- 必須設定為true,當沒有活動的觸發器和任務關聯,排程器會自動刪除任務 -->
        	<property name="durability" value="true"></property>
        	<!-- 指定spring上下文容器的key,這樣在execute方法中,就能獲取到上下文,從而獲取到bean -->
        	<property name="applicationContextJobDataKey" value="applicationContext"></property>
        </bean>

依賴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觸發的任務");
	}
}
 <bean id="jobDetail2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
           	<!-- 指定工作類 -->
            <property name="targetObject">
				<bean class="cn.bing.quartz.TestJob"></bean>
            </property>
            <!-- 工作類的執行方法 -->
            <property name="targetMethod">
                <value>work</value>
            </property>
        </bean>

建立觸發器

建立兩種常見的觸發器simpleTrigger和cronTrigger,分別註冊到上面兩種方式生成的jobDetail

建立SimpleTrigger

  <!-- 定義觸發器 -->
        <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        	<!-- 定義觸發器名稱 -->
        	<property name="name" value="trigger1"></property>
        	<!-- 定義所屬組的名稱 -->
        	<property name="group" value="group1"></property>
        	<!-- 執行間隔是5s -->
        	<property name="repeatInterval" value="5000"></property>
        	<!-- 執行次數10次 -->
        	<property name="repeatCount" value="10"></property>
        	<!-- 繫結的任務 -->
        	<property name="jobDetail" ref="jobDetail1"></property>
        </bean>

建立CronTrigger

<!-- 定義cron觸發器 -->
         <!-- 2.2:定義觸發器的bean,定義一個Cron的Trigger,一個觸發器只能和一個任務進行繫結 -->
    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <!-- 指定Trigger的名稱 -->
        <property name="name" value="hw_trigger"/>
        <!-- 指定Trigger的名稱 -->
        <property name="group" value="hw_trigger_group"/>
        <!-- 指定Tirgger繫結的Job -->
        <property name="jobDetail" ref="jobDetail2"/>
        <!-- 指定Cron 的表示式 ,當前是每隔1s執行一次 -->
        <property name="cronExpression" value="0/1 * * * * ?" />
    </bean>

定義排程器scheduler

將觸發器註冊到任務中

 <!-- 定義排程器,將觸發器註冊到任務中 -->
        <bean id="scheduler1" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" 
        lazy-init="false"
        >
        	<property name="triggers">
        		<list>
					<ref bean="simpleTrigger"/>
					<ref bean="cronTrigger"/>
        		</list>
        	</property>
        </bean>

測試

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

相關文章