Quartz官方网站对SimpleTrigger和CronTrigger的简单对比:
SimpleTrigger is handy if you need ‘one-shot‘ execution (just single execution of a job at a given moment in time), or if you need to fire a job at a given time, and have it repeat N times, with a delay of T between executions.当你需要的是一次性的调度(仅是安排单独的任务在指定的时间及时执行),或者你需要在指定的时间激活某个任务并执行N次,设置每次任务执行的间隔时间T。那此时使用SimpleTrigger将是非常方便的。
CronTrigger is useful if you wish to have triggering based on calendar-like schedules - such as "every Friday, at noon" or "at 10:15 on the 10th day of every month."如果你需要安排的任务时基于日期的-比如"每个星期五正午"或者"每个月10号的10:15",使用CronTrigger将是非常有用的。
1、配置applicationcontext.xml
<!-- cronTrigger实现方式 --> <bean name="exampleJob" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass" value="com.ouku.entities.report.ReportTimerTask" /> <property name="jobDataAsMap"> <map> <entry key="timeout" value="3600" /> </map> </property> </bean> <bean id="springUtil" class="com.ouku.util.SpringUtil" /> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="exampleJob" /> <!-- run every morning at 6 AM --> <!-- <property name="cronExpression" value="0 0 6 * * ?" /> --> <!-- <property name="cronExpression" value="0 0/1 * * * ?" /> --><!-- 每分钟 --> <property name="cronExpression" value="0/5 * * * * ?" /> <!-- 每秒 --> </bean> <bean id="exampleBusinessObject" class="com.ouku.entities.report.ReportTimerTaskTwo" /> <bean id="jobDetail" <!-- simpleTrigger实现方式 --> class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="exampleBusinessObject" /> <property name="targetMethod" value="doIt" /> <property name="concurrent" value="false" /> </bean> <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"> <!-- see the example of method invoking job above --> <property name="jobDetail" ref="jobDetail" /> <!-- 0 seconds --> <property name="startDelay" value="0" /> <!-- repeat every 5 seconds --> <property name="repeatInterval" value="5000" /> </bean> <!-- 总调度用于启动Spring定时器 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronTrigger" /> <ref bean="simpleTrigger"/> </list> </property> </bean>
2.利用cronTrigger的Java实现
package com.ouku.entities.report; import java.util.Date; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; import com.ouku.report.ReportTask; public class ReportTimerTask extends QuartzJobBean { private int timeout; public void setTimeout(int timeout) { this.timeout = timeout; } @Override protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException { try { Date date = new Date(); String dd = " " + date.getMinutes() + ":" + date.getSeconds() + " "; System.out.println("AAA" + dd); //to do } catch (Exception e) { e.printStackTrace(); } } }
3.利用simpleTrigger的Java实现
package com.ouku.entities.report; import java.util.Date; import org.quartz.JobExecutionException; import com.ouku.report.ReportTask; public class ReportTimerTaskTwo { public void doIt() throws JobExecutionException { try { Date date = new Date(); String dd = " " + date.getMinutes() + ":" + date.getSeconds() + " "; System.out.println("sss1" + dd); //to do .. } catch (Exception e) { e.printStackTrace(); } } }
默认情况下,当Job执行时间超过间隔时间时,调度框架为了能让任务按照我们预定的时间间隔执行,会马上启用新的线程执行任务。
时间: 2024-10-05 04:09:34