标签:
package scheduleTest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class scheduleTest2 implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0) {
}
@Override
/**
* 启动项目的时候调用
* @author:tuzongxun
* @Title: contextInitialized
* @Description: TODO
* @param @param arg0
* @date Jun 3, 2016 12:33:36 PM
* @throws
*/
public void contextInitialized(ServletContextEvent arg0) {
this.startTask();
}
/**
* 启动方法
*
* @author:tuzongxun
* @Title: startTask
* @param
* @return void
* @date Jun 3, 2016 12:34:23 PM
* @throws
*/
public void startTask() {
Timer timer = new Timer();
timer.schedule(new Sche(), 0, 5000);
}
/**
* 定时器类
*
* @author tuzongxun123
*
*/
class Sche extends TimerTask {
@Override
public void run() {
Date date = new Date();
SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = sim.format(date);
System.out.println("这是java定时器3,每五秒执行一次,当前时间:" + dateStr);
}
}
}<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>appversion</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring2.xml</param-value>
</context-param>
<listener>
<description>spring监听器</description>
<listener-class>scheduleTest.scheduleTest2</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:task="http://www.springframework.org/schema/task"
xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd">
</beans>
标签:
原文地址:http://blog.csdn.net/tuzongxun/article/details/51578044