类的调用监控(原创)

     系统中一些主要的类,你可能需要去监控它,需要知道在什么地方用到了它的什么方法。比如对数据库连接类的监控,可能要监控何时打开连接,何时关闭了连接等;也可以用到项目的单元测试中;log4j的日志和jdk的Logging日志也是通过监控类实现的。
有三中方法可以做到:
1、用异常的堆栈。
2、用JDK1.4中的StackTraceElement类。
3、JDK1.5在Thread类里面引入了getStackTrace()和getAllStackTraces()两个方法。可以调用Thread.getCurrentThread().getStackTrace()来获得当前线程的运行栈信息。不仅如此,只要权限允许,还可以获得其它线程的运行栈信息。

/***********类的痕迹类****************/
public class MethodTrace {
// for jdk 1.4
private StackTraceElement elements[];
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw);
private String trueMethodName = null;
private String methodName = null;
private String methodNameExTest = null;
private String e = null;
private int a[] = new int[2];
private int b;
private String cutStr = "test";
private int cutLength = cutStr.length();
private char[] line = null;
private StringBuffer buf = new StringBuffer();

public MethodTrace() {
}

private String cutTestStr(String str) {
if (str == null) {
return null;
}
String strLowerCase = str.toLowerCase();
int i = strLowerCase.indexOf(cutStr);
int j;
if (i != -1) {
line = str.toCharArray();
buf.setLength(str.length());
buf.append(line, 0, i);
i += cutLength;
j = i;
i = strLowerCase.indexOf(cutStr, i);
while (i != -1) {
buf.append(line, j, i - j);
i += cutLength;
j = i;
i = strLowerCase.indexOf(cutStr, i);
}
buf.append(line, j, line.length - j);
str = buf.toString();
buf.setLength(0);
}
return str;
}

/**
* 得到应用该方法的类和方法
*
* @return
*/
public String getTrueMethod() {
trueMethodName = null;
try {
b = a[4];
} catch (ArrayIndexOutOfBoundsException aioobe) {
// jdk 1.3
/*
aioobe.printStackTrace(out);
out.flush();
e = sw.toString();
try {
out.close();
sw.close();
} catch (IOException e) {
// Do nothing, this should not happen as it is StringWriter.
}
out = null;
sw = null;
boolean logError = false;
int pos = e.indexOf("at ");
for (int i = 0; i < 3; i++) {
if (pos == -1) {
logError = true;
break;
}
e = e.substring(pos + 3, e.length());
pos = e.indexOf("at ");
}
if (logError) {
trueMethodName = "UnknownClass.unknownMethod()";
} else {
trueMethodName = e.substring(0, e.indexOf('('));
}
*/

// for jdk 1.4

elements = aioobe.getStackTrace();
if (elements.length < 3) {
return "UnknownClass.unknownMethod()";
}
trueMethodName = elements[2].getClassName() + "." + elements[2].getMethodName();

}
return trueMethodName;
}

public String getMethod() {
return methodName;
}

public String getMethodExTest() {
methodNameExTest = cutTestStr(getTrueMethod());
int index = methodNameExTest.lastIndexOf('.');
methodNameExTest =
methodNameExTest.substring(0, index + 1)
+ methodNameExTest.substring(index + 1, index + 2).toLowerCase()
+ methodNameExTest.substring(index + 2, methodNameExTest.length());
return methodNameExTest;
}
}


/********测试类*************/

public class DbUtil {
static MethodTrace methodTrace = new MethodTrace();

/**
* 得到连接-oracle
*
* @param url jdbc:oracle:thin:@server:1521:oracle
* @param user
* @param password
* @return
*/
public static Connection getOracleConnection(String url, String user, String password) throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException {
System.out.println("-----------get---"+methodTrace.getTrueMethod());
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
return DriverManager.getConnection(url, user, password);
}

/**
* 关闭连接-oracle
*
* @param conn
*/
public static void closeOracleConnection(Connection conn) {
System.out.println("-----------close--"+methodTrace.getTrueMethod());
System.out.println("--closeOracleConnection()");
if (conn != null) {
try {
conn.commit();
} catch (Exception e) {
System.out.println("error:db.closeOracleConnection().commit();");
}
try {
conn.close();
} catch (Exception e) {
System.out.println("error:db.closeOracleConnection().close();");
}
conn = null;
}
}
/*********测试方法*********/
public void test() {
DbUtil db = new DbUtil();
Connection conn = null;
try {
conn = db.getOracleConnection("jdbc:oracle:thin:@server:1521:oracle", "bsecp", "manager");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from t_sys_user");
while (rs.next()) {
System.out.println("---1---" + rs.getString(1));
System.out.println("---2---" + rs.getString(2));
}
rs.close();
stmt.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
db.closeOracleConnection(conn);
}
}

public static void main(String[] args) {
DbUtil d = new DbUtil();
d.test();
DbUtil.getCaller();
}
}

执行 d.test()后,将可以看到谁掉用了DbUtil 的getOracleConnection()方法和closeOracleConnection()方法。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值