维护复杂的遗留系统是一项挑战,而文档、理性设计以及编码实践的缺乏通常会使情况变得更为糟糕。遗憾的是,几乎所有的软件开发人员在其职业生涯中都会遇到此类任务。
对于任何使用数据库的应用程序,跟踪应用程序所生成的sql语句是非常有益的。这样的跟踪有助于分析性能瓶颈和调试错误,还可以帮助开发人员了解与应用程序相关的业务流程。
对于遗留的应用程序,我们希望可以进行这样的跟踪而不必修改任何代码或应用程序配置。利用weblogic的jmx api,我们可以快速地编写出跟踪大型复杂遗留应用程序的jdbc调用的少量代码,而不会对现有代码和应用程序配置产生影响。此外,这种小项目有助于我们理解jmx以及在幕后weblogic是如何使用jmx的。在本文中,我将展示利用weblogic jmx跟踪sql语句的细节。
什么是jmx?
jmx全称为java management extensions(java管理扩展)。mbean(即managed bean,托管bean)是可以通过jmx api进行管理的资源。大多数应用服务器使用jmx来提供管理控制台并管理资源。此外,应用程序开发人员可以在他们的定制应用程序中使用jmx来提供管理和审计功能。
weblogic的jmx实现为开发人员和管理员提供了哪些优点?
weblogic server使用jmx mbeans进行配置和管理。每个weblogic server会有一个自己的mbean的副本,它由管理服务器负责更新。管理服务器维护它所管理的所有服务器的mbeans的正本。一旦管理服务器发生故障,托管服务器将根据本地的mbean副本运行,直到管理服务器可以再次更新该服务器的本地mbean。
weblogic不仅提供了一个使用jmx mbeans的管理控制台,它还提供了一个api以便允许应用程序开发人员配置和研究weblogic资源。利用weblogic jmx的最容易的方式就是使用weblogic控制台来更改weblogic资源的配置,以及查看控制台中的技术指标。虽然weblogic控制台的监控和配置功能相当强大,可以满足运行在weblogic上的大多数应用程序的需要,weblogic jmx api还是提供了一种更为强大的工具来管理运行在weblogic平台上的应用程序。weblogic jmx api的使用使得配置和扩展weblogic资源成为可能,还可以从weblogic的子系统接收通知。例如,一个jdbc连接的最小和最大数设为n的应用程序可能希望有一个监听器,以便监听来自weblogic jmx mbeans的通知,并且在有n-x个并发jdbc连接使用应用程序时,会向管理员发送电子邮件,从而使管理员可以决定增加n值并重新配置jdbc连接池(这里x是一个由管理员决定的任意数字)。应用程序开发人员进一步使用jmx的例子包括weblogic子系统中的跟踪事件,包括ejb事件和服务器启动/停止事件。
在分析jdbc语句方面,weblogic应用程序中有哪些可用选项?
在weblogic应用程序中,有多种技术可以用来创建对jdbc语句的动态跟踪。为来自java.sql包的statement、preparedstatement和callablestatement类创建子类,以便使用log4j或weblogic记录之类的记录系统打印跟踪信息,然后在应用程序中使用这些子类,这是一个可行方案,但是并不适用于遗留代码。也可以使用类似于toad的工具来实现这种跟踪,但是此类工具对于应用程序开发人员而言可能不容易得到,而且可能无法提供所需的全部信息。aop技术是打印jdbc语句的另一种可行方案。然而在撰写本文时,bea weblogic还没有正式支持aop,尽管关于weblogic aop的文章已经在dev2dev网站上出现。在撰写本文时,在weblogic上实现aop也并不是一项轻而易举的任务。使用weblogic 6.1或8.1的weblogic jmx不需要使用任何附加的类库和配置,因为所需的所有类均可在weblogic.jar中得到,而且代码实现起来相当简单。况且weblogic jmx是一项非常成熟的技术,可以通过不改变任何核心应用程序代码或者字节码来实现。
使用weblogic jmx api
weblogic javadoc可以通过http://e-docs.bea.com/wls/docs81/javadocs/在线获得。该api包括几个名称中包含management的包,这些包就是weblogic的jmx实现(参见表1)。

使用jmx跟踪jdbc调用
一种编写跟踪代码并提供一个用户界面来查看sql的简单方法是编写一个jsp、一个servlet以及一个java bean或对象。我们将展示bean/pojo的全部细节,而省去用户界面/控制器方面的大多数细节,因为大多数weblogic开发人员对此已有很深的了解。注意,无需修改任何部署描述符、数据库连接池或数据源来实现跟踪,所有对应用程序的更改将在运行时进行。
步骤1
首先我们将创建一个名为mytracerbean.java的类,并导入所需的weblogic jmx包和类。
import javax.naming.context;
import weblogic.jndi.environment;
import weblogic.management.mbeanhome;
import weblogic.management.configuration.jdbcconnectionpoolmbean;
import weblogic.management.runtime.jdbcstatementprofile;
import weblogic.management.runtime.jdbcconnectionpoolruntimembean;
import javax.management.instancenotfoundexception;
import javax.management.invalidattributevalueexception;
import javax.naming.namingexception;

这些类均位于weblogic.jar中,因此不需要向weblogic类路径添加任何jar或类。
步骤2
接下来我们将编写一个获取mbeanhome的方法。
private mbeanhome getmbeanhome() {
//url to the serve whose jdbc activity we are tracing
string url = "t3://localhost:7001";
string username = "mywlconsoleuname";
string password = "mywlconsolepsswd";
//the mbeanhome will allow us to
//retrieve the mbeans related to jdbc statement tracing
mbeanhome home = null;
try { //we'll need the environment so that we can //retrieve the initial context
environment env = new environment();
env.setproviderurl(url);
env.setsecurityprincipal(username);
env.setsecuritycredentials(password context ctx = env.getinitialcontext();
//retrieving the mbeanhome interface for the server with //the url t3://localhost:7001
home =(mbeanhome)ctx.lookup(mbeanhome.local_jndi_name);
} catch (namingexception ne) {
system.out.println("error getting mbeanhome " + ne);
}
return home;
}
对于最简单的情形:管理服务器也驻留了我们要跟踪的jdbc应用程序,上述代码完全可行;但是对于管理服务器独立于托管服务器,并且涉及到几个独立jvm的情形,我们需要获得管理mbeans home而不是本地mbeans home。二者的区别在于,本地home只为单个服务器提供mbean,而管理home则为管理服务器所管理的所有服务器提供mbean。为了获得管理mbeans home而不是本地mbeans home,可以将上述代码中的local_jndi_name替换为admin_jndi_name。
步骤3
提供一种打开和关闭jdbc分析的方式是非常有用的,因为分析的开销相当大,所以不需要时应当将其关闭。默认情况下,分析是关闭的,因此必须在跟踪任何jdbc语句前打开它。创建一个如下的方法:
public void configurejdbcauditing(boolean ison) {
try {
mbeanhome home = getmbeanhome();
//retreive the bean to help us configure the pool
jdbcconnectionpoolmbean mconfigbean =
(jdbcconnectionpoolmbean)home.getconfigurationmbean("mypool","jdbcconnectionpoolconfig");
mconfigbean.setsqlstmtprofilingenabled(ison);
mconfigbean.setsqlstmtparamloggingenabled(ison);
} catch (invalidattributevalueexception iave) {
system.out.println("invalid attribute while configuring tracing " + iave);
} catch (instancenotfoundexception infe) {
system.out.println("instance not found while configuring tracing " + infe);
}
}
在上述代码中,我们还告知连接池我们希望查看传入sql语句中的参数。这会增加跟踪的开销,但是它可以为我们提供一些有价值的信息。
步骤4
在配置jdbc池来保存配置文件之后,我们可以对其进行查询。记住,检索到的配置文件数等于打开分析后所执行的sql语句数,而不等于所有由mypool connectionpool执行的sql语句数。以下的代码检索配置文件,maxprofiles参数指示应该获取最近的多少个配置文件。创建的方法如下:
/** pass in -1 to get all profiles */
public jdbcstatementprofile[] getprofiles(int maxprofiles) {
jdbcstatementprofile[] profiles = null;
try {
mbeanhome home = getmbeanhome();
jdbcconnectionpoolruntimembean mbean =
(jdbcconnectionpoolruntimembean)home.getruntimembean("mypool
","jdbcconnectionpoolruntime");
int numprofiles = mbean.getstatementprofilecount();
int profilesindex = 0;
//figure out index to start at and how many we want
if (maxprofiles != -1) {
profilesindex = numprofiles - maxprofiles;
}else {
maxprofiles = numprofiles;
}
profiles =mbean.getstatementprofiles(profilesindex,maxprofiles);
} catch (instancenotfoundexception infe) {
system.out.println("problem retrieving jdbc profiles " + infe);
}
return profiles;
}
jdbcconnectionpoolruntimembean的getstatementprofiles方法在weblogic 8.1 api文档中没有提及,尽管它曾在weblogic 6.1文档中出现。不过这看起来是个错误,因为在weblogic 8.1中该方法是可用的,并且weblogic 8.1还修复了方法中的一个bug(cr094729,参见http://e-docs.bea.com/wls/docs81/notes/resolved_sp01.html),这意味着weblogic 8.1是打算包含该方法的。
步骤5
可以添加一个清空功能,使得重启服务器时可以清空语句缓存:
public void reset() {
mbeanhome home = getmbeanhome();
try {
jdbcconnectionpoolruntimembean mbean =
(jdbcconnectionpoolruntimembean)home.getruntimembean("mypool
","jdbcconnectionpoolruntime");
//remove everything from the cache
mbean.resetstatementprofile();
} catch (instancenotfoundexception infe) {
system.out.println("problem while resetting jdbc profiles " + infe);
}
}
步骤6
对配置文件进行迭代,获得要显示的信息(参见清单1)。这些代码可能放在用户界面层,比如放在一个jsp中。
清单1
mytracerbean mytracer = new mytracerbean();
//in this case we want the 100 most recently executed statements
jdbcstatementprofile[] profiles = mytracer.getprofiles(100);
//doing the looping so that the most recent statements information is
//retrieved first
for (int i=profiles.length-1;i>-1;i--) {
//getting the number of parameters passed into the current //statement
int paramcount = profiles[i].getparametercount();
//format the start and end time for the current statement
simpledateformat simpledateformat =
new simpledateformat("yyyyy.mmmmm.dd ggg hh:mm:ss:ss aaa");
string starttime=simpledateformat.format(new date(profiles[i].getstarttime()));
string endtime=simpledateformat.format(new date(profiles[i].getstoptime()));
//append the parameters together in order to display them
stringbuffer paramsbuffer = new stringbuffer();
if (paramcount < 1) {
paramsbuffer.append("none");
} else {
for (int j=0;j<paramcount;j++) {
paramsbuffer.append(profiles[i].getparameter(j));
paramsbuffer.append(" ");
}
}
string statementtxt = profiles[i].getstatementtext();
string paramstxt = paramsbuffer.tostring();
string timetaken = profiles[i].gettimetaken()
//then use statementtxt, paramstxt, timetaken, starttime, endtime
// etc to show the statement details in a ui
}
展望weblogic jmx的前景
在撰写本文时,刚刚发布的weblogic 9.0支持的是jmx 1.2而不是weblogic 8.1及以前版本一直支持的jmx 1.0。响应jmx规范的变化,9.0中的weblogic jmx api有了相当大的变化,清单1中的代码可能会引起不支持的警告。当升级至9.0时,应当用jdbcdatasourceruntimembean替换jdbcconnectionpoolruntimembean。不过,在撰写本文时,绝大多数运行在weblogic上的遗留应用程序还没有使用weblogic server 9.0,而且很可能在相当长的一段时间内不会使用9.0。
闽公网安备 35060202000074号