quartz 是spring集成的调度框架。可以定时执行任务。
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
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>
QuarzInstance.java
package com.ttcity.portal.common; import java.util.Date; /** * 写一些调度任务的 * @author cjr * */ public class QuarzInstance { public void work(){ System.out.println("现在的时间是:"+new Date()); } }