服务器启动完成执行定时任务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语言进行开发,对于定 ...
随机推荐
- selenium 基本常用操作
from selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChains #鼠标操作 ...
- Linux加密到K8S中
文件名字 test.conf 加密: base64 --wrap=0 aaa.conf 把得到的密钥填入配置文件当中即可
- 关于nodejs中遇到mysql默认8小时连接断开机制的终极简单解决方案
由于mysql默认8小时连接无访问,就会断开.为此查了一下资料,有同种比较简单的解决方案: 1. 增加 MySQL 的 wait_timeout 属性的值. 修改 /etc/mysql/my.cnf文 ...
- Linux命令之mount挂载
挂载概念 Linux中的根目录以外的文件要想被访问,需要将其“关联”到根目录下的某个目录来实现,这种关联操作就是“挂载”,这个目录就是“挂载点”,解除次关联关系的过程称之为“卸载”. 注意:“挂载点” ...
- Django之Models与ORM操作
一.models例子 from django.db import models class User(models.Model): """ 用户表 "" ...
- vue入门之单文件组件
介绍 在很多 Vue 项目中,我们使用 Vue.component 来定义全局组件,紧接着用 new Vue({ el: '#container '}) 在每个页面内指定一个容器元素. 这种方式在很多 ...
- CF 1095C Powers Of Two(二进制拆分)
A positive integer xx is called a power of two if it can be represented as x=2y, where y is a non-ne ...
- 【欢迎来怼】 Beta发布事后诸葛亮会议
队名:欢迎来怼 项目名称:博客园Android端APP 小组成员队长:田继平成员:李圆圆,葛美义,王伟东,姜珊,邵朔,阚博文 ————————————————————————————————————— ...
- iOS软件"一天八杯水“app开发过程
作为一个ios系统测试者和app外观设计者.我们首先要了解iOS系统的开发工具和资源.xcode和iOS sdk作为一个免费的开发环境值得我们去学习和了解.interface builder提供创建了 ...
- java项目 相对路径(本项目的地址)
File file=new File(""); String abspath=file.getAbsolutePath(); System.out.println(abspath) ...