最近小虾米我配置了个Tomcat的网站,其中设置了一个Listener,放置了两个Timer,其中一个timer_ExchangeRate一直很正常,但是timer_FondValue却会不定时死掉,不再执行任务。(timer_FondValue运行程序的时间会长于另一个TIMER)这究竟是什么原因导致的呢?有没有解决方案啊?Listener代码如下
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.test.GetExchangeRate;
import com.test.GetFondValue;//import com.publisherTest.GetFond;public class GetSourceTimer implements ServletContextListener {
public GetSourceTimer(){}
public long refresh_ExchangeRate = 60000;
public long refresh_FondValue = 180000;
/**
* 无延迟
*/
public static final long NO_DELAY = 0;
/**
* 定时器
*/
private Timer timer_ExchangeRate;
private Timer timer_FondValue;
//public static int i = 0;
/**
* 在Web应用启动时初始化任务
*/
public void contextInitialized(ServletContextEvent event) {
//定义定时器
//i++;
timer_ExchangeRate = new Timer("汇率刷新",true);
timer_FondValue = new Timer("净值刷新",true);
timer_ExchangeRate.schedule(new myTask(),NO_DELAY, refresh_ExchangeRate);
timer_FondValue.schedule(new FondTask(), NO_DELAY,refresh_FondValue);
}
/**
* 在Web应用结束时停止任务
*/
public void contextDestroyed(ServletContextEvent event) {
timer_ExchangeRate.cancel(); // 定时器销毁
timer_FondValue.cancel();
}
class myTask extends TimerTask{
private boolean isRunning = false;
//private static Log log = LogFactory.getLog(BackUpTableTask.class);
public void run() {
if (!isRunning) {
isRunning = true;
//更新汇率信息
GetExchangeRate getrate = new GetExchangeRate();
if(getrate.CheckIsNull()){getrate.iniExchangeRate();}
else{
if(!getrate.CheckExchangeTime()){
getrate.UpadateExchangeRate();
}
}
isRunning = false;
} else {
this.cancel();
}
}
}
class FondTask extends TimerTask{
private boolean isRunning = false;
//private static Log log = LogFactory.getLog(BackUpTableTask.class);
public void run() {
if (!isRunning) {
isRunning = true;
//更新汇率信息
GetFondValue getfond = new GetFondValue();
if (getfond.CheckIsNull()){getfond.iniFondValue();}
else{
if(!getfond.CheckExchangeTime()){
getfond.reload();
}
}
isRunning = false;
} else {
this.cancel();
}
}
}
}
跪求各位大大了!
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.test.GetExchangeRate;
import com.test.GetFondValue;//import com.publisherTest.GetFond;public class GetSourceTimer implements ServletContextListener {
public GetSourceTimer(){}
public long refresh_ExchangeRate = 60000;
public long refresh_FondValue = 180000;
/**
* 无延迟
*/
public static final long NO_DELAY = 0;
/**
* 定时器
*/
private Timer timer_ExchangeRate;
private Timer timer_FondValue;
//public static int i = 0;
/**
* 在Web应用启动时初始化任务
*/
public void contextInitialized(ServletContextEvent event) {
//定义定时器
//i++;
timer_ExchangeRate = new Timer("汇率刷新",true);
timer_FondValue = new Timer("净值刷新",true);
timer_ExchangeRate.schedule(new myTask(),NO_DELAY, refresh_ExchangeRate);
timer_FondValue.schedule(new FondTask(), NO_DELAY,refresh_FondValue);
}
/**
* 在Web应用结束时停止任务
*/
public void contextDestroyed(ServletContextEvent event) {
timer_ExchangeRate.cancel(); // 定时器销毁
timer_FondValue.cancel();
}
class myTask extends TimerTask{
private boolean isRunning = false;
//private static Log log = LogFactory.getLog(BackUpTableTask.class);
public void run() {
if (!isRunning) {
isRunning = true;
//更新汇率信息
GetExchangeRate getrate = new GetExchangeRate();
if(getrate.CheckIsNull()){getrate.iniExchangeRate();}
else{
if(!getrate.CheckExchangeTime()){
getrate.UpadateExchangeRate();
}
}
isRunning = false;
} else {
this.cancel();
}
}
}
class FondTask extends TimerTask{
private boolean isRunning = false;
//private static Log log = LogFactory.getLog(BackUpTableTask.class);
public void run() {
if (!isRunning) {
isRunning = true;
//更新汇率信息
GetFondValue getfond = new GetFondValue();
if (getfond.CheckIsNull()){getfond.iniFondValue();}
else{
if(!getfond.CheckExchangeTime()){
getfond.reload();
}
}
isRunning = false;
} else {
this.cancel();
}
}
}
}
跪求各位大大了!
} else {
this.cancel();
}所以,以后不再执行任务。