ITeye博客

使用Tomcat的jmx服务

bill.end · 2009-04-24 08:44 · unread · Apache · .net · Tomcat · Web · SUN · Markdown

http://www.blogjava.net/happyy2k/archive/2009/04/22/266881.html

Tomcat 5.5.20可以使用JMX服务进行管理操作。下面介绍如何查看Tomcat提供哪些JMX服务,并如何使用这些JMX服务。

1. 使用JDK1.5自带的JConsole程序查看Tomcat的JMX服务

  要让JConsole能查看到Tomcat的JMX服务,需要Tomcat启动一个管理口。由于tomcat5.5.20缺省启动文件不提供JMX服务接口 加入下面红色内容到catalina.bat:


set JAVA_OPTS=%JAVA_OPTS% [color=red]-Dcom.sun.management.jmxremote.port=1090 -Dcom.sun.management.jmxremote.ssl=false [/color]-Dcom.sun.management.jmxremote.authenticate=false -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file="%CATALINA_BASE%\conf\logging.properties" 

启动Tomcat5.5.20

再运行jdk1.5的jconsole程序

d:\jdk1.5\bin\jconsole nnnnn (nnnn 是tomcat的进程号 用Task Manager查)

2. 调用Tomcat的JMX服务,如停止、启动web应用 写一个JavaBean,用来调用Tomcat的JMX服务,关键方法如下:


public static boolean callWebModuleMBeanMethod(String appName,String methodName) throws Exception{ 
MBeanServer mBeanServer = null; 
  
if (MBeanServerFactory.findMBeanServer(null).size() > 0) { 
mBeanServer = (MBeanServer) MBeanServerFactory.findMBeanServer( 
null).get(0); 
} else { 
throw new Exception("cann't find catalina MBeanServer"); 
} 
  
Set names = null; 
try { 
names = mBeanServer.queryNames(new ObjectName( 
"*:j2eeType=WebModule,name=//localhost/"+appName+",*"), null); 
} catch (Exception e) { 
throw new Exception("cann't find "+appName+ " web moudule mbean! can't undeploy web app.\n"+e.getMessage()); 
} 
if(names==null || names.size()==0) { 
log.debug("can't find "+appName+ " web moudule mbean!"); 
return false; 
} 
  
ObjectName oname =null; 
Iterator it = names.iterator(); 
if (it.hasNext()) { 
oname=(ObjectName) it.next(); 
} 
  
if(oname==null) 
return false; 
try { 
mBeanServer.invoke(oname,methodName,null,null); 
return true; 
} catch (Exception e) { 
throw new Exception("can't "+methodName+" "+appName+ " web application!\n"+e.getMessage()); 
} 
} 
  
public static void main(String[] args){ 
callWebModuleMBeanMethod("app1","stop"); //停止web应用app1 
callWebModuleMBeanMethod("app1","start"); //启动web应用app1 
} 

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。