spring boot 整合quartz并进行jdbc持久化

引言

quartz是什么在这里我就不接少了,想要了解的可以查看我的这篇介绍quartz的文章
http://blog.csdn.net/wujiaqi0921/article/details/78632760

在这里我们来接受一些spring与quartz的整合。

1.引入maven引用

        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz-jobs</artifactId>
            <version>2.2.3</version>
        </dependency>

2.quartz配置类

spring boot建议去除配置文件的形式而改为bean暴露的形式,在这里我们我们以bean暴露的形式为例,当然你也可以选择配置文件的形式。
首先创建QuartzConfig.Java配置类。添加Configuration注解。具体代码如下:

package com.god.config;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.ee.servlet.QuartzInitializerListener;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.Properties;

/**
 * Created by wujiaqi on 17/11/6.
 */
@Configuration
public class QuartzConfig {
   
    /**
     * 设置调度工厂,并返回调度管理器
     * 交由spring管理,使用时由autowire注入
     * @return
     * @throws IOException
     * @throws SchedulerException
     */
    @Bean
    public Scheduler schedule() throws  IOException, SchedulerException {
        SchedulerFactory schedulerFactory = new StdSchedulerFactory(quartzProperties());
        Scheduler scheduler = schedulerFactory.getScheduler();
        scheduler.start();
        return scheduler;
    }


    /**
     * 设置quartz属性
     *
     * @throws IOException
     */
    public static Properties quartzProperties() throws IOException {
        Properties prop = new Properties();
        prop.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");//实例化threadPool时,使用的线程类为SimpleThreadPool
        //threadCount和threadPriority将setter的形式注入ThreadPoolS实例
        prop.put("org.quartz.threadPool.threadCount", "5");//并发数
        prop.put("org.quartz.threadPool.threadPriority", "5");//优先级
        prop.put("org.quartz.scheduler.threadsInheritContextClassLoaderOfInitializer", true);
        //prop.put("org.quartz.jobStore.class", "org.quartz.simpl.RAMJobStore");//默认存储在内存中
        prop.put("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");//数据库持久化
        prop.put("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.StdJDBCDelegate");
//        prop.put("org.quartz.jobStore.isClustered", false);//是否开启集群
//        prop.put("org.quartz.jobStore.clusterCheckinInterval", 20000);//集群检查
        prop.put("org.quartz.jobStore.tablePrefix", "QRTZ_");//表前缀:true
        prop.put("org.quartz.jobStore.dataSource", "mytest");//数据库
        prop.put("org.quartz.dataSource.mytest.driver", "com.mysql.jdbc.Driver");
        prop.put("org.quartz.dataSource.mytest.URL", "jdbc:mysql://localhost:3306/mytest?characterEncoding=UTF-8");
        prop.put("org.quartz.dataSource.mytest.user", "root");
        prop.put("org.quartz.dataSource.mytest.password", "root");
        prop.put("org.quartz.dataSource.mytest.maxConnections", "10");
        return prop;
    }

    /**
     * 监听到工程的启动,在工程停止再启动时可以让已有的定时任务继续进行
     *
     * @return
     */
    @Bean
    public QuartzInitializerListener quartzInitializerListener() {
        return new QuartzInitializerListener();
    }
}

3.使用

我们在使用时,只需要交给spring注入就行了

    @
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值