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

 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. Spring Cloud限流思路及解决方案

    转自: http://blog.csdn.net/zl1zl2zl3/article/details/78683855 在高并发的应用中,限流往往是一个绕不开的话题.本文详细探讨在Spring Clo ...

  2. ajax 异步刷新

    第一种方法,ajax实现:当然,ajax使用起来确实很简单就可以实现,但是里面的很多知识还是比较有点深的.我之前做页面时间自动刷新的功能就是用的ajax.完整代码是:1.getTime.php: 复制 ...

  3. css修改input自动提示的黄色背景

    css修改input自动提示的黄色背景 input:-webkit-autofill { background-color: #FAFFBD; background-image: none; -web ...

  4. 20135337朱荟潼Java实验报告二

    20135337朱荟潼 实验二 Java面向对象程序设计 一.实验内容 1. 初步掌握单元测试和TDD 2. 理解并掌握面向对象三要素:封装.继承.多态 3. 初步掌握UML建模 4. 熟悉S.O.L ...

  5. 每日scrum(1)

    今天又正式开始了第二个冲刺周期,计划十天,主要需要改进的地方包括UI界面,还有一些细节的把握. 今天出现的主要问题有:在讨论UI界面风格的时候,小组内部意见不统一,对UI界面的创作流程不熟悉,以及难度 ...

  6. rua出300道四则运算题

  7. 001_JavaWeb之Servlet的路径映射问题

    001_JavaWeb之Servlet的路径映射问题 在web.xml中写入: <servlet> <servlet-name>DeleteStudent</servle ...

  8. 16_常用API_第16天(正则表达式、Date、DateFormat、Calendar)_讲义

    今日内容介绍 1.正则表达式的定义及使用 2.Date类的用法 3.Calendar类的用法 ==========================================第一阶段======= ...

  9. ABP ModuleZero 添加角色_创建角色是报错的问题解决方案

    ABP升级后,大概3.6.1以后,在.net framework里面,从官方下载的Module zero模板添加角色是出现下面错误. 这个问题,算是新版apb的一点小缺憾,好像在.net core的模 ...

  10. AWS EC2安装docker时的问题

    在AWS EC2的实例(Ubuntu)里面安装docker时,使用通常的安装步骤 :~$ sudo apt-get update :~$ sudo apt-get install docker 安装完 ...