Spring与Quartz的任务调度比较
任务调度代码比较
Spring 2.x 任务调度示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<bean id="demoJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 目标Bean --> <property name="targetObject"> <bean class="cc.gmem.demo.DemoService" /> </property> <!-- 目标方法 --> <property name="targetMethod" value="doStuff" /> <!-- 防止并发执行 --> <property name="concurrent" value="false" /> </bean> <!-- 简单触发器 --> <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail" ref="demoJob" /> <!-- 启动后,调度开始的时间 --> <property name="startDelay" value="0" /> <!-- 每隔2000ms调度一次 --> <property name="repeatInterval" value="2000" /> </bean> <!-- Cron触发器 --> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="demoJob" /> <property name="cronExpression" value="15 0/2 * * * ?" /> </bean> <!-- 调度工厂Bean --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="simpleTrigger" /> <ref bean="cronTrigger" /> </list> </property> </bean> |
Spring 3.x 任务调度示例
配置文件方式
1 2 3 4 5 6 7 |
<!-- 任务调度器配置,pool-size为线程池大小,限制了同时最多被调度的任务 --> <task:scheduler pool-size="100" id="scheduler" /> <!-- 任务计划列表,支持固定频率、固定延迟、Cron表达式等 --> <task:scheduled-tasks scheduler="scheduler"> <task:scheduled fixed-rate="120000" method="doStuff" ref="demoService" /> <task:scheduled cron="15 0/2 * * * ?" method="doStuff" ref="demoService" /> </task:scheduled-tasks> |
Quartz任务调度示例
特性比较
比较项 | Spring2.x | Spring3.x | Quartz |
优… |
13 years ago
0