程式實現
下面提供了部分代碼的節選。
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