Spring中集成任务调度组件Quartz

       不多说了,直接贴配置文件说明吧。

一、继承QuartzJobBean的配置

<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
        "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>	
	<bean name="warningTask" class="org.springframework.scheduling.quartz.JobDetailBean">
    	<property name="jobClass">
    		<value>com.xxx.xxx.TimedTask</value><!-- 任务类其继承QuartzJobBean-->
    	</property> 
    	<property name="jobDataAsMap">
    		<map>
    			<entry key="earlyWarningMainThread">
    				<ref bean="earlyWarningMainThread" />  				
    			</entry>
    			<entry key="superviseRecordDomain">
    				<ref bean="superviseRecordDomain" />  <!-- 需要spring进行注入的服务-->				
    			</entry>
    		</map>
    	</property>	
    </bean>
	<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    	<property name="jobDetail">
    		<ref bean="warningTask"/>
    	</property>
    	<property name="cronExpression">
    		<value>${task.time}</value><!-- 从外部注入的定时时间,具体可以查阅时间格式定义-->
    	</property>
    </bean>
    
    <bean id="quartzFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
	    <property name="triggers">
	        <list>
	            <ref local="cronTrigger" />
	        </list>
	    </property>
	</bean>
</beans>

 其中TimedTask代码如下

  

public class TimedTask extends QuartzJobBean{

	
	protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
        //定时方法
        //do something

    }
}

 

二、指定执行特定方法的配置

 

  

<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
        "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>	
	<bean id="sampledDataTask" class="com.xxx.SampledDataTask"/>
	<bean id="sampledDataJobDetail"

		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

    	<property name="targetObject"><ref bean="sampledDataTask"/></property><!--指定那个类 -->

    	<property name="targetMethod"><value>task</value></property><!--指定类中的那个方法,为方法名-->
    </bean>

               <!--定时执行的配置-->
	<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    	<property name="jobDetail">
    		<ref bean="sampledDataJobDetail"/>
    	</property>
    	<property name="cronExpression">
    		<value>${task.time}</value>
    	</property>
    </bean>
    
    <!-- 按一定频率执行的配置 -->
	<bean id="importDataJobDetail"

		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

    	<property name="targetObject"><ref bean="xmlDataImpDaemon"/></property>

    	<property name="targetMethod"><value>monitor</value></property>
    </bean>
	<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
    	<property name="jobDetail">
    		<ref bean="importDataJobDetail"/>
    	</property>
    	<property name="startDelay">
    		<value>10000</value>
    	</property>
    	<property name="repeatInterval">
    		<value>${dataSwitch.repeat}</value>
    	</property>
    </bean>

	<bean id="quartzFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
	    <property name="triggers">
	        <list>
	        	<ref local="simpleTrigger"/>
	            <ref local="cronTrigger" />
	        </list>
	    </property>
	</bean>
</beans>

 PS:个人偏向第二种的用法,第一种与quartz的耦合性太强,并且在第一种的方式里面,必须配置指定注入那些服务,不能像其他那样只声明就可以利用Spring的IOC

你可能感兴趣的:(spring,xml,bean,quartz,IOC)