quartz2.2.1使用jdbcjobstore模式集成到springmvc中,并随tomcat启动

使用quartz2.2.1,quartz有两种执行模式,一种是RAMJobStore,一种是JDBCJobStore。
1、RAMJobStore

RAMJobStore是默认的数据存储方式,其把数据存在本地内存中,官方宣称这是最有效率的方式(在本地内存当然快啦)。但是在宕机或者重启的时候数据就会丢失。
//配置方式
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

2、JDBCJobStore

JDBCJobStore终于把数据给持久化起来了,这个也是使用最广泛的数据存储方式。在于Spring集成后都不需要自己再配置一遍数据库连接了。建表脚本在官方包里面可以找到(http://www.quartz-scheduler.org/downloads/)
//配置方式
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.dataSource = myDS

之前采用的是第一种方式,就像上面说的,这种方式对内存依赖较多,并且服务器一旦怠机或者重启,所有的定时任务的数据就会丢失,定时任务无法再跑起来,所以现在尝试使用第二种方法使用quartz。

1、执行quartz数据库脚本
首先下载quartz的官方压缩包,里面包括源码、examples、javadoc一应俱全,http://www.quartz-scheduler.org/downloads/
下载之后找到quartz-2.2.1/docs/dbTables/tables_mysql.sql文件,去自己数据库中执行下,这是quartz任务和触发器的相关表。

2、添加quartz的properities
在resources目录下新建文件quartz.properities文件,该文件用来保存quartz执行的相关配置信息,具体内容如下:

# Default Properties file for use by StdSchedulerFactory  
# to create a Quartz Scheduler Instance, if a different  
# properties file is not explicitly specified.  
#  
  
org.quartz.scheduler.instanceName: DefaultQuartzScheduler  
org.quartz.scheduler.instanceId: AUTO
 
<!--线程池配置--> 
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool  
org.quartz.threadPool.threadCount: 10  
org.quartz.threadPool.threadPriority: 5  
org.quartz.jobStore.misfireThreshold: 60000  

<!--jobstore配置-->
org.quartz.jobStore.class: org.quartz.impl.jdbcjobstore.JobStoreTX  
org.quartz.jobStore.driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate  
org.quartz.useProperities: true

<!--数据库配置-->
org.quartz.jobStore.tablePrefix: qrtz_  
org.quartz.jobStore.dataSource: qzDS
  
org.quartz.dataSource.qzDS.connectionProvider.class: org.quartz.examples.example17.MyPoolingconnectionProvider  
org.quartz.dataSource.qzDS.driver: com.mysql.jdbc.Driver  
org.quartz.dataSource.qzDS.url: jdbc:mysql://localhost:3306/quartz?useUnicode=true&characterEncoding=utf-8  
org.quartz.dataSource.qzDS.user: root  
org.quartz.dataSource.qzDS.password: 123456
org.quartz.dataSource.qzDS.maxConnections: 30

3、配置spring-quartz.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd">

	<context:property-placeholder location="classpath:quartz.properities" ignore-unresolvable="true"/>
    <!-- 配置调度工厂 -->
    <bean id="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false">
	    <properity name="applicationContextSchedulerContextKey" value="applicationContext"/>
	    <properity name="configLocation" value="classpath:quartz.properties"/>
    </bean>
</beans>

4、编写Job类

import org.quartz.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class JobTest implements Job {
	@Override
	public void excute(JobExecutionContext context) throws JobExecutionException {
		JobKey jobKey = context.getJobDetail().getKey();
		JobDataMap data = context.getJobDetail.getJobDataMap();
		String color = data.getString("color");
		System.out.println("job <" + jobKey + "> start at:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " and color=" + color);
	}
}

5、编写quartz的测试类

import org.quartz.*;
import org.springframework.context.ApplicationContext;
import org.springframework.support.ClassPathXmlApplicationContext;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

public class QuartzTest {
	private static Scheduler scheduler;
	public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring/spring-quartz.xml");
		scheduler = (Scheduler) applicationContext.getBean("quartzScheduler");
		startSchedule();
	}
	public static void startSchedule() {
		JobDetail job = new Job(JobTest.class).withIdentity("job1", "group1").build();
		CronScheduleBuilder cron = CronScheduleBuilder.cronSchedule("0/2 * * * * ?");
		cron.withMisfireHandlingInstructionFireAndProceed();
		CronTrigger trigger = newTrigger().withIdentity("trigger1", "group1").withSchedule(cronScheduleBuilder).build();
		job.getJobDataMap.put("color", "Red");
		try {
			scheduler.deleteJob(job.getKey());
			scheduler.scheduleJob(job, trigger);
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值