spring配置文件servicegateway-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"
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"
default-autowire="byName">
<!-- 每个定义的任务都要在这里进行引用才能运行 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" autowire="no">
<property name="triggers">
<list>
<ref local="BusinessTestTrigger" />
</list>
</property>
</bean>
<!-- 定时器触发器,配置定时器、以及触发时机 -->
<bean id="BusinessTestTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="BusinessTestDetail" />
</property>
<property name="cronExpression">
<value>0 40 15 ? * *</value>
</property>
</bean>
<!-- 引用,配置要运行的方法 -->
<bean id="BusinessTestDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="BusinessTestTime" />
</property>
<property name="concurrent" value="false" />
<property name="targetMethod" value="run" />
</bean>
<!-- 定义我们要运行的类,可以使用注入定制一些参数 -->
<bean id="BusinessTestTime" class="com.xxx.eb.tools.quartz.QuartzTest">
<property name="para" value="Spring定时器测试V1" />
</bean>
</beans>
测试类QuartzTest.java:
package com.xxx.eb.tools.quartz;
import java.text.SimpleDateFormat;
import java.util.Date;
public class QuartzTest{
// 执行参数
private String para ;
// 执行方法
public void run() {
// 定义输出的格式化日期,以便于区分调用
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("the para is : " + para + " ! Time is :" + format.format(new Date()));
}
public String getPara() {
return para;
}
public void setPara(String para) {
this.para = para;
}
}
<?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"
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"
default-autowire="byName">
<!-- 每个定义的任务都要在这里进行引用才能运行 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" autowire="no">
<property name="triggers">
<list>
<ref local="BusinessTestTrigger" />
</list>
</property>
</bean>
<!-- 定时器触发器,配置定时器、以及触发时机 -->
<bean id="BusinessTestTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="BusinessTestDetail" />
</property>
<property name="cronExpression">
<value>0 40 15 ? * *</value>
</property>
</bean>
<!-- 引用,配置要运行的方法 -->
<bean id="BusinessTestDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="BusinessTestTime" />
</property>
<property name="concurrent" value="false" />
<property name="targetMethod" value="run" />
</bean>
<!-- 定义我们要运行的类,可以使用注入定制一些参数 -->
<bean id="BusinessTestTime" class="com.xxx.eb.tools.quartz.QuartzTest">
<property name="para" value="Spring定时器测试V1" />
</bean>
</beans>
测试类QuartzTest.java:
package com.xxx.eb.tools.quartz;
import java.text.SimpleDateFormat;
import java.util.Date;
public class QuartzTest{
// 执行参数
private String para ;
// 执行方法
public void run() {
// 定义输出的格式化日期,以便于区分调用
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("the para is : " + para + " ! Time is :" + format.format(new Date()));
}
public String getPara() {
return para;
}
public void setPara(String para) {
this.para = para;
}
}
Spring Quartz定时任务配置与实现
本文介绍了一个基于Spring框架的Quartz定时任务的具体配置方法及其实现过程。通过spring配置文件servicegateway-quartz.xml,定义了定时任务的触发器、触发时机以及要执行的方法,并演示了一个简单的测试类QuartzTest的实现。
530

被折叠的 条评论
为什么被折叠?



