📄 threadmanager.java
字号:
package com.trulytech.mantis.thread;
import java.lang.reflect.*;
import java.sql.*;
import java.util.*;
import com.trulytech.mantis.system.*;
import com.trulytech.mantis.system.Properties;
/**
*
* <p>Title: Mantis</p>
*
* <p>Description: 后台线程控制类</p>
*
* <p>Copyright: Copyright (c) 2002</p>
*
* <p>Company: </p>
*
* @author Wang Xian
* @version 1.0
*/
public class ThreadManager
extends Thread {
//是否初始化
public static boolean isInited = false;
//数据库连接
private Connection conn = null;
//存放每个方法启动时间的hashmap
private HashMap map = null;
//是否已经提交
private boolean isCommit = false;
//当前时间
private long currtime = 0;
//反射参数对象
Object[] obj = null;
//是否停止线程
private boolean isstop = false;
/**
* 构造函数
*/
public ThreadManager() {
if (isInited)
return;
try {
this.conn = ConnManager.getConnection();
conn.setAutoCommit(false);
map = new HashMap();
isInited = true;
currtime = System.currentTimeMillis();
obj = new Object[2];
logWriter.Info("线程启动成功");
}
catch (Exception ex) {
logWriter.Error(ex.toString() + " (" + this.getClass().getName() +
")");
}
}
//运行线程
public void run() {
while (!isstop) { //如果线程没有终止
try {
currtime = System.currentTimeMillis();
//执行ThreadProcess
Class c = Class.forName(Properties.ThreadProcessClass);
//查找指定类的所有方法
Method m[] = c.getDeclaredMethods();
for (int i = 0; i < m.length; i++) {
if (m[i].getName().length() > 2)
if (m[i].getName().substring(0, 2).equalsIgnoreCase("do")) {
if (m[i].getParameterTypes().length == 2) {
//构造反射方法
if (m[i].getParameterTypes()[0].getName().equalsIgnoreCase(
"java.lang.Long")
&&
m[i].getParameterTypes()[1].getName().equalsIgnoreCase(
"java.lang.Long")
) {
if (map.get(m[i].getName()) == null) {
obj[0] = new Long(0);
obj[1] = new Long(currtime);
}
else {
obj[0] = map.get(m[i].getName());
obj[1] = new Long(currtime);
}
isCommit = false;
//执行方法成功
try {
if ( ( (Boolean) m[i].invoke(this, obj)).booleanValue()) {
conn.commit();
map.put(m[i].getName(), new Long(currtime));
isCommit = true;
logWriter.Info("线程执行完毕" + " (" + m[i].getName() + ")");
}
}
catch (Exception e) {
map.put(m[i].getName(), new Long(currtime));
try {
if (!isCommit)
conn.rollback();
}
catch (SQLException ex) {}
if (e.getCause() != null) {
e = new Exception(e.getCause());
logWriter.Error(e.toString() + " (" + m[i].getName() +
")");
}
else
logWriter.Error("后台线程 " + e.toString());
}
obj[0] = null;
obj[1] = null;
}
}
}
}
}
catch (ClassNotFoundException e) {
logWriter.Error(e.toString() + " (" + this.getClass().getName() +
")");
}
finally {
try {
sleep(Properties.ThreadInterval);
}
catch (InterruptedException e) {
//如果终止
if (conn != null)
try {
ConnManager.closeConnection(conn);
logWriter.Info("线程终止成功");
try {
sleep(Properties.ThreadInterval);
}
catch (Exception ex) {}
}
catch (Exception ex) {
logWriter.Error("线程终止失败-" + ex.toString());
}
}
}
}
}
/**
* 获得数据库连接
* @return Connection
*/
public Connection getConn() {
return conn;
}
/**
* 停止线程
*/
public synchronized void setstop() {
this.isstop = true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -