`

spring之quartz定时器

    博客分类:
  • j2ee
阅读更多
public class ParseTask {
	
	private static Logger logger = Logger.getLogger(ParseTask.class);
	
	protected static boolean FLAG = true;
	
	public void taskStart() {
		if (FLAG) {
			FLAG = false;
			try {
				System.out.println("测试日志");
				System.out.println(new Date());
			} catch (Exception e) {
				logger.error(" Start ParseTask fail ,error : " + e.getMessage());
			}
			FLAG = true;
		}
	} 
	
}

 applicationContext-quartz.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 配置事务管理器 -->
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 配置解析日志任务开始 -->
	<bean id="parseTask" class="com.spider.reader.common.init.ParseTask" /> <!-- 指定启动类  -->
	<bean id="taskStartMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject">
			<ref local="parseTask" />
		</property>
		<property name="targetMethod"><!-- 指定启动类中的方法  -->
			<value>taskStart</value>
		</property>
		<property name="concurrent" value="false" />
	</bean>
	<bean id="taskStartTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail">
			<ref bean="taskStartMethod" />
		</property>
		<property name="cronExpression">
			<!-- 设定启动时间,05分08时*日*月?周,0 0/5 * * * ?每5分钟  -->
			<value>0 0/1 * * * ?</value>
		</property>
	</bean>
	<bean id="startTriggers" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
	    <property name="triggers">
	         <list>
	             <ref local="taskStartTime"/><!-- 设置触发器  -->
	         </list>
	     </property>
   </bean>
</beans>

 

运行结果:

每隔一分钟打印一次 

测试日志
Mon May 20 16:25:00 CST 2013
测试日志
Mon May 20 16:26:00 CST 2013

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics