`

spring配置quartz 多任务

阅读更多
这是我的一个测试,我配的2个任务。
spring 的配置:

<!-- 与quartz有关的设置 -->
	<bean id="billJob" class="test.BillReliabilityJob"/>
	<bean id="exceptionJob" class="test.ExceptionJob"/>
	
	<bean id="billJobTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject">
			<ref bean="billJob"/><!-- 调用的Job类 -->
		</property>
		<property name="targetMethod">
			<value>run</value><!-- 调用的类中的方法 -->
		</property>
	</bean>
	<bean id="exceptionJobTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject">
			<ref bean="exceptionJob"/><!-- 调用的Job类 -->
		</property>
		<property name="targetMethod">
			<value>run</value><!-- 调用的类中的方法 -->
		</property>
	</bean>
	
	<bean id="billRunTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail">
			<ref bean="billJobTask"/>
		</property>
		<property name="cronExpression">
			<value>0/5 * * * * ?</value>
		</property>
	</bean>
	<bean id="ExceptionRunTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail">
			<ref bean="exceptionJobTask"/>
		</property>
		<property name="cronExpression">
			<value>10 * * * * ?</value>
		</property>
	</bean>
	
	<!-- 总管理类,如果lazy-init='false',则容器启动时就会执行调度程序-->
	<!-- 如果lazy-init='true',则需要实例化该bean才能执行调度程序            -->
	
	<bean id="billStartQuartz" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="billRunTime"/>
				<ref bean="ExceptionRunTime"/>
			</list>
		</property>
	</bean>


运行时,首先我采用MainTest 测试

public class MainTest extends TestCase {

	public void testJob(){
		System.out.println("start");
		ApplicationContext context = new FileSystemXmlApplicationContext(
		"./config/spring.xml");
		System.out.println("end");
	}
}


发现程序打印end后就结束了。
然后我用Main测试,可以正常执行。

Java代码
public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("start");
		ApplicationContext context = new FileSystemXmlApplicationContext(
		"./config/spring.xml");
		System.out.println("end");
	}

}

job类的内容为:

Java代码
public class ExceptionJob {

	public void run(){
		System.out.println("ExceptionJob running");
	}
}

分享到:
评论
1 楼 Wentasy 2012-08-01  
好文章,多谢分享。

相关推荐

Global site tag (gtag.js) - Google Analytics