美文网首页
Spring整合quartz用法

Spring整合quartz用法

作者: sunpy | 来源:发表于2018-10-27 20:09 被阅读24次

    题外话

    之前已经阅读过quartz的官方文档了,像quartz的Job、JobDetail、Scheduler、Trigger的概念将不在说明了,就是单纯的使用spring整合quartz。

    引入jar包

    <!-- quartz组件 -->
    <dependency>
         <groupId>org.quartz-scheduler</groupId>
         <artifactId>quartz</artifactId>
         <version>2.2.1</version>
    </dependency>
    

    配置工程

    1. properties配置文件
      ① sys.properties
    jdbc.driverClassName=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://IP:端口/sunpy-quartz?characterEncoding=utf-8
    jdbc.username=用户名
    jdbc.password=密码
    jdbc.initialSize=1
    jdbc.maxActive=30
    jdbc.minIdle=1
    

    ② quartz.properties

    org.quartz.scheduler.instanceName=myQuartzScheduler
    org.quartz.scheduler.instanceId=AUTO
    #quartz-scheduler出口本身通过RMI作为服务器,然后设置“出口”标志true(默认值为false)
    org.quartz.scheduler.rmi.export = false
    org.quartz.scheduler.rmi.proxy = false
    
    org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
    org.quartz.threadPool.threadCount=10
    org.quartz.threadPool.threadPriority=5
    
    #配置JobStore
    org.quartz.jobSotre.useProperties=false
    org.quartz.jobStore.tablePrefix=QRTZ_
    #信息保存时间 默认值60秒
    org.quartz.jobStore.misfireThreshold=60000
    
    #开启集群
    #org.quartz.jobStore.isClustered=true
    #org.quartz.jobStore.clusterCheckinInterval=5000
    #org.quartz.jobStore.txIsolationLevelReadCommitted=true
    
    org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
    org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
    
    1. 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:beans="http://www.springframework.org/schema/beans"  
        xmlns:context="http://www.springframework.org/schema/context"  
        xmlns:aop="http://www.springframework.org/schema/aop"  
        xmlns:mvc="http://www.springframework.org/schema/mvc"  
        xmlns:p="http://www.springframework.org/schema/p"  
        xmlns:task="http://www.springframework.org/schema/task" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/aop  
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/mvc  
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task-4.0.xsd">  
        
        <context:component-scan base-package="cn.sunpy.*"></context:component-scan>
        <context:annotation-config></context:annotation-config>
        <context:property-placeholder location="classpath:sys.properties"/>
        
        <beans:bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <beans:property name="driverClassName" value="${jdbc.driverClassName}"></beans:property>
            <beans:property name="url" value="${jdbc.url}"></beans:property>
            <beans:property name="username" value="${jdbc.username}"></beans:property>
            <beans:property name="password" value="${jdbc.password}"></beans:property>
            <beans:property name="initialSize" value="${jdbc.initialSize}"></beans:property>
            <beans:property name="minIdle" value="${jdbc.minIdle}"></beans:property>
            <beans:property name="maxActive" value="${jdbc.maxActive}"></beans:property>
        </beans:bean>
        
        <!-- 配置JobDetail -->
        <beans:bean id="myJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
            <beans:property name="jobClass" value="cn.sunpy.quartz.MyJob"></beans:property>
        </beans:bean>
        
        <!-- 配置Cron触发器 -->
        <beans:bean id="myCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
            <beans:property name="jobDetail" ref="myJobDetail"></beans:property>
            <beans:property name="cronExpression" value="0/3 * * * * ?"></beans:property>   
        </beans:bean>
            
        <!-- 配置调度者 -->
        <beans:bean id="myScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <beans:property name="configLocation" value="classpath:quartz.properties"></beans:property>
            <beans:property name="dataSource" ref="dataSource"></beans:property>
            
            <beans:property name="triggers">
                <beans:list>
                    <beans:ref bean="myCronTrigger"/>
                </beans:list>
            </beans:property>
            
            <beans:property name="quartzProperties">
                <beans:props>
                    <beans:prop key="org.quartz.scheduler.skipUpdateCheck">true</beans:prop>
                </beans:props>
            </beans:property>
        </beans:bean>
    </beans>
    
    1. 任务
    public class MyJob implements Job{
    
        @Override
        public void execute(JobExecutionContext context)
                throws JobExecutionException {
            System.out.println("MyJob execute");
        }
    }
    
    结果.jpg

    相关文章

      网友评论

          本文标题:Spring整合quartz用法

          本文链接:https://www.haomeiwen.com/subject/omrdtqtx.html