spring中集成TimerTask执行定时任务 中配置文件的配置
来源:互联网 发布:星游记网络大电影收益 编辑:程序博客网 时间:2024/04/30 08:04
在spring的bean配置文件中配置,当系统加载该文件时,配置的定时器将自动启动,同时被spring管理。
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 implements ServletContextAware{
- /*通过实现ServletContextAware可获得servletContext*/
- private ServletContext servletContext;
- private static Logger logger = Logger.getLogger(BirthdayReminderTimerTask.class);
- @Override
- public void run() {
- // logger.debug("BirthdayReminderTimerTask is running");
- setServletContext(servletContext);
- try {
- System.out.println(this.servletContext.getRealPath("/"));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void setServletContext(ServletContext servletContext) {
- this.servletContext = servletContext;
- }
- }
- <!--自定义任务-->
- <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:beans.xml</param-value>
- </context-param>
- <!--Spring ApplicationContext 载入 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
0 0
- spring中集成TimerTask执行定时任务 中配置文件的配置
- spring中集成TimerTask执行定时任务
- spring中集成TimerTask执行定时任务
- spring中集成TimerTask执行定时任务
- spring中集成TimerTask执行定时任务
- spring中集成TimerTask执行定时任务
- spring中集成TimerTask执行定时任务
- Spring中定时执行任务的配置
- cronExpression 的写法,spring中执行定时任务,spring 集成Quartz
- 在Listener(监听器)定时启动的TimerTask(定时任务)中使用Spring@Service注解的bean
- Spring中配置定时任务
- spring中配置定时任务
- spring中配置定时任务
- spring集成quartz定时任务的配置
- 定时任务:Java中Timer和TimerTask的使用
- 定时任务:Java中Timer和TimerTask的使用
- 定时任务:Java中Timer和TimerTask的使用
- 【整理】定时任务:Java中Timer和TimerTask的使用
- [LeetCode]Invert Binary Tree
- codeforces #549G Happy Line
- Doubles
- quick-cocos2dx的windows版如何显示标题栏下方的菜单栏
- CocoaPods 执行pod install很慢 没反应
- spring中集成TimerTask执行定时任务 中配置文件的配置
- quick-cocos2dx在xcode下如何添加额外的文件夹
- FaceBook/infer-bin目录下的文件说明
- 删除grub引导的linux双系统
- 努力不一定有什么卵用,但...请你努力!
- SpannableString与SpannableStringBuilder
- 电脑和树莓派之间文件传输
- 通达OA 公共文件柜二次开发增加管理信息(图文)
- java线程安全理解