日期:2014-05-19 浏览次数:20921 次
package com;
import java.util.Date;
public class YouObject
{
public void youMethod()
{
System.out.println(new Date()+":每隔一分钟执行一次");
}
}
------解决方案--------------------
4,web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/*.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- acegi -->
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
</web-app>
------解决方案--------------------
首先得告诉楼主,你想要实现的效果是定时执行任务。
你在web.xml 中配置
<listener>
<listener-class>com.fastunit.samples.listener.MyListener</listener-class>
</listener>
表示你启动web 服务器的时候,会实例化对象com.fastunit.samples.listener.MyListener,并且调用
public void contextInitialized(ServletContextEvent event) {
? timer = new Timer(true);
? //设置任务计划,启动和间隔时间
? timer.schedule(new MyTask(), 0, 86400000);
? }
这个方法的代码。
如果你想实现同样的效果的话,只需要在写个一main 方法在里面,并且创建com.fastunit.samples.listener.MyListener对象,然后调用contextInitialized(null)方法即可。
------解决方案--------------------
spring定时器:http://apps.hi.baidu.com/share/detail/31761760