Our goal is to implement a simple WebLogic JMX client that can be used as a starting point to write any WebLogic JMX client. We will look at the client’s code. The classpath used to compile and run the code. The different WebLogic MBeanServers and associated JMX URI’s the JMX Client can connect to.

Let’s directly dive in the client’s code below:


package blog.wls.jmx.client;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXServiceURL;
import javax.management.remote.JMXConnectorFactory;
import java.util.Hashtable;
import java.util.Set;
public class JMXClient {
public static void main(String[] args) throws Exception {
JMXConnector jmxCon = null;
try {
JMXServiceURL serviceUrl =
new JMXServiceURL(
"service:jmx:iiop://127.0.0.1:7001/jndi/weblogic.management.mbeanservers.runtime");
System.out.println("Connecting to: " + serviceUrl);
Hashtable env = new Hashtable();
env.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
"weblogic.management.remote");
env.put(javax.naming.Context.SECURITY_PRINCIPAL, "weblogic");
env.put(javax.naming.Context.SECURITY_CREDENTIALS, "welcome1");
jmxCon = JMXConnectorFactory.newJMXConnector(serviceUrl, env);
jmxCon.connect();
MBeanServerConnection con = jmxCon.getMBeanServerConnection();
Set<ObjectName> mbeans = con.queryNames(null, null);
for (ObjectName mbeanName : mbeans) {
System.out.println(mbeanName);
}
}
finally {
if (jmxCon != null)
jmxCon.close();
}
}
}

Let’s take a quick look at the above code.

JMXServiceURL serviceUrl = 
new JMXServiceURL(
"service:jmx:iiop://127.0.0.1:7001/jndi/weblogic.management.mbeanservers.runtime");

The JMX service URI identifies the following:

  • The protocol used to communicate with the remote WebLogic process. “iiop” in the above example. WebLogic supports “t3”, “iiop” and “rmi”. We won’t touch further on this topic here.
  • The WebLogic process to connect to (host & port ). “127.0.0.1:7001” in the above example.

    The port value is available from the <WLS_INSTANCE_HOME>/config/config.xml file:

     <server>
    <name>myServer</name>
    <listen-port>7001</listen-port>
    <listen-address>myhost</listen-address>
    </server>

    Make sure you look under the correct server if several servers are defined as part of your config.xml. For instance in the above case we are connecting to the server identified as “myServer”.

    The same information is also available from the server log at <WLS_INSTANCE_HOME>/servers/myServer/logs/myServer.log:

    <Sep 22, 2009 12:05:23 PM PDT> <Notice> <Server> <localhost><myServer>
    <[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'>
    <<WLS Kernel>> <> <> <1253585123268> <BEA-002613> <Channel "Default" is now
    listening on myhost:7001 for protocols iiop, t3, ldap, snmp, http.>
  • The MBeanServer to connect to: ” weblogic.management.mbeanservers.runtime”. This identifies WebLogic’s “Runtime” MBeanServer. This is the MBeanServer available from any WebLogic process, and that contains both WebLogic and user MBeans.

            Hashtable env = new Hashtable();
env.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
"weblogic.management.remote");
env.put(javax.naming.Context.SECURITY_PRINCIPAL, "weblogic");
env.put(javax.naming.Context.SECURITY_CREDENTIALS, "welcome1");

The above code specifies properties associated with the connection. “weblogic.management.remote” identifies the package implementing WebLogic’s client side connector code. For instance the ‘t3’ or ‘iiop’ client connector.

“weblogic” and “welcome1” are the login/password associated with the user originating the JMX connection.

In the rest of the code we establish the JMX connection, and then retrieve the ObjectNames for all the MBeans registered in the “Runtime” MBeanServer. The ObjectNames are then displayed on the console. We also make sure we release the connection in a finally block to avoid leaking any resource.

The code doesn’t contain any WebLogic specific classes, and can be compiled as follow:


javac -d . blog/wls/jmx/client/JMXClient.java

Executing the code requires to add WebLogic client side classes to the execution ClassPath. We need to include the WebLogic JMX connector client side classes. Remember we specified those in the environment properties passed to the JMXConnectorFactory. Below is the command I used:


java -classpath .:$WL_HOME/server/lib/wljmxclient.jar blog/wls/jmx/client/JMXClient

WL_HOME points to the WebLogic binary install.

The above uses WebLogic’s thin client classes. In some cases when you access server side classes, or you want to take full advantage of the t3 protocol, you are required to use WebLogic’s thick client classes. To do so, you first need to build wlfullclient.jar as follow:


cd $WL_HOME/server/lib
java -jar wljarbuilder.jar

You can then execute the JMX Client as follow:


java -classpath .:$WL_HOME/server/lib/wlfullclient.jar  blog/wls/jmx/client/JMXClient

You can also bundle wlfulclient.jar with your client code. When using the thin client you cannot just bundle wljmxclient.jar with you client code, as this jar references other jars as part of its manifest Class-Path entry. At the time of this writing, you also need to bundle wlclient.jar

At this point we are now able to connect to WebLogic’s “Runtime” MBeanServer. Each WebLogic process contains a “Runtime” MBeanServer in which local WebLogic MBeans and user-defined application MBeans are registered. WebLogic also offers two other MBeanServers that are only available from the Domain “AdminServer” process:

  • The “Domain Runtime” MBeanServer. It aggregates the MBeans registered on the domain’s “Runtime” MBeanServers. So as long as a managed server is up, its MBeans can be accessed through the “Domain Runtime” MBeanServer. To connect to that MBeanServer, just use the following JMX URI:


    service:jmx:iiop://127.0.0.1:7002/jndi/
    weblogic.management.mbeanservers.domainruntime

    “7002” is the iiop port for my “AdminServer” install. Replace with your AdminServer’s port as previously explained. Remember; only the AdminServer process runs the “Domain Runtime” MBeanServer.


  • The “Edit” MBeanServer. It contains the WebLogic Config MBeans that are used to configure the Domain. To connect to that MBeanServer, just use the following JMX URI:


    service:jmx:iiop://127.0.0.1:7002/jndi/weblogic.management.mbeanservers.edit 

    “7002” is the iiop port for my “AdminServer” install. Replace with your AdminServer’s port as previously explained. Remember; only the AdminServer process runs the “Domain Runtime” MBeanServer.


Note: One can also use “t3” or “rmi” as protocol in place of “iiop”

The rest of the client’s code remains unchanged. Only the JMX URI needs to be changed to connect to different MBeanServers.