spring的任务调度spring+quartz和spring+jdk的Time的结合的讲解
//这是spring+quartz的整合讲解
<?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:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!--这是要执行任务的方法 -->
<bean id="taskExample" class="cn.itcast.tasks.TaskExample"></bean>
<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="taskExample"></property><!--这是要执行的对象 -->
<property name="targetMethod" value="display"></property><!--这是要执行对象的某个方法 -->
<property name="concurrent" value="false"></property><!-- 这是保证下面的两个触发器不并发运行,因为在默认情况下是默认并发运行的 -->
</bean>
<!-- 要执行的时间,这是simpleTrigger类型的触发器 -->
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="jobDetail"></property><!-- 这是要对哪个任务的执行进行时间的限制 -->
<property name="startDelay" value="10000"></property><!--这是要启动spring容器后延迟十秒就执行 -->
<property name="repeatInterval" value="1000"></property><!-- 这是每隔1秒就重复执行 -->
</bean>
<!-- 这是cronTrigger类型的触发器 -->
<!-- 要执行时间
秒 分 时 日 月 星期 年
*和? 代表任意时间
- 区间
/ 每隔多久触发
-->
<bean id="conTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="jobDetail"></property>
<property name="cronExpression" value="1/3 * * * * ?"></property>
</bean>
<!-- 这是开启计划 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTrigger"/>
<ref bean="conTrigger"/>
</list>
</property>
</bean>
</beans>
//这是spring+jdk的timer的整合
<?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:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!--这是要执行任务的方法 -->
<bean id="taskExample" class="cn.itcast.tasks.TaskExample"></bean>
<bean id="timerTask" class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
<property name="targetObject" ref="taskExample"></property><!-- 这是要执行的对象 -->
<property name="targetMethod" value="display"></property><!-- 这是要执行的对象的方法 -->
</bean>
<!--执行的时间 -->
<bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="delay" value="1000"></property><!--这是在spring容器实例化后的1秒后执行 -->
<property name="period" value="1000"></property><!--这是每隔1秒后执行 -->
<property name="timerTask" ref="timerTask"></property><!--这是要对哪个任务进行再执行 -->
</bean>
<!--开启计划 -->
<bean class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref bean="scheduledTask"/><!--这是要执行的任务的时间规划加载进来 -->
</list>
</property>
</bean>
</beans>
//这样用spring定义定时器的类
package cn.itcast.tasks;
public class TaskExample {
public void display()
{
System.out.println("this is really love!!!");
}
}
//这是测试
package cn.itcast.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
public class TestTaskExample {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
}