Hi,
Jay SenSharma

Jay SenSharma

After WLS8.1 there has been a lots of changes in the MBean tree and the way we access the MBeans. So sometimes we face difficulty to get the MBean Server connection if we dont know the AdminServer URL. Because without knowing the AdminServer URL we are unable to get Connected to the MBeanServer of adminServer.

So in the following Sample JMX code we will see that how we can access the AdminServer URL from the Web Application  which is deployed on the Managed Server.

Step1). Please create a Domain with AdminServer and a Managed Server.

Example:
AdminServer : localhost:7001
ManagedServer MS1 : localhost:7003

Step2). Create a WebApplication directory somewhere in your File system:

Example:  C:JmxDemoWebapp

Step3). Create the “WEB-INF/classes” directory inside “C:JmxDemoWebapp”  and now Place the following Java program inside “JMXHelper.java” inside “C:JmxDemoWebappWEB-INFclasses” directory

package jmx.helper;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.naming.InitialContext;
public class JMXHelper {
private static JMXHelper instance = new JMXHelper();
private String domainName;
private String managedServerName;
private JMXHelper() {
}
public static JMXHelper getInstance() {
return instance;
}
public String getDomainName() {
if (domainName == null) {
try {
MBeanServer server = getMBeanServer();
ObjectName domainMBean = (ObjectName) server.getAttribute(getRuntimeService(), "DomainConfiguration");
domainName = (String) server.getAttribute(domainMBean, "Name");
System.out.println("nt DomainName: "+domainName);
} catch (Exception ex) {
System.out.println("Caught Exception: " + ex);
ex.printStackTrace();
}
}
return domainName;
}
public String getCurrentServerName()
{
if (managedServerName == null)
{
try {
managedServerName = (String) getMBeanServer().getAttribute(getRuntimeService(), "ServerName");
System.out.println("nt Current ServerName: "+managedServerName);
}
catch (Exception ex)
{
System.out.println("Caught Exception: " + ex);
ex.printStackTrace();
}
return managedServerName;
}
return managedServerName;
}
public String getAdminServerURL()
{
String administrationURL=null;
try{
ObjectName msServerRuntime=new ObjectName("com.bea:Name="+managedServerName+",Type=ServerRuntime");
administrationURL=(String) getMBeanServer().getAttribute(msServerRuntime, "AdminServerHost");
int adminPort=(Integer) getMBeanServer().getAttribute(msServerRuntime, "AdminServerListenPort");
administrationURL=administrationURL+":"+adminPort;
System.out.println("nt AdminServerURL: "+administrationURL);
} catch (Exception ex) {
System.out.println("Caught Exception: " + ex);
ex.printStackTrace();
}
return administrationURL;
}
private MBeanServer getMBeanServer() {
MBeanServer retval = null;
InitialContext ctx = null;
try {
ctx = new InitialContext();
retval = (MBeanServer) ctx.lookup("java:comp/env/jmx/runtime");
} catch (Exception ex) {
System.out.println("Caught Exception: " + ex);
ex.printStackTrace();
} finally {
if (ctx != null) {
try {
ctx.close();
} catch (Exception dontCare) {
}
}
}
return retval;
}
private ObjectName getRuntimeService() {
ObjectName retval = null;
try {
retval = new ObjectName("com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean");
} catch (Exception ex) {
System.out.println("Caught Exception: " + ex);
ex.printStackTrace();
}
return retval;
}
}
Step4). Compile the above class after running “. ./setWLSEnv.sh” like:
JmxDemoWebappWEB-INFclasses> javac -d . JMXHelper.java

Step5). Provide the Following “index.jsp” page inside “C:JmxDemoWebapp” directory.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="jmx.helper.JMXHelper" %>
<%
JMXHelper jmx = JMXHelper.getInstance();
String domainName = jmx.getDomainName();
String managedServerName = jmx.getCurrentServerName();
String adminServerURL = jmx.getAdminServerURL();
%>
<html>
<head>
<title>Getting Admin URL</title>
</head>
<body>
<h2>Domain Name: <%= domainName %></h2>
<h2>CurrentServerName: <%= managedServerName %></h2>
<h2>AdminServer URL: <%= adminServerURL %></h2>
</body>
</html>

Step6). Deploy the application “JmxDemoWebapp” on Managed Server MS1 and then hit the URL: “http://localhost:7003/JmxDemoWebapp/index.jsp”

AdminServer Url Finding using Jmx

AdminServer Url Finding using Jmx

.
.
Thanks
Jay SenSharma
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.