- 浏览: 520585 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (299)
- Oracle(pl/sql_Erp_Pro*C) (69)
- 设计模式 (4)
- spring (23)
- ext (17)
- apache开源项目应用 (4)
- jquery (16)
- 生活琐事 (8)
- 下载资源 (23)
- mysql (2)
- Eclipse使用积累 (5)
- 报表类(报表/图表) (13)
- php (4)
- Web多彩文本框 (3)
- json (4)
- jqgrid (2)
- ant (2)
- java算法积累 (8)
- EL表达式/JSTL (4)
- poi (3)
- gwt (2)
- 爬网第一步 (2)
- javascript (17)
- Javaweb (8)
- tomcat (1)
- flex (1)
- Java&DB (3)
- J2SE (7)
- linux (3)
- 数据结构 (1)
- dot net (5)
- struts (1)
- ibatis (1)
- log4j (1)
- 项目管理 (1)
- Java native interface(jni,jacob......) (5)
- applet (1)
- VB.net/C#.net/JNI (20)
- css (1)
- Sqlite (1)
- servlet (1)
- REST (1)
最新评论
-
wenhurena:
能不能给一下解压密码roki.work.2017@gmail. ...
Ebs解体新書と学習資料1 -
liutao1600:
楼主写的太好了,每天学习~~
Spring_MVC(6)测试 -
liutao1600:
太好了,每天学习你的文章~~~
Spring_MVC(3)表单页面处理 -
liutao1600:
学习了,太好了
Spring_MVC(2)控制层处理 -
liutao1600:
学习了~~~
Spring_MVC(1)构建简单web应用
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(){
- }
- }
java 代码(spring的源代码)
- /*
- * 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;
- }
- }
真正运行的任务的类是:
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了。
发表评论
-
cronExpression
2010-08-30 17:57 1193一个Cron-表达式是一个由六至七个字段组成由空格分隔的 ... -
spring任务调度(task)
2010-08-23 13:24 2855spring 任务调度总结参考资料http://www.ibm ... -
spring3.0注解式声明
2010-08-19 17:39 4379从spring2.5开始,annotation结合BeanPo ... -
Spring Roo 自动化集成开发工具
2010-08-04 14:54 2255Roo是一种 Spring 开发的辅助工具,使用命令行操作来生 ... -
spring3.0学习
2010-08-03 16:24 861https://src.springframework.org ... -
Spring mvc 转发、重定向
2010-07-28 16:53 2413spring控制器最后返回一个ModelAndView( ... -
spring api download
2010-07-28 08:43 1190在网上找了好多,都下不下来,要不就是需要注册登录什么的,才能下 ... -
Spring uploadFile
2010-05-02 20:45 2239先让我们来看一段摘自《Spring 2.5 Referen ... -
Spring_MVC(6)测试
2010-04-24 18:48 3732这里将用到以下几个包: 引用 aopalliance-1 ... -
Spring_MVC(5)业务层处理
2010-04-24 18:47 1986这里将用到以下几个包: 引用 aopalliance-1 ... -
Spring_MVC(4)持久层处理
2010-04-24 18:45 1206这里将用到以下几个包: 引用 aopalliance-1 ... -
Spring_MVC(3)表单页面处理
2010-04-24 18:43 3045// 跳转到用户信息页面 ... -
Spring_MVC(2)控制层处理
2010-04-24 18:42 1700言归正传,研究一下注解下的控制层。 我习惯于使用JSTL展示页 ... -
Spring_MVC(1)构建简单web应用
2010-04-24 18:40 1445Java代码 /** * 2010 ... -
spring实现文件上传
2010-04-24 15:15 1623Spring由内置的multipart支持web应用中的文件上 ... -
Spring MVC:使用InternalResourceViewResolver视图解析器
2010-02-24 09:14 2054参考:Sping 2.0.8\docs\MVC-step-by ... -
ibatis+spring控制事务配置
2009-04-05 10:25 1928<bean id="dataSource&qu ... -
转:spring事务控制配置实例
2009-04-03 10:47 3364Spring声明式事务配置的几种方法 在Spring中进 ... -
spring+ibatis+struts事务控制配置
2009-04-03 10:38 1563<?xml version="1.0" ... -
spring+ibatis+struts配置问题
2009-04-01 14:48 1137以下web.xml配置 <?xml version=&q ...
相关推荐
spring定时任务SimpleTrigger 和CronTrigger 配置
这时,我们可以将定时任务配置移到数据库中,通过读取数据库中的定时规则来动态执行任务。首先,我们需要创建一个数据库表来存储这些规则,如`job_config`,包含字段`id`、`task_name`、`cron_expression`等。 接着...
这里我们将深入讲解如何在Spring中配置和使用定时任务。 首先,Spring提供了两种定时任务的实现方式:`Spring Task` 和 `Quartz Scheduler`。在本例中,我们看到的是使用Quartz Scheduler的例子,这是一个功能强大...
springtask配置,大家一起参考学习下,分享下。Java方面
通过深入学习Spring任务调度,开发者可以有效地管理和执行各种定时任务,提升系统的自动化水平。在实际项目中,可以根据需求的复杂程度选择使用Spring Task或是集成Quartz。同时,理解源码有助于我们更高效地利用...
本文将深入探讨如何在Spring中配置多个Quartz定时任务,并结合`quartz.properties`文件进行详细讲解。 首先,我们需要理解Quartz的基本概念。Quartz是开源的作业调度框架,允许应用程序在特定时间执行任务。它支持...
Spring 定时任务配置详解 Spring 框架提供了对定时调度服务的内置支持类,支持从 JDK 1.3 开始内置的 Timer 类和 Quartz Scheduler。开发者可以通过 FactoryBean,分别指向 Timer 或 Trigger 实例的引用进行配置。...
在Spring框架中,XML配置是传统且广泛使用的方式来设置应用的组件和行为,包括实现定时任务。定时任务在软件开发中扮演着重要角色,它允许应用程序在预设的时间执行特定的任务,例如数据清理、日志归档或者发送通知...
下面将详细解释Spring任务调度的关键概念和实现方法。 1. **Spring Task模块** Spring Task是Spring框架内置的任务调度模块,它提供了简单的定时任务执行能力。通过使用Spring Task,我们可以轻松地在应用中添加...
总结来说,Spring的定时任务配置涉及引入相关依赖、开启任务调度、定义任务方法并使用`@Scheduled`注解进行定时设置。通过这种方式,开发者可以轻松地在Spring应用中实现定时任务,提高系统自动化程度和效率。
《Spring任务调度配置详解:Spring+Quartz的整合应用》 在Java开发中,任务调度是不可或缺的一部分,Spring框架提供了与Quartz集成的能力,使得我们可以方便地管理和执行定时任务。本文将详细介绍如何通过Spring和...
spring计划任务时间配置完整格式,各种配置说明,案例配置介绍
通过以上步骤,你已经成功地在Spring中配置并启动了一个基于Quartz的定时任务。在实际开发中,可以根据需求调整触发器的配置,以实现不同周期、不同时间点的定时任务。此外,还可以利用Spring的AOP特性,将定时任务...
本文将详细介绍如何在Spring 3中配置Quartz来实现定时任务。 首先,理解定时任务的基本概念。定时任务是在指定时间点或按照一定规律自动执行的任务,这在业务系统中非常常见,例如数据清理、报表生成等。Quartz提供...
Quartz + spring简单配置多个任务调度 spring配置文件 简单配置
标题中的“Spring Job”的配置指的是在Spring框架中配置定时任务,通常使用的是Spring的Task执行器或者Quartz、Spring Batch等扩展组件。Spring Job是一个宽泛的概念,它可能包括Spring的AsyncConfigurer支持的异步...
### Spring 定时任务配置详解 #### 一、Spring 定时任务简介 Spring 框架提供了强大的任务调度功能,使得开发者能够轻松地在应用中实现定时任务。本篇文章将详细探讨如何在 Spring 中配置定时任务,并通过具体的...
在Spring Boot应用中,动态配置定时任务是提升系统灵活性和可维护性的重要手段。Spring Boot集成了Spring Framework的TaskExecution和TaskScheduling模块,使得我们可以方便地创建和管理定时任务。本文将深入探讨...
在实际项目应用中经常会用到定时任务,可以通过quartz和spring的简单配置即可完成,但如果要改变任务的执行时间、频率,废弃任务等就需要改变配置甚至代码需要重启服务器,这里介绍一下如何通过quartz与spring的组合...
要使用Spring的内置任务调度,我们需要在配置中启用`TaskExecutor`和`TaskScheduler`。在XML配置中,可以添加如下配置: ```xml <bean id="taskExecutor" class="org.springframework.scheduling.concurrent....