banq,你好,我有一個急需解決的問題??

lvyg發表於2005-11-09
你好,banq,我寫了一個總的Timer,用來控制各分Timer進行靈活停啟以進行一定的操作,但各分Timer.cancel()後,這些分Timer仍然在反覆執行,也就是說Timer.cancel()沒有啟作用,請你幫我分析一下:
-------
原始碼:

package new97.sp.util.inter;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletException;

import net.sf.hibernate.HibernateException;
import new97.pub.util.HibernateSession;
import new97.pub.util.HibernateSessionException;
import new97.sp.util.inter.SPGetBillDataFromLongShine;
import new97.sp.util.inter.SPJKGwlIntelTimer;
import new97.sp.util.inter.SPRealScanCNIP;
import new97.sp.util.inter.SPRealScanDSLAM;
import new97.sp.util.inter.SPRealScanIPSX;
import new97.sp.util.inter.SPRealScanJHWG;
import new97.sp.util.inter.SPRealScanPHSZNW;
import new97.sp.util.inter.SPRealScanSMTP;
import new97.sp.util.inter.SPSendBillDataToNet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;

public class SPTimerControl implements PlugIn {

Log log = LogFactory.getLog(SPTimerControl.class);

private static String[] initOF = { "R", "R", "R", "R", "R", "R", "R", "R",
"R" };

private static String[] timers = { "timerDSLAM", "timerJHWG", "timerCNIP",
"timerSMTP", "timerGI", "timerBillDataShine", "timerIPSX",
"timerPHS", "timerBillDataNet" };

private static String[] tasks = { "SPRealScanDSLAM", "SPRealScanJHWG",
"SPRealScanCNIP", "SPRealScanSMTP", "SPJKGwlIntelTimer",
"SPGetBillDataFromLongShine", "SPRealScanIPSX", "SPRealScanPHSZNW",
"SPSendBillDataToNet" };

private static int delay = 10000;

private static int period = 60000;

private Timer controlTimer;

private String[] runableOF;

private String[] runableNF;

private Timer timerCNIP;

private Timer timerSMTP;

private Timer timerJHWG;

private Timer timerDSLAM;

private Timer timerGI;

private Timer timerBillDataShine;

private Timer timerIPSX;

private Timer timerPHS;

private Timer timerBillDataNet;

private SPRealScanCNIP realScanCNIP;

private SPRealScanSMTP realScanSMTP;

private SPRealScanJHWG realScanJHWG;

private SPRealScanDSLAM realScanDSLAM;

private SPJKGwlIntelTimer realScanGI;

private SPGetBillDataFromLongShine realScanBDS;

private SPRealScanIPSX realScanIPSX;

private SPRealScanPHSZNW realScanPHS;

private SPSendBillDataToNet realScanBDN;

private String control;

public SPTimerControl() {
this.setRunableOF(initOF);
this.setControl(timerConfig());
}

public void init(ActionServlet servlet, ModuleConfig config)
throws ServletException {
log.debug("Timer Controller Is Load...");

if (!"".equals(this.getControl())) {
if ("Y".equals(this.getControl())) {
log.debug("Timer Is Run,Run Status...");
timerDSLAM = new Timer();
realScanDSLAM = new SPRealScanDSLAM();
timerDSLAM.scheduleAtFixedRate(realScanDSLAM, delay, period);
timerJHWG = new Timer();
realScanJHWG = new SPRealScanJHWG();
timerJHWG.scheduleAtFixedRate(realScanJHWG, delay, period);
timerCNIP = new Timer();
realScanCNIP = new SPRealScanCNIP();
timerCNIP.scheduleAtFixedRate(realScanCNIP, delay, period);
timerSMTP = new Timer();
realScanSMTP = new SPRealScanSMTP();
timerSMTP.scheduleAtFixedRate(realScanSMTP, delay, period);
timerGI = new Timer();
realScanGI = new SPJKGwlIntelTimer();
timerGI.scheduleAtFixedRate(realScanGI, delay, period);
timerBillDataShine = new Timer();
realScanBDS = new SPGetBillDataFromLongShine();
timerBillDataShine.scheduleAtFixedRate(realScanBDS, delay,
period);
timerIPSX = new Timer();
realScanIPSX = new SPRealScanIPSX();
timerIPSX.scheduleAtFixedRate(realScanIPSX, delay, period);
timerPHS = new Timer();
realScanPHS = new SPRealScanPHSZNW();
timerPHS.scheduleAtFixedRate(realScanPHS, delay, period);
timerBillDataNet = new Timer();
realScanBDN = new SPSendBillDataToNet();
timerBillDataNet
.scheduleAtFixedRate(realScanBDN, delay, period);
} else {
log.debug("Timer Is Stop,Stop Status...");
}
}
controlTimer = new Timer();
TimerTask controlTimerTask = new TimerTask() {
SPTimerControl timerControl = new SPTimerControl();

public void run() {
log.debug("Timer Controll Task Is Run...");
String controlFlag = "";
String ctrl = "";
String[] switchFlag = new String[initOF.length];
Connection connection = null;
Statement stmt = null;
ResultSet rs = null;

try {
connection = HibernateSession.openSession(100).connection();
} catch (HibernateSessionException hse) {
log.debug("Create Hibernate Session Fail...");
hse.printStackTrace();
} catch (HibernateException he) {
log.debug("Create Hibernate Session Fail...");
he.printStackTrace();
}

String sql = "select a.cur_value from sys_config a where a.config_id=22086";
try {
stmt = connection.createStatement();
rs = stmt.executeQuery(sql);
if (rs.next()) {
controlFlag = rs.getString(1);
ctrl = controlFlag.substring(0, 1);
controlFlag = controlFlag.substring(1, controlFlag
.length());
}
} catch (SQLException se) {
log.debug("Query Control Config Fail...");
se.printStackTrace();
}

for (int i = 0; i < controlFlag.length(); i++) {
if ("1".equals(controlFlag.substring(i, i + 1))) {
switchFlag = "R";
}
if ("0".equals(controlFlag.substring(i, i + 1))) {
switchFlag = "S";
}
}

timerControl.setRunableNF(switchFlag);

if (!"".equals(controlFlag)) {
timerControl.getRunableOF()[0] = (processTimer(ctrl,
timerDSLAM, realScanDSLAM, timers[0], tasks[0],
timerControl.getRunableNF()[0], timerControl
.getRunableOF()[0]));
timerControl.getRunableOF()[1] = (processTimer(ctrl,
timerJHWG, realScanJHWG, timers[1], tasks[1],
timerControl.getRunableNF()[1], timerControl
.getRunableOF()[1]));
timerControl.getRunableOF()[2] = (processTimer(ctrl,
timerCNIP, realScanCNIP, timers[2], tasks[2],
timerControl.getRunableNF()[2], timerControl
.getRunableOF()[2]));
timerControl.getRunableOF()[3] = (processTimer(ctrl,
timerSMTP, realScanSMTP, timers[3], tasks[3],
timerControl.getRunableNF()[3], timerControl
.getRunableOF()[3]));
timerControl.getRunableOF()[4] = (processTimer(ctrl,
timerGI, realScanGI, timers[4], tasks[4],
timerControl.getRunableNF()[4], timerControl
.getRunableOF()[4]));
timerControl.getRunableOF()[5] = (processTimer(ctrl,
timerBillDataShine, realScanBDS, timers[5],
tasks[5], timerControl.getRunableNF()[5],
timerControl.getRunableOF()[5]));
timerControl.getRunableOF()[6] = (processTimer(ctrl,
timerIPSX, realScanIPSX, timers[6], tasks[6],
timerControl.getRunableNF()[6], timerControl
.getRunableOF()[6]));
timerControl.getRunableOF()[7] = (processTimer(ctrl,
timerPHS, realScanPHS, timers[7], tasks[7],
timerControl.getRunableNF()[7], timerControl
.getRunableOF()[7]));
timerControl.getRunableOF()[8] = (processTimer(ctrl,
timerBillDataNet, realScanBDN, timers[8], tasks[8],
timerControl.getRunableNF()[8], timerControl
.getRunableOF()[8]));
} else {
log.debug("Not Found Control Config,Use Default Config...");
}

// finally{
try {
stmt.close();
rs.close();
connection.close();
HibernateSession.closeSession(100);
} catch (HibernateSessionException hse) {
log.debug("Close Session Faile...");
} catch (SQLException se) {
log.debug("Close Connection Faile...");
}
// }

}
};
controlTimer.scheduleAtFixedRate(controlTimerTask, 60000, 60000);
}

private String processTimer(String control, Timer timer, TimerTask task,
String timerClassName, String taskClassName, String newStatus,
String oldStatus) {

if ("1".equals(control)) {
if (newStatus.equals(oldStatus)) {
log.debug("Timer Not Change,Continue Status...");
}
if (newStatus.equals("R")) {
log.debug("Timer Is Change, Change Status...");
log.debug(timerClassName + " Timer Is Running...");
timer = buildTimer(timerClassName);
task = buildTask(taskClassName);
timer.scheduleAtFixedRate(task, 10000, 60000);
oldStatus = newStatus;
}
if (newStatus.equals("S")) {
log.debug("Timer Is Change, Change Status...");
try {
timer.cancel();

log.debug(timerClassName + " Timer Is Stopping...");
task.cancel();
log.debug(timerClassName + " Task Is Stopping...");
} catch (Exception e) {
log.debug("Stop Timer Is Error...");
log.debug("Stop Task Is Error...");
}
oldStatus = newStatus;
}
} else {
try {
timer.cancel();
log.debug(timerClassName + " Timer Is Stopping...");
task.cancel();
log.debug(timerClassName + " Task Is Stopping...");
} catch (Exception e) {
log.debug("Stop Timer Is Error...");
log.debug("Stop Task Is Error...");
}
}

return oldStatus;
}

private TimerTask buildTask(String taskName) {
TimerTask task = null;
try {
Class cla = Class.forName("new97.sp.util.inter." + taskName);
task = (TimerTask) cla.newInstance();
} catch (ClassNotFoundException cnfe) {
log.debug("Task Class Not Found...");
} catch (InstantiationException ie) {
log.debug("Task Class Not Found...");
} catch (IllegalAccessException ae) {
log.debug("Task Class Not Found...");
}

return task;
}

private Timer buildTimer(String timerName) {
Timer timer = new Timer();
return timer;
}

private String timerConfig() {
Connection conn = null;
Statement statement = null;
ResultSet resultSet = null;
try {
conn = HibernateSession.openSession(1).connection();
} catch (HibernateSessionException hse) {
log.debug("Create Hibernate Session Fail...");
hse.printStackTrace();
} catch (HibernateException he) {
log.debug("Create Hibernate Session Fail...");
he.printStackTrace();
}
String sql = "select a.cur_value from sys_config a where a.config_id=22086";
try {
statement = conn.createStatement();
resultSet = statement.executeQuery(sql);
if (resultSet.next()) {
String ctrl = resultSet.getString(1);
if (ctrl != null && !"".equals(ctrl)) {
if ("1".equals(ctrl.substring(0, 1))) {
return "Y";
}
if ("0".equals(ctrl.substring(0, 1))) {
return "N";
}
}
}
} catch (SQLException se) {
log.debug("Query Control Config Fail...");
se.printStackTrace();
} finally {
try {
statement.close();
resultSet.close();
conn.close();
HibernateSession.closeSession(1);
} catch (HibernateSessionException hse) {
log.debug("Close Session Faile...");
} catch (SQLException se) {
log.debug("Close Connection Faile...");
}
}
return "N";
}

public void destroy() {
if (controlTimer != null) {
controlTimer.cancel();
log.debug("Timer Controll Task Is Stop...");
log.debug("Timer Controller Is Unload...");
}
}

/**
* @return Returns the runableNF.
*/
public String[] getRunableNF() {
return runableNF;
}

/**
* @param runableNF
* The runableNF to set.
*/
public void setRunableNF(String[] runableNF) {
this.runableNF = runableNF;
}

/**
* @return Returns the runableOF.
*/
public String[] getRunableOF() {
return runableOF;
}

/**
* @param runableOF
* The runableOF to set.
*/
public void setRunableOF(String[] runableOF) {
this.runableOF = runableOF;
}

/**
* @return Returns the control.
*/
public String getControl() {
return control;
}

/**
* @param control
* The control to set.
*/
public void setControl(String control) {
this.control = control;
}

}

相關文章