package org.zyn.ws.listener;import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
public class BugXmlTimer {
public Timer timer;
public void timerStart(){
timer = new Timer();
Date datetime=new Date();
Date midnightDate=new Date(); SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
midnightDate = sdf2.parse(sdf1.format(datetime)+" 23:00:00");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long in=midnightDate.getTime()-datetime.getTime();
System.out.println("before task");
//立刻执行,然后每隔30s执行一次
timer.schedule(new BugXmlTimerTask(), 0,5000);
}
public void timerStop(){
if(timer!=null)
timer.cancel();
}
public static void main(String[] args){
BugXmlTimer myTimer=new BugXmlTimer();
// TODO Auto-generated method stub
myTimer.timerStart();
}
}package org.zyn.ws.listener;import java.util.TimerTask;public class BugXmlTimerTask extends TimerTask {
@Override
public void run() {
System.out.print("run task");
}
}package org.zyn.ws.listener;import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;public class MyTimerListener implements ServletContextListener {
private BugXmlTimer mytimer = new BugXmlTimer ();
public void contextInitialized(ServletContextEvent event) {
mytimer.timerStart();
} public void contextDestroyed(ServletContextEvent event) {
mytimer.timerStop();
}
}
这个是我在网上找的
有几个地方我不太明白 希望大家指点下
1. midnightDate = sdf2.parse(sdf1.format(datetime)+" 23:00:00"); 这句话代表什么?23点开始执行任务?
2.我在代码没有改变的情况下 在下午3点执行了一次
public static void main(String[] args){
BugXmlTimer myTimer=new BugXmlTimer();
// TODO Auto-generated method stub
myTimer.timerStart();
}
结果是
before task
run tas 然后梅5秒显示一次run tas
3.当我在WEB下部署时 加载监听器的时候
为什么每次都是调用2次?
结果显示
2011-8-21 15:24:58 org.apache.catalina.core.AprLifecycleListener init
信息: Loaded APR based Apache Tomcat Native library 1.1.20.
2011-8-21 15:24:58 org.apache.catalina.core.AprLifecycleListener init
信息: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
2011-8-21 15:24:59 org.apache.coyote.http11.Http11AprProtocol init
信息: Initializing Coyote HTTP/1.1 on http-8154
2011-8-21 15:24:59 org.apache.coyote.ajp.AjpAprProtocol init
信息: Initializing Coyote AJP/1.3 on ajp-8009
2011-8-21 15:24:59 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 703 ms
2011-8-21 15:24:59 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2011-8-21 15:24:59 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/5.5.33
2011-8-21 15:24:59 org.apache.catalina.core.StandardHost start
信息: XML validation disabled
before task
run task2011-8-21 15:24:59 org.apache.catalina.core.ApplicationContext log
信息: org.apache.webapp.balancer.BalancerFilter: init(): ruleChain: [org.apache.webapp.balancer.RuleChain: [org.apache.webapp.balancer.rules.URLStringMatchRule: Target string: News / Redirect URL: http://www.cnn.com], [org.apache.webapp.balancer.rules.RequestParameterRule: Target param name: paramName / Target param value: paramValue / Redirect URL: http://www.yahoo.com], [org.apache.webapp.balancer.rules.AcceptEverythingRule: Redirect URL: http://jakarta.apache.org]]
2011-8-21 15:24:59 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: contextInitialized()
2011-8-21 15:24:59 org.apache.catalina.core.ApplicationContext log
信息: SessionListener: contextInitialized()
2011-8-21 15:24:59 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: contextInitialized()
2011-8-21 15:24:59 org.apache.catalina.core.ApplicationContext log
信息: SessionListener: contextInitialized()
before task
run task2011-8-21 15:25:00 org.apache.coyote.http11.Http11AprProtocol start
信息: Starting Coyote HTTP/1.1 on http-8154
2011-8-21 15:25:00 org.apache.coyote.ajp.AjpAprProtocol start
信息: Starting Coyote AJP/1.3 on ajp-8009
2011-8-21 15:25:00 org.apache.catalina.storeconfig.StoreLoader load
信息: Find registry server-registry.xml at classpath resource
2011-8-21 15:25:00 org.apache.catalina.startup.Catalina start
信息: Server startup in 1313 ms然后每5秒调用2次
结果显示run taskrun task run taskrun task run taskrun task .....其实我想实现的就是在每天23:59:59 这个时间调用1次方法 就结束 每天如此 希望高手指点
timer.schedule(new BugXmlTimerTask(), 0,60*60*24*1000);}
谢谢了
<job>
<job-detail>
<name>dd</name>
<group>DEFAULT</group>
<job-class>lee.TestJob</job-class>
</job-detail> <trigger>
<simple>
<name>dd</name>
<group>DEFAULT</group>
<job-name>dd</job-name>
<job-group>DEFAULT</job-group>
<start-time>2007-07-13 20:42:00 PM</start-time>
<repeat-count>1</repeat-count>
<repeat-interval>60000</repeat-interval>
</simple>
</trigger>
</job>
</quartz>大家受累给解释下几个参数的意思可以么
http://blog.csdn.net/liuyuhua0066/article/details/6600065
<quartz>
<job>
<job-detail>
<name>dd</name>
<group>DEFAULT</group>
<job-class>lee.TestJob</job-class>
</job-detail> <trigger>
<simple>
<name>dd</name>
<group>DEFAULT</group>
<job-name>dd</job-name>
<job-group>DEFAULT</job-group>
<start-time>2007-07-13 20:42:00 PM</start-time>
<repeat-count>1</repeat-count>
<repeat-interval>60000</repeat-interval>
</simple>
</trigger>
</job>
</quartz>
但是如何调用 servlet啊?
<property name="targetMethod">
<value>work</value>
</property> public void work() {
//Do something
} 可以在work方法里调用你的需要定时执行的方法。
ServletContext context=request.getSession().getServletContext();