- 浏览: 2110253 次
- 性别:
- 来自: 广州
文章分类
最新评论
-
ratlsun:
想请教下uc最新版本在android4.2和4.3版本上是不是 ...
UC浏览器8.3 (for iPhone)设计理念.“無”为而设 -
gly0920sky520123:
很有用哦,谢谢
DOS命令大全(经典收藏) -
chenyu0748:
UC加油,花哥加油~
UC浏览器8.3 (for iPhone)设计理念.“無”为而设 -
cnliuyix:
LZ搞点更有层次的吧,介个一般工程里根本用不到这么简单的。Si ...
Android 设计一个可单选,多选的Demo -
gang4415:
rgz03407@163.com
JSR规范,系统参数测试大全
相信做软件的朋友都有这样的经历,我的软件是不是少了点什么东西呢?比如定时任务啊,
就拿新闻发布系统来说,如果新闻的数据更新太快,势必涉及一个问题,这些新闻不能由人工的去发布,应该让系统自己发布,这就需要用到定时定制任务了,以前定制任务无非就是设计一个Thread,并且设置运行时间片,让它到了那个时间执行一次,就ok了,让系统启动的时候启动它,想来也够简单的。不过有了spring,我想这事情就更简单了。
看看spring的配置文件,想来就只有这个配置文件了
xml 代码
- <bean id="infoCenterAutoBuildTask"
- class="com.teesoo.teanet.scheduling.InfoCenterAutoBuildTask">
- <property name="baseService" ref="baseService" />
- <property name="htmlCreator" ref="htmlCreator" />
- </bean>
- <bean id="scheduledTask"
- class="org.springframework.scheduling.timer.ScheduledTimerTask">
- <!-- wait 10 seconds before starting repeated execution -->
- <property name="delay" value="10000" />
- <!-- run every 50 seconds -->
- <property name="period" value="1000000" />
- <property name="timerTask" ref="infoCenterAutoBuildTask" />
- </bean>
- <bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
- <property name="scheduledTimerTasks">
- <list>
- <!-- see the example above -->
- <ref bean="scheduledTask" />
- </list>
- </property>
- </bean>
上面三个配置文件中只有一个配置文件是涉及到您自己的class的,其他的都是spring的类。很简单吧
我们只需要涉及一个class让他继承java.util.TimerTask;
java 代码
- BaseTask extends java.util.TimerTask {
- //用户只需要实现这个方面,把自己的任务放到这里
- public void run(){
- }
- }
下面让我们来看看 spring的源代码
java 代码
- /*
- * Copyright 2002-2005 the original author or authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package org.springframework.scheduling.timer;
- import java.util.TimerTask;
- /**
- * JavaBean that describes a scheduled TimerTask, consisting of
- * the TimerTask itself (or a Runnable to create a TimerTask for)
- * and a delay plus period. Period needs to be specified;
- * there is no point in a default for it.
- *
- * <p>The JDK Timer does not offer more sophisticated scheduling
- * options such as cron expressions. Consider using Quartz for
- * such advanced needs.
- *
- * <p>Note that Timer uses a TimerTask instance that is shared
- * between repeated executions, in contrast to Quartz which
- * instantiates a new Job for each execution.
- *
- * @author Juergen Hoeller
- * @since 19.02.2004
- * @see java.util.TimerTask
- * @see java.util.Timer#schedule(TimerTask, long, long)
- * @see java.util.Timer#scheduleAtFixedRate(TimerTask, long, long)
- */
- public class ScheduledTimerTask {
- private TimerTask timerTask;
- private long delay = 0;
- private long period = 0;
- private boolean fixedRate = false;
- /**
- * Create a new ScheduledTimerTask,
- * to be populated via bean properties.
- * @see #setTimerTask
- * @see #setDelay
- * @see #setPeriod
- * @see #setFixedRate
- */
- public ScheduledTimerTask() {
- }
- /**
- * Create a new ScheduledTimerTask, with default
- * one-time execution without delay.
- * @param timerTask the TimerTask to schedule
- */
- public ScheduledTimerTask(TimerTask timerTask) {
- this.timerTask = timerTask;
- }
- /**
- * Create a new ScheduledTimerTask, with default
- * one-time execution with the given delay.
- * @param timerTask the TimerTask to schedule
- * @param delay the delay before starting the task for the first time (ms)
- */
- public ScheduledTimerTask(TimerTask timerTask, long delay) {
- this.timerTask = timerTask;
- this.delay = delay;
- }
- /**
- * Create a new ScheduledTimerTask.
- * @param timerTask the TimerTask to schedule
- * @param delay the delay before starting the task for the first time (ms)
- * @param period the period between repeated task executions (ms)
- * @param fixedRate whether to schedule as fixed-rate execution
- */
- public ScheduledTimerTask(TimerTask timerTask, long delay, long period, boolean fixedRate) {
- this.timerTask = timerTask;
- this.delay = delay;
- this.period = period;
- this.fixedRate = fixedRate;
- }
- /**
- * Create a new ScheduledTimerTask, with default
- * one-time execution without delay.
- * @param timerTask the Runnable to schedule as TimerTask
- */
- public ScheduledTimerTask(Runnable timerTask) {
- setRunnable(timerTask);
- }
- /**
- * Create a new ScheduledTimerTask, with default
- * one-time execution with the given delay.
- * @param timerTask the Runnable to schedule as TimerTask
- * @param delay the delay before starting the task for the first time (ms)
- */
- public ScheduledTimerTask(Runnable timerTask, long delay) {
- setRunnable(timerTask);
- this.delay = delay;
- }
- /**
- * Create a new ScheduledTimerTask.
- * @param timerTask the Runnable to schedule as TimerTask
- * @param delay the delay before starting the task for the first time (ms)
- * @param period the period between repeated task executions (ms)
- * @param fixedRate whether to schedule as fixed-rate execution
- */
- public ScheduledTimerTask(Runnable timerTask, long delay, long period, boolean fixedRate) {
- setRunnable(timerTask);
- this.delay = delay;
- this.period = period;
- this.fixedRate = fixedRate;
- }
- /**
- * Set the Runnable to schedule as TimerTask.
- * @see DelegatingTimerTask
- */
- public void setRunnable(Runnable timerTask) {
- this.timerTask = new DelegatingTimerTask(timerTask);
- }
- /**
- * Set the TimerTask to schedule.
- */
- public void setTimerTask(TimerTask timerTask) {
- this.timerTask = timerTask;
- }
- /**
- * Return the TimerTask to schedule.
- */
- public TimerTask getTimerTask() {
- return timerTask;
- }
- /**
- * Set the delay before starting the task for the first time,
- * in milliseconds. Default is 0, immediately starting the
- * task after successful scheduling.
- */
- public void setDelay(long delay) {
- this.delay = delay;
- }
- /**
- * Return the delay before starting the job for the first time.
- */
- public long getDelay() {
- return delay;
- }
- /**
- * Set the period between repeated task executions, in milliseconds.
- * Default is 0, leading to one-time execution. In case of a positive
- * value, the task will be executed repeatedly, with the given interval
- * inbetween executions.
- * <p>Note that the semantics of the period vary between fixed-rate
- * and fixed-delay execution.
- * @see #setFixedRate
- */
- public void setPeriod(long period) {
- this.period = period;
- }
- /**
- * Return the period between repeated task executions.
- */
- public long getPeriod() {
- return period;
- }
- /**
- * Set whether to schedule as fixed-rate execution, rather than
- * fixed-delay execution. Default is "false", i.e. fixed delay.
- * <p>See Timer javadoc for details on those execution modes.
- * @see java.util.Timer#schedule(TimerTask, long, long)
- * @see java.util.Timer#scheduleAtFixedRate(TimerTask, long, long)
- */
- public void setFixedRate(boolean fixedRate) {
- this.fixedRate = fixedRate;
- }
- /**
- * Return whether to schedule as fixed-rate execution.
- */
- public boolean isFixedRate() {
- return fixedRate;
- }
- }
说实话这个类也没什么,只是简单的包装了我们的timertask,里面也就只有几个属性,一个是时间片,一个是任务等。
真正运行我们的任务的类是:
java 代码
- /*
- * Copyright 2002-2006 the original author or authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package org.springframework.scheduling.timer;
- import java.util.Timer;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.springframework.beans.factory.DisposableBean;
- import org.springframework.beans.factory.FactoryBean;
- import org.springframework.beans.factory.InitializingBean;
- /**
- * FactoryBean that sets up a JDK 1.3+ Timer and exposes it for bean references.
- *
- * <p>Allows for registration of ScheduledTimerTasks, automatically starting
- * the Timer on initialization and cancelling it on destruction of the context.
- * In scenarios that just require static registration of tasks at startup,
- * there is no need to access the Timer instance itself in application code.
- *
- * <p>Note that Timer uses a TimerTask instance that is shared between
- * repeated executions, in contrast to Quartz which instantiates a new
- * Job for each execution.
- *
- * @author Juergen Hoeller
- * @since 19.02.2004
- * @see ScheduledTimerTask
- * @see java.util.Timer
- * @see java.util.TimerTask
- */
- public class TimerFactoryBean implements FactoryBean, InitializingBean, DisposableBean {
- protected final Log logger = LogFactory.getLog(getClass());
- private ScheduledTimerTask[] scheduledTimerTasks;
- private boolean daemon = false;
- private Timer timer;
- /**
- * Register a list of ScheduledTimerTask objects with the Timer that
- * this FactoryBean creates. Depending on each SchedulerTimerTask's
- * settings, it will be registered via one of Timer's schedule methods.
- * @see java.util.Timer#schedule(java.util.TimerTask, long)
- * @see java.util.Timer#schedule(java.util.TimerTask, long, long)
- * @see java.util.Timer#scheduleAtFixedRate(java.util.TimerTask, long, long)
- */
- public void setScheduledTimerTasks(ScheduledTimerTask[] scheduledTimerTasks) {
- this.scheduledTimerTasks = scheduledTimerTasks;
- }
- /**
- * Set whether the timer should use a daemon thread,
- * just executing as long as the application itself is running.
- * <p>Default is "false": The timer will automatically get cancelled on
- * destruction of this FactoryBean. Hence, if the application shuts down,
- * tasks will by default finish their execution. Specify "true" for eager
- * shutdown of threads that execute tasks.
- * @see java.util.Timer#Timer(boolean)
- */
- public void setDaemon(boolean daemon) {
- this.daemon = daemon;
- }
- public void afterPropertiesSet() {
- logger.info("Initializing Timer");
- this.timer = createTimer(this.daemon);
- // Register all ScheduledTimerTasks.
- if (this.scheduledTimerTasks != null) {
- for (int i = 0; i < this.scheduledTimerTasks.length; i++) {
- ScheduledTimerTask scheduledTask = this.scheduledTimerTasks[i];
- if (scheduledTask.getPeriod() > 0) {
- // repeated task execution
- if (scheduledTask.isFixedRate()) {
- this.timer.scheduleAtFixedRate(
- scheduledTask.getTimerTask(), scheduledTask.getDelay(), scheduledTask.getPeriod());
- }
- else {
- this.timer.schedule(
- scheduledTask.getTimerTask(), scheduledTask.getDelay(), scheduledTask.getPeriod());
- }
- }
- else {
- // One-time task execution.
- this.timer.schedule(scheduledTask.getTimerTask(), scheduledTask.getDelay());
- }
- }
- }
- }
- /**
- * Create a new Timer instance. Called by <code>afterPropertiesSet</code>.
- * Can be overridden in subclasses to provide custom Timer subclasses.
- * @param daemon whether to create a Timer that runs as daemon thread
- * @return a new Timer instance
- * @see #afterPropertiesSet()
- * @see java.util.Timer#Timer(boolean)
- */
- protected Timer createTimer(boolean daemon) {
- return new Timer(daemon);
- }
- public Object getObject() {
- return this.timer;
- }
- public Class getObjectType() {
- return Timer.class;
- }
- public boolean isSingleton() {
- return true;
- }
- /**
- * Cancel the Timer on bean factory shutdown, stopping all scheduled tasks.
- * @see java.util.Timer#cancel()
- */
- public void destroy() {
- logger.info("Cancelling Timer");
- this.timer.cancel();
- }
- }
这个类就是运行我们任务的类了,我们可以定制N个任务,只需要塞到这里就ok了。
评论
16 楼
tomzhu0526
2007-08-10
版主,把我的上个贴给删了吧,不好意思.误操作了.
在努力学习spring中,不是太懂呢.
在努力学习spring中,不是太懂呢.
15 楼
tomzhu0526
2007-08-10
caocao 写道
呵呵,我用python来写调度脚本,代码行数比spring配置文件行数还小很多,很稳定、跨平台,想怎么跑就怎么跑
ssss
14 楼
caocao
2007-08-09
呵呵,我用python来写调度脚本,代码行数比spring配置文件行数还小很多,很稳定、跨平台,想怎么跑就怎么跑
13 楼
peterwei
2007-08-08
spring的定时任务在具体项目时还是有不少不足的地方。我最近这星期在搞定时任务+任务配置的开发,像收费的账户定时冲账,服务自动停断,欠费催缴等.当需要随意配置任务,以及任意时间执行时,用spring那种配置方法就很不好用了,因为spring都是写死在配置文件中。而且像web服务器死了,任务来不及执行等各种情况,我想spring的定制任务是没法处理的。还有像集群这种情况,我想也是不好处理。所以我现在只能用quartz+db来实现,搞了一个星期了,快搞完了。搞完后,我把代码整理出来,我相信会有不少人遇到我这样的项目,大家互相交流一下吧。
12 楼
cljhyjs
2007-08-08
spring的定制任务,有时并不灵活,在几个项目中使用发现有以下问题
1、经常会运行一段时间后,不再调度运行了。怀疑是run()方法中的业务处理有异常引起的。
2、和使用一个thread来控制作循环来讲,后者更加灵活一些,特别是不需要做同步处理。
1、经常会运行一段时间后,不再调度运行了。怀疑是run()方法中的业务处理有异常引起的。
2、和使用一个thread来控制作循环来讲,后者更加灵活一些,特别是不需要做同步处理。
11 楼
boddi
2007-08-08
启动服务后,ScheduledTimerTask是不是第一次设置了delay和period后就无法修改了呢
10 楼
heartsong
2007-08-03
cherami 写道
我记得spring是使用Quartz作为任务调度的内部实现的
Spring支持Timer和Quartz
Quartz功能更为强大。
9 楼
boddi
2007-08-02
<bean id="infoCenterAutoBuildTask" class="com.teesoo.teanet.scheduling.InfoCenterAutoBuildTask"> <property name="baseService" ref="baseService" /> <property name="htmlCreator" ref="htmlCreator" /> </bean> <bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"> <!-- wait 10 seconds before starting repeated execution --> <property name="delay" value="10000" /> <!-- run every 50 seconds --> <property name="period" value="1000000" /> <property name="timerTask" ref="infoCenterAutoBuildTask" /> </bean> <bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean"> <property name="scheduledTimerTasks"> <list> <!-- see the example above --> <ref bean="scheduledTask" /> </list> </property> </bean>
还可以这样配置吧:
<bean id="infoCenterAutoBuildTaskFactory" class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean"> <property name="targetObject"> <ref bean="infoCenterAutoBuildTask"/> </property> <property name="targetMethod"> <value>your taskMethod</value> </property> </bean> <bean id="infoCenterAutoBuildTask" class="com.teesoo.teanet.scheduling.InfoCenterAutoBuildTask"> <property name="baseService" ref="baseService" /> <property name="htmlCreator" ref="htmlCreator" /> </bean> <bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"> <!-- wait 10 seconds before starting repeated execution --> <property name="delay" value="10000" /> <!-- run every 50 seconds --> <property name="period" value="1000000" /> <property name="timerTask" ref="infoCenterAutoBuildTaskFactory" /> </bean> <bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean"> <property name="scheduledTimerTasks"> <list> <!-- see the example above --> <ref bean="scheduledTask" /> </list> </property> </bean>
8 楼
srdrm
2007-07-28
quartz 的功能更强大.
7 楼
oboaix
2007-04-22
好!
6 楼
pikachu
2007-04-04
realreal2000 写道
用了下,又看了下代码,好像不能设置具体时间执行任务,只能
<property name="delay" value="10000" />
设置启动后多长时间自动执行,郁闷了
不知道是不是自己没有看懂
<property name="delay" value="10000" />
设置启动后多长时间自动执行,郁闷了
不知道是不是自己没有看懂
看spring的quartz版定时器
5 楼
realreal2000
2007-04-04
用了下,又看了下代码,好像不能设置具体时间执行任务,只能
<property name="delay" value="10000" />
设置启动后多长时间自动执行,郁闷了
不知道是不是自己没有看懂
<property name="delay" value="10000" />
设置启动后多长时间自动执行,郁闷了
不知道是不是自己没有看懂
4 楼
realreal2000
2007-04-04
收藏,正好需要
3 楼
cherami
2007-04-03
我记得spring是使用Quartz作为任务调度的内部实现的
2 楼
sorphi
2007-04-02
linux下我更倾向于做一个standalone app,让cron来管理
1 楼
roc8633284
2007-04-02
哦了,收下了。
相关推荐
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!-- 这里添加触发器配置 --> <!-- 这里添加任务详情配置 --> ``` 然后,定义Job类并实现`org....
Spring 排程(Scheduling)技术是Spring框架中用于实现定时任务的重要功能,它允许开发者在应用程序中安排任务定期执行,以满足各种自动化需求,如数据同步、日志清理、定时报告生成等。排程技术的核心在于管理和...
通过深入学习Spring任务调度,开发者可以有效地管理和执行各种定时任务,提升系统的自动化水平。在实际项目中,可以根据需求的复杂程度选择使用Spring Task或是集成Quartz。同时,理解源码有助于我们更高效地利用...
通过阅读和理解这些源码,你可以更好地了解Spring如何管理和调度定时任务,以及如何根据项目需求选择合适的定时任务实现方式。在chapter13目录下的文件可能包含了这些源码示例,你可以逐一研究,加深对Spring定时...
`TaskExecution`和`TaskScheduling`是Spring框架的基础任务调度API,它们允许开发者创建和执行简单的定时任务。`TaskExecution`接口提供了任务执行的核心功能,如启动、停止和查询任务状态,而`TaskScheduling`接口...
然而,Quartz的配置相对复杂,需要对作业类进行特殊继承,如`org.springframework.scheduling.quartz.QuartzJobBean`,这可能会增加代码的复杂性。 Spring 3.0以后引入了自己的Task模块,提供了一种轻量级的定时...
在IT行业中,Spring Cloud Schedule是Spring Cloud框架的一个重要组件,用于构建分布式系统中的定时任务。本文将深入探讨如何使用Spring Cloud Schedule与MyBatis相结合,实现对MySQL数据库的读写操作,并设定按照...
在Spring中,我们可以通过实现`org.springframework.scheduling.quartz.JobDetailBean`和`org.springframework.scheduling.quartz.CronTriggerBean`来定义任务和触发器。JobDetailBean用于描述任务的属性,如任务类...
在Spring中,我们可以使用Spring的TaskExecution和TaskScheduling模块来实现定时任务,这些功能通常依赖于特定的jar包。以下是对标题和描述中涉及的知识点的详细解释: 1. **Spring Task模块**:这是Spring提供的一...
spring定时任务 xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=...
### Spring2.5定时器任务实现机制与配置详解 #### 一、Spring2.5定时器任务概述 在Spring框架2.5版本中,提供了强大的定时任务管理功能,支持多种方式来实现定时任务的调度与执行。这为开发人员提供了一个灵活且...
定时任务通常通过实现`org.springframework.scheduling.Trigger`接口或者继承`org.springframework.scheduling.annotation.Scheduled`注解的类来创建。这里我们使用注解方式,因为它是更简洁且常见的方法。 4. **...
Spring 整合任务调度框架 Quartz 在软件开发中,任务调度框架是非常重要的一部分,它可以帮助开发者更好地管理和执行各种任务。在 Java 领域中,Quartz 是一个非常流行的任务调度框架,而 Spring 是一个非常流行的 ...
这个jar包中包含了`org.springframework.scheduling`包,里面包含了用于实现定时任务的接口和类,如`TaskScheduler`和`ScheduledTaskRegistrar`。 为了实现定时任务,我们还需要`spring-tx.jar`,它提供了事务管理...
在Spring框架中,定时任务是常见的需求,例如用于执行定期数据同步、日志清理或发送邮件等。...通过理解Quartz的工作原理和Spring的定时任务API,我们可以有效地管理定时任务,确保它们按预期执行。
在Spring框架中,定时任务是一项重要的功能,它允许开发者在特定的时间间隔内执行特定的任务,无需手动触发。这个实例是关于如何在Spring中配置和使用定时任务,同时结合MyBatis来向数据库插入数据。接下来,我们将...
这些资源可以帮助读者深入理解如何在Spring 2.0中配置和使用Quartz,以及如何编写和调度定时任务。通过学习这些资料,开发者可以更好地掌握Spring框架的定时任务功能,从而在实际项目中灵活地安排任务执行。
在Java Spring框架中,动态配置定时任务是一项非常实用的功能,它允许我们根据需求灵活地更改或添加定时任务,而无需每次改动都重启应用。本文将深入探讨如何在Spring中实现这种动态配置,以及如何结合数据库来管理...
首先,我们要理解Spring的Task Execution和Scheduling模块。这个模块提供了一个统一的接口,可以方便地在应用中执行异步任务和定时任务。Spring通过`org.springframework.scheduling.TaskScheduler`和`org.spring...