spring定时任务之quartz基于xml开发

来源:互联网 发布:社会网络分析软件 编辑:程序博客网 时间:2024/05/17 03:07
定时器配置完成抛出异常
1.SchedulerException: Jobs added with no trigger must be durable 
需要在在jobDeatailsBean 中加一句<property name="durability" value="true" />
 2.DateSource name is not set  说明quartz的配置文件中没有配置链接数据库的信息
1 编写业务类,该类继承了org.quartz.Job,主要的逻辑在execute方法中编写
2 配置spring的applicationContext.xml文件
    2.1 配置任务JobDetailBean
    2.2配置触发器 CronTriggerBean
    2.3配置调度器  SchedulerFactoryBean

3 所需要的jar包:
         spring.jar,quartz.jar,commons-logging-1.0.4.jar,commons-dbcp-1.2.2.jar,commons-pool-1.3.jar
4 把quartz.properties放到类路径下

配置文件代码 写入spring.xml中
<context:component-scan base-package="com.fac.timer" />
 <bean id="toPtAccountTime" class="com.fac.timer.ToPtAccountTime" /> 
    <bean name="reportTask" 
        class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> 
        <property name="jobClass" value="com.fac.timer.ToPtAccountTime" /> 
        <property name="durability" value="true" />
    </bean> 
    <!-- 触发器 --> 
    <bean id="cronTrigger" 
        class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> 
        <!-- 指向我们的任务 --> 
        <property name="jobDetail" ref="reportTask" /> 
        <!--  每天下午16点10分到55分,每分钟运行一次 --> 
        <property name="cronExpression" value="0 10-55 16 * * ?" /> 
    </bean> 
    <!-- 调度器  --> 
    <bean 
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 
        <property name="triggers"> 
            <list> 
                <!--  触发器列表 --> 
                <ref bean="cronTrigger" /> 
            </list> 
        </property> 
        <property name="configLocation" value="classpath:config/quartz.properties" />  
    </bean>  
  
配置文件的内容、
#============================================================================
# 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 = 10
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\://192.168.1.254\:3306/p2pv1?useUnicode\=true&characterEncoding\=utf8
org.quartz.dataSource.myDS.user = admin
org.quartz.dataSource.myDS.password = P@ssword\!
org.quartz.dataSource.myDS.maxConnections = 10 
0 0