易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
软件资源
软件开发
、
小程序制作
、
系统集成与运维
、
空间租用
、
硬件开发
、
视频监控
、
技术咨询与支持
——联系电话:0311-88999002/88999003
首页
/
操作系统
/
Linux
/
Spring整合Quartz
1.定时执行的类
package
thb.quartz;
public
class
QuartzJob {
/**
* 定时执行的方法
*/
public
void
work() {
System.out.println(System.currentTimeMillis() + ">>>执行定时任务。。。");
}
}
2.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd"
default
-lazy-init="true">
<!-- 要调用执行定时任务的类 -->
<bean id="quartzJob"
class
="thb.quartz.QuartzJob"></bean>
<!-- 定义调用对象和调用对象的方法(触发器) -->
<bean id="jobTask"
class
="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="quartzJob"/>
</property>
<!-- 调用的方法 -->
<property name="targetMethod">
<value>work</value>
</property>
</bean>
<!-- 定义触发事件(调度器) -->
<bean id="doTime"
class
="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="jobTask"></property>
<!-- 10点开始,每隔1分钟 -->
<property name="cronExpression" value="0 * 10 * * ?"></property>
</bean>
<bean id="startQuartz" lazy-init="false" autowire="no"
class
="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="doTime"/>
</list>
</property>
</bean>
</beans>
3.测试代码
package
thb.quartz;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.FileSystemXmlApplicationContext;
public
class
QuartzTest {
/**
* 定时任务测试
*/
public
static
void
main(String[] args) {
ApplicationContext ctx =
new
FileSystemXmlApplicationContext("/resource/spring/quartz.xml");
System.out.println("定时任务开始执行。。。");
}
}
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图