紧急求助,Listener监听器问题
在作一个项目的时候,由于系统需要在后台定时的自动的向远程服务器请求数据来同步本地数据库,项目采用的技术主要是hibernate,因此在作的时候采用了HttpSessionListener监听器,并使用Timer作为定时执行,远程请求方法也使用了同步,但是现在遇到一个问题,如果Timer的参数通过配置文件改变后,怎样可以在避免重启服务器的情况下使Timer使用新的参数来运行?
程序代码:
//监听器
public class BankUpdateListener extends DbBase implements HttpSessionListener {
LogUtil log= LogFactory.getLogUtil(BankUpdateListener.class);
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
BankTimer.start();
}
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
//To change body of implemented methods use File | Settings | File Templates.
}
}
//定时器
public class BankTimer extends DbBase {
static LogUtil log = LogFactory.getLogUtil(BankTimer.class);
private static final String CONFIG_URL="/bank/bank-config.properties";
private static PropUtil pu;
private static Timer timer=null;//定时器
private static int deplay;//延迟时间
private static int internal;//执行时间间隔
private static String curDate;//当前日期
public BankTimer() {
}
/**
* 时间定时器
*/
private static TimerTask task = new TimerTask() {
synchronized public void run() {
System.out.println("定时器启动!"+System.currentTimeMillis());
}
};
/**
* 无延迟定时器启动
*
* @param internal
*/
public static void start(int internal) {
timer.schedule(task, 0, internal);
}
/**
* 延迟一定时候后定时器启动
*
* @param delay
* @param internal
*/
public static void start(int delay, int internal) {
timer.schedule(task, delay, internal);
}
/**
* 定时器启动
*/
public static void start() {
init();
timer.schedule(task, deplay, internal);
}
/**
* 定时器停止
*/
public static void stop(){
timer.cancel();
}
private static void init() {
timer = new Timer();
Date now=new Date();
DateFormat df=DateFormat.getDateInstance(DateFormat.DEFAULT);
curDate=df.format(now);
try {
pu = PropUtil.getInstance(CONFIG_URL);
internal=Integer.parseInt(pu.getProperty("internal"));
deplay=Integer.parseInt(pu.getProperty("deplay"));
} catch (Exception e) {
deplay = 5000;//延迟5秒后执行
internal = 2000;//默认间隔时间为30分钟
log.error(e.getMessage());
}
}
}