1. 程式人生 > >Quartz儲存與持久化--mysql

Quartz儲存與持久化--mysql

工程結構:

web.xml: 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>quartz</display-name>
	<filter>
		<filter-name>encoding</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:application*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<servlet>
		<servlet-name>mvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:applicationMvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>mvc</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
</web-app>

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans.xsd
						http://www.springframework.org/schema/aop 
						http://www.springframework.org/schema/aop/spring-aop.xsd
						http://www.springframework.org/schema/context 
						http://www.springframework.org/schema/context/spring-context.xsd
						http://www.springframework.org/schema/tx 
						http://www.springframework.org/schema/tx/spring-tx.xsd">
	<!-- 配置掃描包 -->
	<context:component-scan base-package="com.test" />
	<!-- 啟用註解 -->
	<context:annotation-config />

	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="fileEncoding" value="utf-8" />
		<property name="locations">
			<list>
				<value>classpath*:config.properties</value>
				<!--<value>classpath*:quartz.properties</value>-->
			</list>
		</property>
	</bean>


	<!-- 配置quartz 資料來源 -->
	<bean id="quartzDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${quartz.driverClassName}" />
		<property name="url" value="${quartz.url}" />
		<property name="username" value="${quartz.username}" />
		<property name="password" value="${quartz.password}" />
	</bean>

	<!-- quartz持久化儲存  -->
	<bean name="quartzScheduler"
		  class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
                <!--採用對是Spring資料來源 沒有用quartz.properties配置檔案中預設對資料來源-->
		<property name="dataSource">
			<ref bean="quartzDataSource" />
		</property>
		<property name="applicationContextSchedulerContextKey" value="applicationContext" />
		<!--必須的,QuartzScheduler 延時啟動,應用啟動完後 QuartzScheduler 再啟動 -->
		<property name="startupDelay" value="3" />
		<!-- 設定自動啟動 -->
		<property name="autoStartup" value="true" />
		<property name="configLocation" value="classpath:quartz.properties" />
	</bean>


</beans>

applicationMvc.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
								http://www.springframework.org/schema/beans/spring-beans.xsd
								http://www.springframework.org/schema/context
								http://www.springframework.org/schema/context/spring-context.xsd
								http://www.springframework.org/schema/mvc 
								http://www.springframework.org/schema/mvc/spring-mvc.xsd">
	<mvc:annotation-driven/>
	<context:component-scan base-package="com.test.controller"/>
	<mvc:default-servlet-handler/>
	<!-- jsp 檢視解析器 -->
	<bean id="jspViewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/view/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
</beans>

config.properties:

quartz.driverClassName=com.mysql.jdbc.Driver
quartz.url=jdbc:mysql://xxx.xxx.xxx.xx:3306/資料庫名?useUnicode=true&characterEncoding=UTF-8
quartz.username=使用者名稱
quartz.password=密碼
quartz.minPoolSize=7
quartz.initialPoolSize=12 

log4j.properties:

#級別  控制檯輸出
log4j.rootLogger = WARN, console
#console out
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = [%c{1}|%[email protected]%d{HH\:mm\:ss.SSS}] %C{1} %L - %m%n
#custom logger  包級別輸出
log4j.logger.com.sharebo = DEBUG

quartz.properties:

# 設定排程器的例項名(instanceName) 和例項ID (instanceId)
org.quartz.scheduler.instanceName=MyScheduler
#如果使用叢集,instanceId必須唯一,設定成AUTO
#org.quartz.scheduler.instanceId: AUTO
org.quartz.scheduler.wrapJobExecutionInUserTransaction=false

org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount=3
org.quartz.threadPool.threadPriority=5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true

# 持久化配置(儲存方式使用JobStoreTX,也就是資料庫)
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
# 驅動器方言 mysql驅動
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#資料庫中quartz表的表名字首
org.quartz.jobStore.tablePrefix=qrtz_
#org.quartz.jobStore.dataSource=myDS
# 使用自己的配置檔案
org.quartz.jobStore.useProperties=true
org.quartz.jobStore.isClustered=true

#org.quartz.dataSource.myDS.driver=com.mysql.jdbc.Driver
#org.quartz.dataSource.myDS.URL=jdbc:mysql://192.168.157.61:3306/xxl-job?useUnicode=true&characterEncoding=UTF-8
#org.quartz.dataSource.myDS.user=admin
#[email protected]
#org.quartz.dataSource.myDS.maxConnections=5

org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin
org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin
org.quartz.plugin.shutdownhook.cleanShutdown = true
package com.test.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.test.util.quartz.MyJob;
import com.test.util.quartz.QuartzTest;


@RestController
public class TestController {
	@Autowired
	QuartzTest q;
	//新增一個倒計時任務
	@RequestMapping("add")
	public void add(String name) {

		q.addJob2(name, MyJob.class, "ccc", 20, "004");
	}
	//刪除一個倒計時任務
	@RequestMapping("remove")
	public Object remove(String name) {
		return q.closeJob(name, "001");
	}
	//從資料庫載入還未執行的任務(spring容器初始化的時候會自動載入)
	@RequestMapping("resume")
	public Object resume(String name) {
		q.resumeJob();
		return "OK";
	}
}
package com.test.util.quartz;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class MyJob implements Job{  
    private static final Logger logger = Logger.getLogger(MyJob.class);    
    @Override  
    public void execute(JobExecutionContext context)  
            throws JobExecutionException {  
        System.out.println("Hello quzrtz  "+   new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(new Date()));  
          
    }  
  
}
package com.test.util.quartz;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.quartz.*;
import org.quartz.core.jmx.JobDetailSupport;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.triggers.SimpleTriggerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class QuartzTest {
	@Autowired
	private Scheduler scheduler;
	private static String JOB_GROUP_NAME = "ddlib";
	private static String TRIGGER_GROUP_NAME = "ddlibTrigger";

	/**
	 * 注意:此種方式由於用到的是SimpleTriggerImpl 任務跑的過程中會存入表中,任務執行完成後,會自動刪除表中的資料 如:觸發器資訊等
	 * 新增任務
	 *
	 * @param jobName
	 *            任務名稱
	 * @param job
	 *            任務處理類 需要繼承Job
	 *            處理任務可以獲取的上下文
	 *            通過context.getMergedJobDataMap().getString("context"); 獲取
	 * @param seconds
	 *            間隔秒
	 * @return 0 新增成功 1:任務已經存在 2:新增異常
	 */
	public int addJob(String jobName, Class<? extends Job> job, Object task, int seconds, String jobGorupName) {
		try {
			// 判斷任務是否存在
			JobKey jobKey = JobKey.jobKey(jobName, jobGorupName);
			if (scheduler.checkExists(jobKey)) {
				return 1;// 任務已經存在
			}
			// 建立一個JobDetail例項,指定SimpleJob
			Map<String, Object> JobDetailmap = new HashMap<String, Object>();
			JobDetailmap.put("name", jobName);// 設定任務名字
			JobDetailmap.put("group", jobGorupName);// 設定任務組
			JobDetailmap.put("jobClass", job.getCanonicalName());// 指定執行類
			// Task.class.getCanonicalName()
			JobDetail jobDetail = JobDetailSupport.newJobDetail(JobDetailmap);
			// 新增資料內容
			jobDetail.getJobDataMap().put("task", task);// 傳輸的上下文
			// 通過SimpleTrigger定義排程規則:馬上啟動,每2秒執行一次,共執行100次 等。。。。
			SimpleTriggerImpl simpleTrigger = new SimpleTriggerImpl();
			simpleTrigger.setName(jobName);
			simpleTrigger.setGroup(TRIGGER_GROUP_NAME);
			// 什麼時候開始執行
			simpleTrigger.setStartTime(new Date());
			// 間隔時間
			simpleTrigger.setRepeatInterval(1000);
			// 最多執行次數 預設執行一次
			simpleTrigger.setRepeatCount(10);
			// 通過SchedulerFactory獲取一個排程器例項
			scheduler.scheduleJob(jobDetail, simpleTrigger);//  註冊並進行排程
			scheduler.start();// ⑤排程啟動
			return 0;// 新增成功
		} catch (Exception e) {
			// e.printStackTrace();
			return 2;// 操作異常
		}
	}

	/**
	 * 注意:此種方式由於用到的是CronScheduleBuilder 任務跑的過程中會存入表中,任務執行完成後,不會自動刪除表中的資料 會存於表中,
	 * 下次重啟程式後  會繼續按照cron表示式 執行任務如:觸發器資訊等
	 * @param jobName
	 * @param job
	 * @param task
	 * @param seconds
	 * @param jobGorupName
	 * @return
	 */
    public int addJob2(String jobName, Class<? extends Job> job, Object task, int seconds, String jobGorupName) {
        try {
            // 判斷任務是否存在
            JobKey jobKey = JobKey.jobKey(jobName, jobGorupName);
            if (scheduler.checkExists(jobKey)) {
                return 1;// 任務已經存在
            }
            // 建立一個JobDetail例項,指定SimpleJob
            Map<String, Object> JobDetailmap = new HashMap<String, Object>();
            JobDetailmap.put("name", jobName);// 設定任務名字
            JobDetailmap.put("group", jobGorupName);// 設定任務組
            JobDetailmap.put("jobClass", job.getCanonicalName());// 指定執行類
            // Task.class.getCanonicalName()
            JobDetail jobDetail = JobDetailSupport.newJobDetail(JobDetailmap);
            // 新增資料內容
            jobDetail.getJobDataMap().put("task", task);// 傳輸的上下文
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/3 * * * * ?");
            CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger_1", "tGroup1")
                    .withSchedule(cronScheduleBuilder).build();

            // 通過SchedulerFactory獲取一個排程器例項
            scheduler.scheduleJob(jobDetail, cronTrigger);//  註冊並進行排程
            scheduler.start();// ⑤排程啟動
            return 0;// 新增成功
        } catch (Exception e) {
            // e.printStackTrace();
            return 2;// 操作異常
        }
    }

	/**
	 * 關閉任務排程
	 *
	 * @param jobName
	 *            任務名稱
	 * @return 0 關閉成功 1: 關閉失敗 2:操作異常
	 */
	public int closeJob(String jobName, String jobGorupName) {
		// 關閉任務排程
		try {
			JobKey jobKey = JobKey.jobKey(jobName, jobGorupName);
			return scheduler.deleteJob(jobKey) == true ? 0 : 1;
		} catch (SchedulerException e) {
			// e.printStackTrace();
			return 2;
		}
	}

	/**
	 * 從資料庫中找到已經存在的job,並重新開戶排程
	 */
	public void resumeJob() {
		try {
			scheduler.start();
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

index.jsp: 

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

執行結果:

用CronScheduleBuilder後,資料庫中表中資料的資訊:

 

等等