Spring中定时执行任务的两种方法配置
<!-- 定义要执行的任务:执行 managerService 中的 sendSMS方法-->
<bean id="cronJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<!-- Service -->
<ref bean="managerService" />
</property>
<property name="targetMethod">
<!-- Regular execute of the method -->
<value>sendSMS</value>
</property>
</bean>
<!-- 任务执行方式 -->
<!-- 1、指定时间 每天凌晨1点 01:00 执行一次 -->
<bean id="cronTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="cronJob" />
</property>
<property name="cronExpression">
<value>0 0 1 * * ?</value>
</property>
</bean>
<!-- 2、指定频率 延迟3秒开始触发、每分钟执行一次 -->
<bean id="simpleTrigger"
class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail">
<ref bean="cronJob" />
</property>
<property name="startDelay">
<!-- 单位毫秒 -->
<value>3000</value>
</property>
<property name="repeatInterval">
<!-- 单位毫秒 -->
<value>60000</value>
</property>
</bean>
<bean
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!--
<ref local="cronTrigger"/>
-->
<ref local="simpleTrigger" />
</list>
</property>
<property name="quartzProperties">
<props>
<prop key="org.quartz.threadPool.threadCount">1</prop>
</props>
</property>
</bean>