1. 程式人生 > >使用JMX搭建WebLogic9監控軟體(二)

使用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