spring-quartz调度

quartz 是spring集成的调度框架。可以定时执行任务。

  1. quartz.properties配置

    
    # Configure Main Scheduler Properties  
    
    
    #============================================================================
    
    org.quartz.scheduler.instanceName = DefaultQuartzScheduler
    org.quartz.scheduler.instanceId = AUTO
    org.quartz.scheduler.rmi.export = false
    org.quartz.scheduler.rmi.proxy = false
    org.quartz.scheduler.wrapJobExecutionInUserTransaction = false
    
    
    # Configure ThreadPool  
    
    
    #============================================================================
    
    
    #org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
    
    org.quartz.threadPool.threadCount = 500
    org.quartz.threadPool.threadPriority = 5
    
    #org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
    
    
    
    # Configure JobStore  
    
    
    #============================================================================
    
    
    #org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
    
    
    #org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.oracle.OracleDelegate
    
    org.quartz.jobStore.misfireThreshold = 60000
    
    #org.quartz.jobStore.useProperties = false
    
    
    #org.quartz.jobStore.tablePrefix = QRTZ_
    
    
    #org.quartz.jobStore.dataSource = myDS
    
    
    
    #org.quartz.jobStore.isClustered = true
    
    
    #org.quartz.jobStore.clusterCheckinInterval = 15000
    
    
    
    # Configure DataSource
    
    
    #============================================================================
    
    
    #org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver
    
    
    #org.quartz.dataSource.myDS.URL =jdbc:mysql://121.199.19.82:3306/ttcity_quarz?useUnicode=true&characterEncoding=utf8&mysqlEncoding=utf8
    
    
    #org.quartz.dataSource.myDS.user =ttcity
    
    
    #org.quartz.dataSource.myDS.password=lianchuang66
    
    
    #org.quartz.dataSource.myDS.maxConnections=1000
    
  2. spring-quartz.xml配置

    
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
        xmlns="http://www.springframework.org/schema/beans">
    
        <!-- 要调用的工作类 -->
        <bean id="quartzJob" class="com.ttcity.portal.common.QuarzInstance"></bean>
        <!-- 定义调用对象和调用对象的方法 -->
        <bean id="jobtask"
            class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <!-- 调用的类 -->
            <property name="targetObject">
                <ref bean="quartzJob" />
            </property>
            <!-- 调用类中的方法-->
            <property name="targetMethod">
                <value>work</value>
            </property> 
        </bean>
        <!-- 定义触发时间 -->
        <bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
            <property name="jobDetail">
                <ref bean="jobtask" />
            </property>
            <!-- cron表达式 -->
            <property name="cronExpression">
                <value>0 50-59 21 * * ?</value>
            </property>
        </bean>
        <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
        <bean id="startQuertz" lazy-init="false" autowire="no"
            class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
                    <ref bean="doTime" />
                </list>
            </property>
            <property name="configLocation" value="classpath:quartz.properties" /> 
        </bean>
    </beans>
  3. QuarzInstance.java

    package com.ttcity.portal.common;
    
    import java.util.Date;
    
    /**
     * 写一些调度任务的
     * @author cjr
     *
     */
    public class QuarzInstance {
        public void work(){
            System.out.println("现在的时间是:"+new Date());
        }
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值