由于项目需求:每隔一段时间就要调外部接口去进行某些操作,于是在网上找了一些资料,用了半天时间弄好了,代码:

 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的更多相关文章

  1. Android 中执行定时任务 Timer + TimerTask

    1. new Timer().schedule(new TimerTask() { @Override public void run() { //任务代码 } }, 0, 5000);

  2. Servlet的init()方法如何才会在服务器启动时执行

    如果要想让 servlet 的 init () 方法在服务器启动 时就被执行,则需要在 web.xml 中相应的 servlet 下配置 <servlet > <servlet -n ...

  3. 线程 Timer TimerTask 计时器 定时任务 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  4. java中服务器启动时,执行定时任务

    package com.ripsoft.util; import java.util.Calendar; import java.util.Timer; import javax.servlet.Se ...

  5. 使用Timer和ScheduledThreadPoolExecutor执行定时任务

    Java使用Timer和ScheduledThreadPoolExecutor执行定时任务 定时任务是在指定时间执行程序,或周期性执行计划任务.Java中实现定时任务的方法有很多,主要JDK自带的一些 ...

  6. Java定时任务Timer、TimerTask与ScheduledThreadPoolExecutor详解

     定时任务就是在指定时间执行程序,或周期性执行计划任务.Java中实现定时任务的方法有很多,本文从从JDK自带的一些方法来实现定时任务的需求. 一.Timer和TimerTask  Timer和Tim ...

  7. 使用Timer执行定时任务

    一.Timer概述 在Java开发中,会碰到一些需要定时或者延时执行某些任务的需求,这时,我们可以使用Java中的Timer类实现. 二.Timer介绍 Timer是一个定时器类,通过该类可以为指定的 ...

  8. Android开发之执行定时任务AlarmManager,Timer,Thread

    1.Thread:使用线程方式2.Timer是java的特性3.AlarmManager:AlarmManager将应用与服务分割开来后,使得应用程序开发者不用 关心具体的服务,而是直接通过Alarm ...

  9. Timer&TimerTask原理分析

    转载地址,请珍惜作者的劳动成果,转载请注明出处:http://www.open-open.com/lib/view/open1337176725619.html 如果你使用Java语言进行开发,对于定 ...

随机推荐

  1. 一个很NB的404页面

    一个带彩蛋的 404 页面 不得不说这个程序猿很有才 前往404页面 触发方法 按住鼠标左键 在页面中心不停的画圈 就可以进入神奇的地方了

  2. 廖雪峰git教程学习笔记

    对git来说,没有消息就是最好的消息 使用 git init 把当前目录变为git仓库 要在仓库里加入文件,先在仓库目录新建这个文件后,比如新建一个文件xiaobai.txt,内容为: 在命令行里输入 ...

  3. 表格 - bootStrap4常用CSS笔记

    [表格标签]   <table> 定义一个表格 <thead> 表格表头 <tbody> 表格主体内容 <tr> 行 <th> 表头列 &l ...

  4. Netty源码分析第2章(NioEventLoop)---->第5节: 优化selector

    Netty源码分析第二章: NioEventLoop   第五节: 优化selector 在剖析selector轮询之前, 我们先讲解一下selector的创建过程 回顾之前的小节, 在创建NioEv ...

  5. 亚马逊如何变成 SOA(面向服务的架构)

    . 亚马逊公司不仅是世界最大的网络书店,还是世界最大的云服务商.它是怎么实现从电商到云商的转变呢? 一切都是CEO杰夫·贝索斯促成的,他对市场有着超乎常人的理解和预见. 2. 2000年前后,贝索斯有 ...

  6. EF三种编程方式详细图文教程(C#+EF)之Model First

    Model First Model First我们称之为“模型优先”,这里的模型指的是“ADO.NET Entity Framework Data Model”,此时你的应用并没有设计相关数据库,在V ...

  7. jsp九大内置对象之config 和 out

    jsp中config的作用是读取web.xml中的配置信息,一般在后台获取初始化的参数,jsp页面用的较少因为jsp属于表现层,一般是获取数据. jsp中的out对象是将内容放到缓冲区中然后显示出来

  8. 【CS231N】7、卷积神经网络

    一.疑问 1. assignments2 在代码文件FullyConnectedNets.ipynd 中,有代码如下: # Test the affine_forward function num_i ...

  9. 解决Shiro+SpringBoot自定义Filter不生效问题

    在SpringBoot+Shiro实现安全框架的时候,自定义扩展了一些Filter,并注册到ShiroFilter,但是运行的时候发现总是在ShiroFilter之前就进入了自定义Filter,结果当 ...

  10. Delphi判断字符串中是否包含汉字,并返回汉字位置

    //1,函数代码{判断字符串是否包含汉字// judgeStr:要判断的字符串//posInt:第一个汉字位置}function TForm2.IsHaveChinese(judgeStr: stri ...