服务器启动完成执行定时任务Timer,TimerTask
由于项目需求:每隔一段时间就要调外部接口去进行某些操作,于是在网上找了一些资料,用了半天时间弄好了,代码:
- import java.util.TimerTask;
- public class AccountTask extends TimerTask {
- @Override
- public void run() {
- System.out.prinln("开始执行定时任务业务");
- }
- }
- import java.util.Timer;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
- public class AccountTimerListener implements ServletContextListener {
- private Timer timer = null;
- @Override
- public void contextInitialized(ServletContextEvent event) {
- timer = new Timer(true);
- event.getServletContext().log("定时器已启动");
- // 服务器启动后,延迟7秒启动,5秒执行一次
- timer.scheduleAtFixedRate(new AccountTask(), 7 * 1000, 5 * 1000);
- }
- @Override
- public void contextDestroyed(ServletContextEvent event) {
- if (timer != null) {
- timer.cancel();
- event.getServletContext().log("定时器销毁");
- }
- }
- }
然后在web.xml文件中配置监听
- <listener>
- <listener-class>com.xxx.AccountTimerListener</listener-class>
- </listener>
启动之后,会发现没隔5秒打印一次: 开始执行定时任务业务 。
然而,当调度类中调用service层业务时,启动tomcat后,执行定时任务时会报空指针异常,这是由于这个定时任务目前只是一个普通的类,我们需要将这个定时任务注入到spring中,监听。
解决方案如下:
- package com.test.utils;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
- import org.springframework.context.ApplicationContext;
- import org.springframework.web.context.WebApplicationContext;
- import org.springframework.web.context.support.WebApplicationContextUtils;
- public class SpringInit implements ServletContextListener {
- private static WebApplicationContext springContext;
- public SpringInit() {
- super();
- }
- @Override
- public void contextDestroyed(ServletContextEvent event) {
- // TODO Auto-generated method stub
- }
- @Override
- public void contextInitialized(ServletContextEvent event) {
- springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
- }
- public static ApplicationContext getApplicationContext() {
- return springContext;
- }
- }
web.xml文件:
- <!-- SpringInit类所在的包 -->
- <context:component-scan base-package="com.test.utils" />
- <listener>
- <listener-class>com.test.utils.SpringInit</listener-class>
- </listener>
若 context:component-scan 出报错,可能是因为没有引入标签。
在xmlns:xsi 里加上
- http://www.springmodules.org/schema/cache/springmodules-cache.xsd http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd
xsi:schemaLocation里加上
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
上面的问题解决。
最后,我们掉用service之前,这样来获取bean:
- DetailService detailService = (DetailService) SpringInit.getApplicationContext().getBean("detailService");
然后就可以掉用service层业务进行定时任务了。
服务器启动完成执行定时任务Timer,TimerTask的更多相关文章
- Android 中执行定时任务 Timer + TimerTask
1. new Timer().schedule(new TimerTask() { @Override public void run() { //任务代码 } }, 0, 5000);
- Servlet的init()方法如何才会在服务器启动时执行
如果要想让 servlet 的 init () 方法在服务器启动 时就被执行,则需要在 web.xml 中相应的 servlet 下配置 <servlet > <servlet -n ...
- 线程 Timer TimerTask 计时器 定时任务 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- java中服务器启动时,执行定时任务
package com.ripsoft.util; import java.util.Calendar; import java.util.Timer; import javax.servlet.Se ...
- 使用Timer和ScheduledThreadPoolExecutor执行定时任务
Java使用Timer和ScheduledThreadPoolExecutor执行定时任务 定时任务是在指定时间执行程序,或周期性执行计划任务.Java中实现定时任务的方法有很多,主要JDK自带的一些 ...
- Java定时任务Timer、TimerTask与ScheduledThreadPoolExecutor详解
定时任务就是在指定时间执行程序,或周期性执行计划任务.Java中实现定时任务的方法有很多,本文从从JDK自带的一些方法来实现定时任务的需求. 一.Timer和TimerTask Timer和Tim ...
- 使用Timer执行定时任务
一.Timer概述 在Java开发中,会碰到一些需要定时或者延时执行某些任务的需求,这时,我们可以使用Java中的Timer类实现. 二.Timer介绍 Timer是一个定时器类,通过该类可以为指定的 ...
- Android开发之执行定时任务AlarmManager,Timer,Thread
1.Thread:使用线程方式2.Timer是java的特性3.AlarmManager:AlarmManager将应用与服务分割开来后,使得应用程序开发者不用 关心具体的服务,而是直接通过Alarm ...
- Timer&TimerTask原理分析
转载地址,请珍惜作者的劳动成果,转载请注明出处:http://www.open-open.com/lib/view/open1337176725619.html 如果你使用Java语言进行开发,对于定 ...
随机推荐
- Netty源码分析第5章(ByteBuf)---->第4节: PooledByteBufAllocator简述
Netty源码分析第五章: ByteBuf 第四节: PooledByteBufAllocator简述 上一小节简单介绍了ByteBufAllocator以及其子类UnPooledByteBufAll ...
- Qt tableWidget 空单元格 获取选中行行号
bool focus = tableWidget->isItemSelected(tableWidget->currentItem()); // 判断是否选中一行 Int row1 = t ...
- 精通Python爬虫-03-狩猎大师
声明: 本系列文章原创于慕课网,作者秋名山车神,任何人不得以任何形式在不经作者允许的情况下,进行任何形式的印刷以及销售,转载需注明出处及此声明. 本系列文章更新至少每周一更,将涉及Python爬虫基础 ...
- -lPods-NewsPushClientSDK is not an object file (not allowed in a library)
今天在给客户定制SDK的过程中,出现了下面的一个问题,具有代表性,现在记录下来 问题: Showing All Errors Only : /Applications/Xcode.app/Conten ...
- 【Oracle】存储过程在字符串单引号'内拼接单引号'
http://blog.csdn.net/u011704894/article/details/44976557 一般变量里面接3个单引号 eg: 'DELETE FROM RDM_SUPP_DATA ...
- 冲刺One之站立会议4 /2015-5-17
今天我们继续了昨天未完成的部分,把服务器端的在线人数显示做了出来,但是在调试的时候还有一些不可预知的自己也不会改的bug,让我们有点不知所措,启动时间的显示相对来说比较容易实现. 燃尽图4
- 蹭课神器NABCD分析
特点:添加了课程分类,同学们可以根据自己的兴趣蹭课N(need):众所周知,大学是丰富多彩的自有天堂,学生能够在课余去旁听一些自己有兴趣的课,我们项目要解 决的就是同学们对校园课程有针对性的选择.A( ...
- 《UML大战需求分析》-读后感三
用例图是用来描述什么角色通过某某系统能做什么的图,用例图关注的是系统的外在表示想爱你.系统与人的交互系统与其他系统的交互,小人执行者就是角色,角色 是对系统使用者的抽象,一个角色可以代表多个具体的人而 ...
- StringBuffer 与 StringBuilder类的使用
/*如果需要频繁修改字符串 的内容,建议使用字符串缓冲 类(StringBuffer). StringBuffer 其实就是一个存储字符 的容器. 笔试题目:使用Stringbuffer无 参的构造函 ...
- 汇编语言段和RSEG用法
RSEG是段选择指令,要想明白它的意思就要了解段的意思.段是程序代码或数据对象的存储单位.程序代码放到代码段,数据对象放到数据段.段分两种,一是绝对段,一是再定位段.绝对段在汇编语言中指定,在用L51 ...