Listing 1: Authentication mechanism

MBeanServer mbeanServer = null;
try {
Hashtable props = new Hashtable();
props.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
props.put(Context.PROVIDER_URL, "t3://AdminServer:7001");
props.put(Context.SECURITY_PRINCIPAL, "weblogic");
props.put(Context.SECURITY_CREDENTIALS, "weblogic");
InitialContext ctx = new InitialContext(props);
mbeanServer =(MBeanServer)
ctx.lookup("weblogic.management.server");
}
catch (NamingException ne) { ... }

Listing 2: getMBeanInfo method

MbeanInfo mbeanInfo = null;
try {
mbeanInfo = mbeanServer.getMBeanInfo(mbeanName); 
}
catch (InstanceNotFoundException infe) { ... }
catch (IntrospectionException ie) { ... }
catch (ReflectionException re) { ... }

MBeanAttributeInfo[] attributes = mbeanInfo.getAttributes();
MBeanOperationInfo[] operations = mbeanInfo.getOperations();

Listing 3: Invoke the method

MBeanAttributeInfo attribute = attributes[j];
String attributeName = attributes.getName();
if (attribute.isReadable()) {
Object attributeValue = null;
try {
attributeValue =
mbeanServer.getAttribute(mbeanName, attributeName);
}
catch (MBeanException mbe) { ... }
catch (AttributeNotFoundException anfe) { ... }
catch (InstanceNotFoundException infe) { ... }
catch (ReflectionException re) { ... }

...
}

Listing 4: JMXSample.java

package wldj;

import java.util.Properties;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MalformedObjectNameException;

import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class JMXSample
{
private static void usage()
{
System.out.println("Usage: java wldj.JMXSample <admin_url> [<username>
<password>]");
}

private static void handleException(String message, Exception e)
{
System.err.println(message + ": " + e.getMessage());
e.printStackTrace();
System.exit(1);
}

public static void main(String[] args)
{
String url = null;
String username = null;
String password = null;

switch (args.length) {
case 3:
username = args[1];
password = args[2];
case 1:
url = args[0];
break;
default:
usage();
return;
}

Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
props.put(Context.PROVIDER_URL, url);
if (username != null) {
props.put(Context.SECURITY_PRINCIPAL, username);
props.put(Context.SECURITY_CREDENTIALS, password);
}

try {

InitialContext ctx = new InitialContext(props);
MBeanServer mbeanServer = (MBeanServer)
ctx.lookup("weblogic.management.server");

String defaultDomain =
mbeanServer.getDefaultDomain();
ObjectName adminServerMBeanName =
new ObjectName(defaultDomain +
":Name=Admin Server,Type=AdminServer");
Object domainMBean = mbeanServer.getAttribute(adminServerMBeanName,
"ActiveDomain");
ObjectName domainMBeanName = new ObjectName(domainMBean.toString());
String domainName = (String)mbeanServer.getAttribute(domainMBeanName,
"Name");
Object adminServerMBean =
mbeanServer.getAttribute(adminServerMBeanName, "Server");
ObjectName serverMBeanName = new
ObjectName(adminServerMBean.toString());
String adminServerName =
(String)mbeanServer.getAttribute(serverMBeanName, "Name");

String defaultExecuteQueueName = domainName +
":Name=weblogic.kernel.Default,Server=" + adminServerName +
",Type=ExecuteQueue";
ObjectName defaultExecuteQueueMBeanName =
new ObjectName(defaultExecuteQueueName);
Integer threadCount =
(Integer)mbeanServer.getAttribute(defaultExecuteQueueMBeanName,
"ThreadCount");
System.out.println("Server " + adminServerName + 
" in domain " + domainName +
" has " + threadCount + 
" threads configured for the " +
"weblogic.kernel.Default" +
" execute queue"); 
}
catch (NamingException ne) {
handleException("JNDI Error", ne);
}
catch (MalformedObjectNameException mone) {
handleException("Bad Object Name", mone);
}
catch (AttributeNotFoundException anfe) {
handleException("Attribute Not Found", anfe);
}
catch (InstanceNotFoundException infe) {
handleException("Instance Not Found", infe);
}
catch (MBeanException me) {
handleException("MBean Error", me);
}
catch (ReflectionException re) {
handleException("Reflection Error", re);
}
}
}

Listing 5: WL_JMXSample.java

package wldj;

import java.util.Properties;
import javax.management.InstanceNotFoundException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import weblogic.management.MBeanHome;
import weblogic.management.configuration.AdminServerMBean;
import weblogic.management.configuration.ExecuteQueueMBean;
import weblogic.management.configuration.ServerMBean;

public class WL_JMXSample
{
private static void usage()
{
System.out.println("Usage: java wldj.WL_JMXSample <admin_url> [<username>
<password>]");

}

private static void handleException(String message, Exception e)
{
System.err.println(message + ": " + e.getMessage());
e.printStackTrace();
System.exit(1);
}

public static void main(String[] args)
{
String url = null;
String username = null;
String password = null;

switch (args.length) {
case 3:
username = args[1];
password = args[2];
case 1:
url = args[0];
break;
default:
usage();
return;
}

Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
props.put(Context.PROVIDER_URL, url);
if (username != null) {
props.put(Context.SECURITY_PRINCIPAL, username);
props.put(Context.SECURITY_CREDENTIALS, password);
}

try {
InitialContext ctx = new InitialContext(props);
MBeanHome mbeanHome = (MBeanHome)
ctx.lookup(MBeanHome.ADMIN_JNDI_NAME);

String domainName = mbeanHome.getDomainName();
AdminServerMBean adminServerMBean = 
(AdminServerMBean) mbeanHome.getAdminMBean("Admin Server",
"AdminServer", "weblogic");
ServerMBean serverMBean =
adminServerMBean.getServer();
String adminServerName = serverMBean.getName();

String defaultExecuteQueueName = 
"weblogic.kernel.Default,Server=" +
adminServerName;
ExecuteQueueMBean defaultExecuteQueueMBean =
(ExecuteQueueMBean)mbeanHome.getAdminMBean(defaultExecuteQueueName, "ExecuteQueue",
domainName);
int threadCount =
defaultExecuteQueueMBean.getThreadCount();
System.out.println("Server " + adminServerName + 
" in domain " + domainName +
" has " + threadCount + 
" threads configured for the " +
"weblogic.kernel.Default" +
" execute queue"); 
}
catch (NamingException ne) {
handleException("JNDI Error", ne);
}
catch (InstanceNotFoundException infe) {
handleException("Instance Not Found", infe);
}
}
}