本文目录一览

1,如何在spring中配置定时任务

有一个quartz任务调度框架,可以单独用,也可以配置在Spring中,网上应该很多例子的,望对楼主有帮助。

如何在spring中配置定时任务

2,java定时任务框架有哪些

类似corn,是框架的,有quartzjdk自身的,有Timer+TimeTask等等,也可以使用线程来实现~

java定时任务框架有哪些

3,java中的定时任务实现的几种方式

如果用spring 的话 可以用spring 的组件 spring task 具体可以搜一下Qutarz 也是一个定时任务框架 原生的话 java timer

java中的定时任务实现的几种方式

4,python 定时任务 用什么东西来实现

用定时任务框架apscheduler apscheduler支持quartz表达式 你可以定义一个job每一秒检查有哪些用户需要被通知做哪些事情 不需要为每个用户的每个任务单独设一个定时器呀~

5,php如何实现定时任务php定时任务方法最佳

需要打开浏览器,用js的定时器执行一个ajax请求,将要执行的脚本放到ajax请求的地址下。<script>$(function() setinterval(“run_code()”, 1000);})function run_code() $.post("php文件");}</script>每1秒执行一次run_code这个方法,这个方法内通过ajax的形式访问一次指定的php文件,你的脚本写在这个php文件里就行。注意setinterval的第二个参数是以毫秒为单位的。

6,如何用Spring实现集群环境下的定时任务

定时任务的实现方式有多种,例如JDK自带的Timer+TimerTask方式,Spring 3.0以后的调度任务(Scheduled Task),Quartz等。Timer+TimerTask是最基本的解决方案,但是比较远古了,这里不再讨论。Spring自带的ScheduledTask是一个轻量级的定时任务调度器,支持固定时间(支持cron表达式)和固定时间间隔调度任务,支持线程池管理。以上两种方式有一个共同的缺点,那就是应用服务器集群下会出现任务多次被调度执行的情况,因为集群的节点之间是不会共享任务信息的,每个节点上的任务都会按时执行。Quartz是一个功能完善的任务调度框架,特别牛叉的是它支持集群环境下的任务调度,当然代价也很大,需要将任务调度状态序列化到数据库。Quartz框架需要10多张表协同,配置繁多,令人望而却步...经过折中考虑,还是选择了Spring的Scheduled Task来实现定时任务。如下:1. Spring配置文件application-context.xml中添加task命名空间和描述。[html] view plain copy<beans xmlns="http://www.springframework.org/schema/beans" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task.xsd"> 2. 添加调度器和线程池声明。[html] view plain copy<task:executor id="taskExecutor" pool-size="10" /> <task:annotation-driven executor="taskExecutor" /> 3. 实现调度方法。基本结构如下:[html] view plain copypackage com.netease.yx.service; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @Service public class ScheduledService @Scheduled(cron = "0 0 5 * * *") public void build() System.out.println("Scheduled Task"); } } @Scheduled注解支持秒级的cron表达式,上述声明表示每天5点执行build任务。前文已经提过,这种方式在单台应用服务器上运行没有问题,但是在集群环境下,会造成build任务在5点的时候运行多次,遗憾的是,Scheduled Task在框架层面没有相应的解决方案,只能靠程序员在应用级别进行控制。如何控制?1. 无非是一个任务互斥访问的问题,声明一把全局的“锁”作为互斥量,哪个应用服务器拿到这把“锁”,就有执行任务的权利,未拿到“锁”的应用服务器不进行任何任务相关的操作。2.这把“锁”最好还能在下次任务执行时间点前失效。在项目中我将这个互斥量放在了redis缓存里,1小时过期,这个过期时间是由任务调度的间隔时间决定的,只要小于两次任务执行时间差,大于集群间应用服务器的时间差即可。完整定时任务类如下:[html] view plain copypackage com.netease.yx.service; import javax.annotation.Resource; import org.apache.commons.lang3.time.DateUtils; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import com.netease.yx.service.ICacheService; @Service public class ScheduledService @Resource private ICacheService cache = null; private static String CACHE_LOCK = "cache_lock"; private static int EXPIRE_PERIOD = (int)DateUtils.MILLIS_PER_HOUR / 1000; @Scheduled(cron = "0 0 5 * * *") public void build() if (cache.get(CACHE_LOCK) == null) cache.set(CACHE_LOCK, true, EXPIRE_PERIOD); doJob(); } } }
定时任务的实现方式有多种,例如JDK自带的Timer+TimerTask方式,Spring 3.0以后的调度任务(Scheduled Task),Quartz等。Timer+TimerTask是最基本的解决方案,但是比较远古了,这里不再讨论。Spring自带的ScheduledTask是一个轻量级的定时任务调度器,支持固定时间(支持cron表达式)和固定时间间隔调度任务,支持线程池管理。以上两种方式有一个共同的缺点,那就是应用服务器集群下会出现任务多次被调度执行的情况,因为集群的节点之间是不会共享任务信息的,每个节点上的任务都会按时执行。Quartz是一个功能完善的任务调度框架,特别牛叉的是它支持集群环境下的任务调度,当然代价也很大,需要将任务调度状态序列化到数据库。Quartz框架需要10多张表协同,配置繁多,令人望而却步...经过折中考虑,还是选择了Spring的Scheduled Task来实现定时任务。如下:1. Spring配置文件application-context.xml中添加task命名空间和描述。[html] view plain copyxmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> 2. 添加调度器和线程池声明。 [html] view plain copy 3. 实现调度方法。基本结构如下: [html] view plain copy package com.netease.yx.service; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @Service public class ScheduledService { @Scheduled(cron = "0 0 5 * * *") public void build() { System.out.println("Scheduled Task"); } } @Scheduled注解支持秒级的cron表达式,上述声明表示每天5点执行build任务。 前文已经提过,这种方式在单台应用服务器上运行没有问题,但是在集群环境下,会造成build任务在5点的时候运行多次,遗憾的是,Scheduled Task在框架层面没有相应的解决方案,只能靠程序员在应用级别进行控制。 如何控制? 1. 无非是一个任务互斥访问的问题,声明一把全局的“锁”作为互斥量,哪个应用服务器拿到这把“锁”,就有执行任务的权利,未拿到“锁”的应用服务器不进行任何任务相关的操作。 2.这把“锁”最好还能在下次任务执行时间点前失效。 在项目中我将这个互斥量放在了redis缓存里,1小时过期,这个过期时间是由任务调度的间隔时间决定的,只要小于两次任务执行时间差,大于集群间应用服务器的时间差即可。 完整定时任务类如下: [html] view plain copy package com.netease.yx.service; import javax.annotation.Resource; import org.apache.commons.lang3.time.DateUtils; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import com.netease.yx.service.ICacheService; @Service public class ScheduledService { @Resource private ICacheService cache = null; private static String CACHE_LOCK = "cache_lock"; private static int EXPIRE_PERIOD = (int)DateUtils.MILLIS_PER_HOUR / 1000; @Scheduled(cron = "0 0 5 * * *") public void build() { if (cache.get(CACHE_LOCK) == null) { cache.set(CACHE_LOCK, true, EXPIRE_PERIOD); doJob(); } } }
定时任务的实现方式有多种,例如jdk自带的timer+timertask方式,spring 3.0以后的调度任务(scheduled task),quartz等。 timer+timertask是最基本的解决方案,但是比较远古了,这里不再讨论。spring自带的scheduled task是一个轻量级的定时任务调度器,支持固定时间(支持cron表达式)和固定时间间隔调度任务,支持线程池管理。以上两种方式有一个共同的缺点,那就是应用服务器集群下会出现任务多次被调度执行的情况,因为集群的节点之间是不会共享任务信息的,每个节点上的任务都会按时执行。quartz是一个功能完善的任务调度框架,特别牛叉的是它支持集群环境下的任务调度,当然代价也很大,需要将任务调度状态序列化到数据库。quartz框架需要10多张表协同,配置繁多,令人望而却步... 经过折中考虑,还是选择了spring的scheduled task来实现定时任务。如下: 1. spring配置文件application-context.xml中添加task命名空间和描述。 [html] view plain copy xmlns:task="" xsi:schemalocation=" /spring-beans.xsd /spring-task.xsd"> 2. 添加调度器和线程池声明。 [html] view plain copy 3. 实现调度方法。基本结构如下: [html] view plain copy package com.netease.yx.service; import org.springframework.scheduling.annotation.scheduled; import org.springframework.stereotype.service; @service public class scheduledservice { @scheduled(cron = "0 0 5 * * *") public void build() { system.out.println("scheduled task"); } } @scheduled注解支持秒级的cron表达式,上述声明表示每天5点执行build任务。 前文已经提过,这种方式在单台应用服务器上运行没有问题,但是在集群环境下,会造成build任务在5点的时候运行多次,遗憾的是,scheduled task在框架层面没有相应的解决方案,只能靠程序员在应用级别进行控制。 如何控制? 1. 无非是一个任务互斥访问的问题,声明一把全局的“锁”作为互斥量,哪个应用服务器拿到这把“锁”,就有执行任务的权利,未拿到“锁”的应用服务器不进行任何任务相关的操作。 2.这把“锁”最好还能在下次任务执行时间点前失效。 在项目中我将这个互斥量放在了redis缓存里,1小时过期,这个过期时间是由任务调度的间隔时间决定的,只要小于两次任务执行时间差,大于集群间应用服务器的时间差即可。 完整定时任务类如下: [html] view plain copy package com.netease.yx.service; import javax.annotation.resource; import org.apache.commons.lang3.time.dateutils; import org.springframework.scheduling.annotation.scheduled; import org.springframework.stereotype.service; import com.netease.yx.service.icacheservice; @service public class scheduledservice { @resource private icacheservice cache = null; private static string cache_lock = "cache_lock"; private static int expire_period = (int)dateutils.millis_per_hour / 1000; @scheduled(cron = "0 0 5 * * *") public void build() { if (cache.get(cache_lock) == null) { cache.set(cache_lock, true, expire_period); dojob(); } } }

文章TAG:定时任务框架  如何在spring中配置定时任务  
下一篇