spring定时配置文件|spring定时器配置在哪个xml

1. 如何配置Spring定时器准确运行时间

Spring中有自带任务调度框架Quartz ,直接在xml配置文件中配置就好。

2. spring定时器如何配置

上面这种仁兄所言极是, spring定时器目前主要就是quartz来实现, 如果自己手写代码不用spring管理的话, TimeTask应该是不错的选择, 不过我平时测试时常有发现TimeTask执行任务时, 有时一个任务重复执行多次, 所以建议还是采用Quartz的方式会好一些。。而且它操作起来也会更加的灵活。。。

3. spring定时器配置在哪个xml

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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"><!– 详细信息 –><bean id="methodInvokingJobDetail"class="org.springframework.scheling.quartz."><property name="targetObject"><ref bean="mainServiceImplTwo"/></property><property name="targetMethod"><value>springTrigger</value></property><!– 一个定时器,可能会重复的执行,有可能第一次还没有执行完,第二次就开始了设置此参数保证第一次定时器,还没有执行完时,不会执行第二次 –><property name="concurrent"><value>false</value></property></bean><!– 触发器 3.2的实现方式 CronTriggerBean<bean id="cronTrigger" class="org.springframework.scheling.quartz.CronTriggerBean"><property name="jobDetail"><ref bean="methodInvokingJobDetail"/></property><property name="cronExpression"><value>0 * * * * ?</value></property></bean> –> <!– 触发器 4.1及以后的实现方式 CronTriggerFactoryBean –> <bean id="cronTrigger" class="org.springframework.scheling.quartz.CronTriggerFactoryBean"><property name="jobDetail"><ref bean="methodInvokingJobDetail"/></property><property name="cronExpression"><value>0 * * * * ?</value></property></bean><!– 定时任务列表 –><bean class="org.springframework.scheling.quartz.SchelerFactoryBean"><property name="triggers"><list> <ref bean="cronTrigger"/> </list></property></bean></beans><value>0 * * * * ?</value> 表示时间配置:时间大小由小到大排列,从秒开始,顺序为 秒,分,时,天,月,年 *为任意 ?为无限制。marven 引入定时器需要的包(不包含其他spring的包。)<dependency> <!– spring 定时器需要 –> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <!– spring 定时器需要 –> <groupId>org.quartz-scheler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency> <dependency> <!– spring 定时器需要 –> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.6</version> </dependency>具体时间设定可参考 "0/10 * * * * ?" 每10秒触发 "0 0 12 * * ?" 每天中午12点触发 "0 15 10 ? * *" 每天上午10:15触发 "0 15 10 * * ?" 每天上午10:15触发 "0 15 10 * * ? *" 每天上午10:15触发 "0 15 10 * * ? 2005" 2005年的每天上午10:15触发 "0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发 "0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发 "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 "0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发 "0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发 "0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发 "0 15 10 15 * ?" 每月15日上午10:15触发 "0 15 10 L * ?" 每月最后一日的上午10:15触发 "0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发 "0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发 "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发 每隔5秒执行一次:*/5 * * * * ? 每隔1分钟执行一次:0 */1 * * * ? 每天23点执行一次:0 0 23 * * ? 每天凌晨1点执行一次:0 0 1 * * ? 每月1号凌晨1点执行一次:0 0 1 1 * ? 每月最后一天23点执行一次:0 0 23 L * ? 每周星期天凌晨1点实行一次:0 0 1 ? * L 在26分、29分、33分执行一次:0 26,29,33 * * * ? 每天的0点、13点、18点、21点都执行一次:0 0 0,13,18,21 * * ? 二、注解形式实现在springmvc.xml里面(根据自己的实际代码改变)xmlns 多加下面的内容、 xmlns:task="http://www.springframework.org/schema/task" 然后xsi:schemaLocation多加下面的内容、http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd<!– task:注解定时器使用 这种配置有错误 会报找不到 org.springframework.scheling.TaskScheler 这个bean(这其实是一个接口)<context:annotation-config/> <bean class="org.springframework.beans.factory.annotation."/> –><!– 这种配置是正常的 —><task:annotation-driven scheler="qbScheler" mode="proxy"/> <task:scheler id="qbScheler" pool-size="10"/> 要放在<mvc:annotation-driven/><context:component-scan base-package="com"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /></context:component-scan>之后代码实现类实现如下:@Component public class UsersTimerImpl implements IUsersTimer{//更新 支付密码状态/// 定时器注解调用@Scheled(cron = "00 00 00 * * *")public void updatePayStatus(){//执行逻辑的代码}}

4. spring定时任务配置问题,求教

(1)在Spring的配置文件中添加定时任务相关配置:

xml配置的头文件中添加:

xmlns:task="http://www.springframework.org/schema/task"

以及在xsi:schemaLocation中添加:

http://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-4.0.xsd

最后添加:

<context:component-scanbase-package="cn.zifangsky.task"/> <task:executorid="executor"pool-size="5"/> <task:schelerid="scheler"pool-size="10"/> <task:annotation-drivenexecutor="executor"scheler="scheler"/>

其中,这里首先定义了Spring自动扫描定时任务所在的package,也就是“cn.zifangsky.task”。接着定义了两个线程池以及启用定时任务的扫描机制

(2)添加测试任务:

packagecn.zifangsky.task;importjava.text.Format;importjava.text.SimpleDateFormat;importjava.util.Date;importorg.springframework.scheling.annotation.Scheled;importorg.springframework.stereotype.Component;@ComponentpublicclassSimpleSpringTask{ /** *每次任务执行完之后的2s后继续执行 */ @Scheled(fixedDelay=2000) publicvoidsay(){ Datecurrent=newDate(); Formatformat=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss"); System.out.println("——–"+format.format(current)+"———"); } /** *0分的时候打印 */ @Scheled(cron="0****?") publicvoidprint(){ System.out.println("当前是整分!!!"); } }

上面第一个任务定义了每个任务执行完之后的2s之后再次执行,如果需要强制指定每隔多少时间执行一次任务,可以将上面的fixedDelay改成fixedRate,如:

/** *每隔两秒执行一次本方法 */ @Scheled(fixedRate=2000) publicvoidsay(){ Datecurrent=newDate(); Formatformat=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss"); System.out.println("——–"+format.format(current)+"———"); }

当然,上面的第二种任务形式类似于Linux下的crontab定时任务,几个参数位分别表示:分钟、小时、天(每月中的天)、月份以及星期。最后的那个问号毫无疑问就表示使用@Scheled注解标注的本个方法了

注:如果想要了解更多的关于Linux中使用crontab命令的用法可以参考我的这篇文章:https://www.zifangsky.cn/591.html

(3)测试:

运行这个项目后,最后控制台中的输出如下:

注:上面的水印是我的个人博客。由于题主的问题不是一两句文字可以描述清楚地,因此引用了我博客中的内容,请审核人员手下留情

PS:如果觉得对你有所帮助的话,望采纳!!!

5. 如何在spring中配置定时任务

spring的定时任务配置分为三个步骤: 1、定义任务 2、任务执行策略配置 3、启动任务(程序中一般我们都是到过写的,直观些) 1、定义任务 <!–要定时执行的方法–> <bean id="testTaskJob" class="org.springframework.scheling.quartz."> <property name="targetObject"> <!–指定要定时执行的方法所在类,将定时任务定义成bean–> <ref bean="testTask" /> </property> <property name="targetMethod"> <!–指定定时执行的方法–> <value>execute</value> </property> <property name="concurrent"> <!–指定目标封装为有状态的任务,有状态的任务不能并发执行,无状态的任务可并发执行–> <value>false</value> </property> </bean> 2、任务执行策略配置 (1)指定重复间隔的定时任务 <!– 调度时间设置–> <bean id="testTaskJobTrigger" class="org.springframework.scheling.quartz.SimpleTriggerBean"> <property name="jobDetail"> <ref bean="testTaskJob" /> </property> <!– 延时启动时间,单位ms –> <property name="startDelay" value="60000"></property> <!– 重复间隔时间,单位ms –> <property name="repeatInterval" value="60000"> </property> </bean> (2)按周期执行的任务


赞 (0)