使用JMX搭建WebLogic9監視軟體(二)

來源:互聯網
上載者:User
程式實現
下面提供了部分代碼的節選。
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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.