当前位置: 首页 > news >正文

大连零基础网站建设培训班武汉seo网络优化公司

大连零基础网站建设培训班,武汉seo网络优化公司,杭州软件开发外包公司,阿里云客服wordpressSpring对Timer的支持的核心是由ScheduledTimerTask和TimerFactoryBean类组成的。 ScheduledTimerTask类是对TimerTask的包装器实现,这样你就可以为这个任务定义触发器信息。使用 TimerFactoryBean类,你可以让Spring使用配置创建触发器,并为一…

Spring对Timer的支持的核心是由ScheduledTimerTask和TimerFactoryBean类组成的。 ScheduledTimerTask类是对TimerTask的包装器实现,这样你就可以为这个任务定义触发器信息。使用 TimerFactoryBean类,你可以让Spring使用配置创建触发器,并为一组指定的ScheduledTimerTask bean自动创建Timer实例。

1、自定义timerTask,比如:定时输出ServletContext中的信息,本例中输出项目的绝对路径(比如:D:\software\apache-tomcat-6.0.33\webapps\spring\)

Java代码 spring中集成TimerTask执行定时任务spring中集成TimerTask执行定时任务spring中集成TimerTask执行定时任务
  1. publicclassBirthdayReminderTimerTaskextendsTimerTaskimplementsServletContextAware{
  2. /*通过实现ServletContextAware可获得servletContext*/
  3. privateServletContextservletContext;
  4. privatestaticLoggerlogger=Logger.getLogger(BirthdayReminderTimerTask.class);
  5. @Override
  6. publicvoidrun(){
  7. //logger.debug("BirthdayReminderTimerTaskisrunning");
  8. setServletContext(servletContext);
  9. try{
  10. System.out.println(this.servletContext.getRealPath("/"));
  11. }catch(Exceptione){
  12. e.printStackTrace();
  13. }
  14. }
  15. publicvoidsetServletContext(ServletContextservletContext){
  16. this.servletContext=servletContext;
  17. }
  18. }
public class BirthdayReminderTimerTask extends TimerTask implements ServletContextAware{/*通过实现ServletContextAware可获得servletContext*/private ServletContext servletContext;private static Logger logger = Logger.getLogger(BirthdayReminderTimerTask.class);@Overridepublic 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;    }
}

2、在spring的bean配置文件中配置,当系统加载该文件时,配置的定时器将自动启动,同时被spring管理。

Xml代码 spring中集成TimerTask执行定时任务spring中集成TimerTask执行定时任务spring中集成TimerTask执行定时任务
  1. <!--自定义任务-->
  2. <beanid="birthdayReminder"class="com.jep.task.BirthdayReminderTimerTask"></bean>
  3. <!--ScheduledTimerTask类是对TimerTask的包装器实现,这样你就可以为这个任务定义触发器信息。-->
  4. <beanid="birthdayReminderTimerTask"
  5. class="org.springframework.scheduling.timer.ScheduledTimerTask">
  6. <!--设置启动延迟-->
  7. <propertyname="delay">
  8. <value>3000</value>
  9. </property>
  10. <!--后续延迟-->
  11. <propertyname="period">
  12. <value>5000</value>
  13. </property>
  14. <!--指定触发器信息-->
  15. <propertyname="timerTask">
  16. <reflocal="birthdayReminder"/>
  17. </property>
  18. </bean>
  19. <!--使用TimerFactoryBean类,你可以让Spring使用配置创建触发器,并为一组指定的ScheduledTimerTaskbean自动创建Timer实例。-->
  20. <beanid="timerFactory"
  21. class="org.springframework.scheduling.timer.TimerFactoryBean">
  22. <propertyname="scheduledTimerTasks">
  23. <list>
  24. <reflocal="birthdayReminderTimerTask"/>
  25. </list>
  26. </property>
  27. </bean>
    <!--自定义任务-->    <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中进行如下配置

Xml代码 spring中集成TimerTask执行定时任务spring中集成TimerTask执行定时任务spring中集成TimerTask执行定时任务
  1. <!--SpringApplicationContext配置文件的路径此参数用于后面的Spring-Contextloader-->
  2. <context-param>
  3. <param-name>contextConfigLocation</param-name>
  4. <param-value>classpath:beans.xml</param-value>
  5. </context-param>
  6. <!--SpringApplicationContext载入-->
  7. <listener>
  8. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  9. </listener>
http://www.lbrq.cn/news/2551303.html

相关文章:

  • 网络推广方案总结沧州seo公司
  • 如何管理wordpress网站模板推广怎么推
  • 专业做淘宝网站公司哪家好google关键词挖掘工具
  • 重庆勘察设计协会网站百度一下首页官网下载
  • wordpress日期格式深圳优化服务
  • 音乐网站页面设计seo优化关键词放多少合适
  • dede网站迁移步骤seo推广专员工作好做吗
  • vps做网站教程免费域名解析网站
  • 张家界网站建设青岛百度推广优化
  • 小企业网站建设哪找什么是seo优化
  • 养殖网站模版百度链接收录
  • Wordpress修改主题默认头像seo工具
  • 洋洋点建站搜索引擎关键词的工具
  • 个人网站备案 网站服务内容百度客户电话
  • 做淘宝客要有网站吗成都官网seo服务
  • 电子产品网站建设策划查询网站备案信息
  • 站网站推广百度app怎么找人工客服
  • 西安做网站设计的公司近三天重大新闻摘抄
  • 萍乡网站建设行吗世界排名前十位
  • 我公司让别人做网站了怎么办google seo实战教程
  • 潍坊公司网站制作seo怎么优化排名
  • 网页小游戏入口搜索关键词优化服务
  • 怎么做卡盟网站西安网站建设推广
  • 培训学校设计sem优化策略
  • 建网站需要注册公司吗seo营销是什么意思
  • 营销型网站建设应该注意什么seo常规优化
  • 做平台销售网站引流黑科技app
  • vs2008怎么做网站微商软文范例大全100
  • 类似淘宝的网站怎么做的百度广告安装入口
  • 如何做企业网站营销怎么搭建属于自己的网站
  • 预测性维护之温振传感器选型与应用秘籍
  • AJAX快速入门 - 四个核心步骤
  • LeetCode 85:最大矩形
  • macOS安装配置Unbound DNS完整指南
  • docker 安装elasticsearch
  • 工厂方法模式:从基础到C++实现