使用JMX搭建WebLogic9监控软件(二)

程序实现 下面提供了部分代码的节选。 1.提供JMXWebLogicHelper作为获取连接的工具。 public class JMXWebLogicHelper implements JMXHelper { /** * 获取JMXMBeanServer连接 * * @param URI * Consts.URI_XXX * @param protocol * 协议 weblogic为T3 * @param hostname * 主机IP地址 * @param port * 端口 * @param username * 管理用户名 weblogic * @param password * 密码 * @return * @throws IOException * @throws MalformedURLException */ private MBeanServerConnection getConnection(String URI, String protocol, String hostname, int port, String username, String password) throws IOException, MalformedURLException { JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port, "/jndi/" + URI); HashMap h = new HashMap (); h.put(Context.SECURITY_PRINCIPAL, username); h.put(Context.SECURITY_CREDENTIALS, password); h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote"); JMXConnector connector = JMXConnectorFactory.connect(serviceURL, h); MBeanServerConnection connection = connector.getMBeanServerConnection(); return connection; } /** * 获取编辑连接 */ public MBeanServerConnection getEditConn(String protocol, String hostname, int port, String username, String password) throws IOException, MalformedURLException { return getConnection("weblogic.management.mbeanservers.edit", protocol, hostname, port, username, password); } /** * 获取运行时连接 */ public MBeanServerConnection getRuntimeConn(String protocol, String hostname, int port, String username, String password) throws IOException, MalformedURLException { return getConnection("weblogic.management.mbeanservers.runtime", protocol, hostname, port, username, password); } /** * 获取DomainRuntime */ public MBeanServerConnection getDomainRuntimeConn(String protocol, String hostname, int port, String username, String password) throws IOException, MalformedURLException { return getConnection("weblogic.management.mbeanservers.domainruntime", protocol, hostname, port, username, password); } } 2.获取weblogic服务器名称 MBeanServerConnection conn = JMXWebLogicHelper.getRuntimeConn("t3", "192.168.1.108", 7001, "weblogic", "weblogic"); ObjectName obn = new ObjectName( "com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean"); String serverName = String.valueOf(conn.getAttribute(obn, "ServerName")); System.out.println(serverName); 如果出现java.net.MalformedURLException: Unsupported protocol: t3异常,则需要将weblogic.jar加到classpath中。 如果出现异常javax.management.InstanceNotFoundException: 请检查new ObjectName()中的名称和键值对是否正确,或者创建的MBeanServer连接是否和ObjectName中的不一致,造成无法找到MBean的实例。 获取服务器是否处于生产模式 生产模式的路径是RuntimeServiceMBean->DomainConfiguration->ProductionModeEnabled,返回值是boolean。 public static void getProductMode() { MBeanServerConnection conn; try { conn = JMXWebLogicHelper.getRuntimeConn("t3", "192.168.1.108", 7001, "weblogic", "weblogic"); ObjectName obn = new ObjectName( "com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean"); obn = (ObjectName) conn.getAttribute(obn, "DomainConfiguration"); boolean b = Boolean.getBoolean(String.valueOf(conn.getAttribute( obn, "ProductionModeEnabled"))); System.out.println("生产模式:" + b); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (MalformedObjectNameException e) { e.printStackTrace(); } catch (NullPointerException e) { e.printStackTrace(); } catch (AttributeNotFoundException e) { e.printStackTrace(); } catch (InstanceNotFoundException e) { e.printStackTrace(); } catch (MBeanException e) { e.printStackTrace(); } catch (ReflectionException e) { e.printStackTrace(); } 监控当前JVM堆的大小 从RuntimeServiceMBean->ServerRuntime->JVMRuntime->HeapSizeCurrent MBeanServerConnection conn = JMXWebLogicHelper.getRuntimeConn("t3", "192.168.1.108", 7001, "weblogic", "weblogic"); ObjectName obn = new ObjectName( "com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean"); obn = (ObjectName) conn.getAttribute(obn, "ServerRuntime"); obn = (ObjectName) conn.getAttribute(obn, "JVMRuntime"); int b = Integer.parseInt(String.valueOf(conn.getAttribute( obn, "HeapSizeCurrent"))); System.out.println("当前堆大小bytes:" + b); 5.监控连接池运行状态 监控连接池状态我们不能简单的应用上面的方法了,因为连接池可能会有多个,而我们只需要监控其中的某个或某几个,那么我们需要自己开发监控插件来完成对某个连接池对象的监控。在配置文件中,我们指定插件的位置,就可以实现定制监控了。 public String getValue(String info, String appserver, String monitorpoint, String attr) throws Exception { String[] infos = info.split("[|]");// 用竖线分割上下文信息 conn = JMXWebLogicHelper.getRuntimeConn("t3", "192.168.1.108",7001, "weblogic", "weblogic"); ObjectName obn = new ObjectName( "com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean"); // 根对象 ObjectName dc = (ObjectName) conn .getAttribute(obnRoot, "ServerRuntime"); ObjectName[] apps = (ObjectName[]) conn.getAttribute(dc, "ApplicationRuntimes"); for (ObjectName app : apps) { String name = (String) conn.getAttribute(app, "ApplicationName"); if (infos[0].equalsIgnoreCase(name)) { // 如果监控的是这个web应用 ObjectName[] runtimes = (ObjectName[]) conn.getAttribute(app, "ComponentRuntimes"); for (ObjectName run : runtimes) { String runName = (String) conn.getAttribute(run, "Name"); if (infos[1].equalsIgnoreCase(runName)) { return String.valueOf(conn.getAttribute(run, attr)); } } } } return null; } 可扩展性的保证,配置文件 ######################应用服务器监控设置################################ #应用服务器1的要监控的内容 appserver1.monitor.count=2 #获取服务名 #*************************************************** appserver1.monitor1=RuntimeServiceMBean.DomainConfiguration.ProductionModeEnabled appserver1.monitor1.name=生产模式 appserver1.monitor1.notice=true #*************************************************** appserver1.monitor2=custom appserver1.monitor2.name=连接池运行状态 appserver1.monitor2.class=com.wonder.monitor.impl.JDBCStateMonitor #在上面的自定义类中,表示context,前面是数据源的JNDI名字,后面是组件名 appserver1.monitor2.info=TEST|TEST appserver1.monitor2.attr=State appserver1.monitor2.notice=true Wonder 2009-9-30
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值