spring+quartz

1、引入jar包


注意:這裏面需要引入spring-tx  我沒有引入 啓動的時候總是報錯創建不了scheduler對象,後來看到源代碼 quartz裏面有相關的事務,所以需要引入(具體該沒研究透徹)

2、寫一個要執行的Job類

public class Job {
	public void excute(){
		System.out.println("當前執行的時間是:"+new Date());
	}
}

3、application.xml文件

	<bean id="ExampleJob" class="com.example.quartz.Job"></bean>
	<bean id="JobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<!-- 執行任務的類 -->
		<property name="targetObject" ref="ExampleJob"></property>
		<!-- 執行任務的方法 -->
		<property name="targetMethod" value="excute"></property>
	</bean>
	<bean id="JobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<property name="jobDetail" ref="JobDetail"></property>
		<property name="cronExpression" value="0/10 * * * * ?"></property>
	</bean>
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers" ref="JobTrigger"></property>
	</bean>

4、在web.xml中開啓監聽器

<!-- needed for ContextLoaderListener -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
	</context-param>

	<!-- Bootstraps the root web application context before servlet initialization -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章