Spring任务调度案例Demo,以及一些问题的解决

这段时间,公司项目要添加一个功能,每五分钟从数据库中拉一次数据,然后提取数据,把需要的数据推送到用户的手机端。

开始,我想到,使用定时线程的方法去五分钟拉一次数据,但是我上网找了一下有没有比较好的办法解决问题,网上给出的答案是: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" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">


	<!-- =========================调度================================= -->

	<bean id="myQuartz" class="com.hzys.testweb.quartz.MyQuartzJob"></bean>

	<!-- ====开始定义调度job== -->
	<bean id="newJob"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<!-- 注入Service层 -->
		<property name="targetObject" ref="myQuartz"></property>
		<!-- 调用的方法 -->
		<property name="targetMethod" value="hello"></property>
	</bean>

	<!-- ======定义一个触发器====== -->
	<bean id="simplerTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
		<!-- 指定要调度的任务job -->
		<property name="jobDetail" ref="newJob"></property>
		<!-- 设置延迟工作的第一次执行,单位毫秒 -->
		<property name="startDelay" value="30000"></property>
		<!-- 设置调度任务频度,单位毫秒 -->
		<property name="repeatInterval" value="30000"></property>
		<!-- 指定调度次数,不指定默认无限次 -->
		<property name="repeatCount" value="3"></property>
		<!-- 指定时间点启动调度,默认立即 -->
<!-- 		<property name="startTime" value=""></property> -->
	</bean>

	<!-- ======定义一个触发器===== -->
	<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<!-- 注入需要触发的job -->
		<property name="jobDetail" ref="newJob"></property>
		<!-- 每5秒执行一次 -->
		<property name="cronExpression" value="5/5 * * * * ? *"></property>
	</bean>


	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<!-- 接受一组触发器,可以接受一个列表 -->
		<property name="triggers">
			<list>
<!-- 				<ref bean="simplerTrigger" /> -->
				<ref bean="cronTrigger"/>
			</list>
		</property>
	</bean>
</beans>

package com.hzys.testweb.quartz;

public class MyQuartzJob{
	
	public void hello(){
		System.out.println("say hello");
	}

	
}

我这里使用的是Spring3.2.jar和quartz-2.2.1.jar的包,所以上面配置运行正常!

如果,你们有使用Spring3.1和quartz2.0以下版本包的同学,就把这两个地方改一下:

<bean id="simplerTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">

<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
改成:

<bean id="simplerTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">

<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">

这样,运行就不会报错了,运行效果也是杠杠滴!

在这里很感谢网上广大网友的帖子和经验,让我有机会学习这些知识,在这里也更加清楚明了的分享给大家!




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值