spring and quartz demo

漆雕奇逸
2023-12-01

一、依赖的包

<dependency>
				<groupId>org.quartz-scheduler</groupId>
				<artifactId>quartz</artifactId>
				<version>2.1.1</version>
			</dependency> 
			
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context-support</artifactId>
				<version>${org.springframework.version}</version>
			</dependency>


二、spring配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
	default-lazy-init="true">

	<!-- 任务启动地方 -->
	<bean id="startQuartz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="myJobTrigger"/>
			</list>
		</property>
	</bean>
	
	<!-- 定义任务规则 -->
	<bean id="myJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<property name="jobDetail" ref="myJobDetail"/>
		<property name="cronExpression" value="0/5 * * * * ?"/><!-- 没5秒执行一次 -->
	</bean>
	
	<!-- 代理目标和目标方法 -->
	<bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="myJbo"/>
		<property name="targetMethod" value="work"/>
		<property name="concurrent" value="false"/><!-- 一个任务结束前,不开始下一个任务 -->
	</bean>
	
	<!-- 我的任务调度器 -->
	<bean id="myJbo" class="com.csair.quarts.MyJob"></bean>
	
</beans>


三、任务类

package com.csair.quarts;

public class MyJob {

	public void work() {
		System.out.println("working.......");
	}
}


 

四、测试

public class TestMyJob {

	public static void main(String[] args) {
		String path = "applicationContext-quarts.xml";
		ClassPathXmlApplicationContext content = new ClassPathXmlApplicationContext(path);
	}
}

关于cronExpression的写法,这里有个链接:http://www.blogjava.net/javainthink/archive/2006/10/19/76077.html

 类似资料:

相关阅读

相关文章

相关问答