Spring任务调度

简单配置spring任务调度:

1、配置Job有两种形式分别为JobDetailBean和MethodInvokingJobDetailFactoryBean。

1.1 JobDetailBean 代码配置:

<bean id="rewardJobDetail"
		class="org.springframework.scheduling.quartz.JobDetailBean">
		<property name="jobClass" value="com.web.manage.SpringRewardTask" />
		<property name="jobDataAsMap">
			<map>
				<entry key="psmExeRewardService">
					<ref bean="psmExeRewardService"/>
				</entry>
				<entry key="moneyServiceImpl">
					<ref bean="moneyServiceImpl"/>
				</entry>
				<entry key="orderService">
					<ref bean="orderService"/>
				</entry>
				<entry key="psmRewardInfoService">
					<ref bean="psmRewardInfoService"/>
				</entry>
			</map>
		</property>	
</bean>

 1.2  MethodInvokingJobDetailFactoryBean

   

<bean id="jobClass" class="com.web.manage.UpopSendQueryTask">
		<property name="psmUpopInfoService">
		    <ref bean="psmUpopInfoService" />
		</property>
		<property name="psmYlUpopService">
		    <ref bean="psmYlUpopService" />
		</property>
</bean>

 

<bean id="upopJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject">
			<ref local="jobClass" />
		</property>	
		<!--定义为单线程-->
		<property name="concurrent" value="false" />
     <!-- 为了使得jobs不并发运行concurrent=false 默认情况下,jobs在并行的方式下运行 -->
		<property name="targetMethod" value="execute" />
</bean>

 2、配置Trigger,Spring提供两种Trigger配置分别为:SimpleTriggerBean和CornTriggerBean

 2.1 SimpleTriggerBean配置:

  

<bean id="doUpopJobTrigger"
		class="org.springframework.scheduling.quartz.SimpleTriggerBean">
		<property name="jobDetail">
			<ref bean="upopJobDetail" />
		</property>
		<property name="startDelay">
			<value>10000</value>
		</property>
		<property name="repeatInterval">
			<value>1800000</value>
		</property>
</bean>

 2.2 CornTriggerBean配置

 

<bean id="doRewardJobTrigger"
		class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail">
			<ref bean="rewardJobDetail" />
		</property>
		<property name="cronExpression">
			<value>0 0 3 * * ?</value>
		</property>
</bean>

 3:配置了Job和Trigger还需要配置SchedulerFactoryBean才能集成以上配置实用

 

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="doRewardJobTrigger" />
				<ref bean="doUpopJobTrigger" />
			</list>
		</property>
		<property name="autoStartup" value="true"/>
		<property name="schedulerName" value="cronScheduler" />   
</bean>

 总结:以上三步配置完毕后就可以实用Spring提供的任务调度了。

你可能感兴趣的:(spring)