任务调度5-------Spring中使用timetask执行定时任务
来源:互联网 发布:讯捷数据恢复 编辑:程序博客网 时间:2024/05/21 17:03
Spring对Timer的支持的核心是由ScheduledTimerTask和TimerFactoryBean类组成的。 ScheduledTimerTask类是对TimerTask的包装器实现,这样你就可以为这个任务定义触发器信息。使用 TimerFactoryBean类,你可以让Spring使用配置创建触发器,并为一组指定的ScheduledTimerTask bean自动创建Timer实例。
1、自定义timerTask,比如:定时输出ServletContext中的信息,本例中输出项目的绝对路径(比如:D:\software\apache-tomcat-6.0.33\webapps\spring\)
- public class BirthdayReminderTimerTask extends TimerTask{
- private static Logger logger = Logger.getLogger(BirthdayReminderTimerTask.class);
- @Override
- public void run() {
- // do something
- }
2、在spring的bean配置文件中配置,当系统加载该文件时,配置的定时器将自动启动,同时被spring管理。
- <!--自定义任务-->
- <bean id="birthdayReminder" class="com.jep.task.BirthdayReminderTimerTask"></bean>
- <!-- ScheduledTimerTask类是对TimerTask的包装器实现,这样你就可以为这个任务定义触发器信息。 -->
- <bean id="birthdayReminderTimerTask"
- class="org.springframework.scheduling.timer.ScheduledTimerTask">
- <!-- 设置启动延迟 -->
- <property name="delay">
- <value>3000</value>
- </property>
- <!-- 后续延迟 -->
- <property name="period">
- <value>5000</value>
- </property>
- <!-- 指定触发器信息 -->
- <property name="timerTask">
- <ref local="birthdayReminder" />
- </property>
- </bean>
- <!-- 使用TimerFactoryBean类,你可以让Spring使用配置创建触发器,并为一组指定的ScheduledTimerTask bean自动创建Timer实例。 -->
- <bean id="timerFactory"
- class="org.springframework.scheduling.timer.TimerFactoryBean">
- <property name="scheduledTimerTasks">
- <list>
- <ref local="birthdayReminderTimerTask" />
- </list>
- </property>
- </bean>
3、对于web项目,需要在web.xml中进行如下配置
- <!-- Spring ApplicationContext配置文件的路径此参数用于后面的Spring-Context loader -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:applicaotionContext.xml</param-value>
- </context-param>
- <!--Spring ApplicationContext 载入 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
0 0
- 任务调度5-------Spring中使用timetask执行定时任务
- spring timetask 定时任务调度
- Spring 定时调度任务
- spring 使用注解来调度定时任务
- spring 使用注解来调度定时任务
- spring 使用注解来调度定时任务
- spring 使用注解来调度定时任务
- spring 使用注解来调度定时任务
- 使用Spring + quartz实现定时任务调度
- 使用laravel的任务调度(定时执行任务)
- java 定时任务 (timer和timetask,quartz,spring,LinuxCron)
- 定时任务实现Timer, TimeTask, ScheduledExecutorService及Spring定时器
- 定时任务实现Timer, TimeTask, ScheduledExecutorService及Spring定时器
- 定时任务实现Timer, TimeTask, ScheduledExecutorService及Spring定时器
- spring执行定时任务
- spring执行定时任务
- spring执行定时任务
- spring 定时执行任务
- ubuntu系统启动时自动进入root用户,此处列出10.04和12.04的方法
- 设置交叉工具链路径
- 总结linux下find命令
- iOS开发系列--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook详解
- SQL Transformation
- 任务调度5-------Spring中使用timetask执行定时任务
- 启用MySQL查询缓存
- jQuery事件代理的应用
- iOS开发 AFNetWork "Request failed: unacceptable content-type: text/html"
- Cell上边或者下边出现不规则长度的黑色细线
- SQLite的WAL机制
- APP微信支付集成
- Device Tree Usage
- GRE写作必备句型