Pages

Wednesday, December 18, 2013

Jmx Client – Weblogic

In this article we will write a sample JMX client for connecting to the Weblogic Instance. The following code gives us a start up in wiring more advanced scripts for handling the Weblogic Instance.

Code
Here is the Code for connecting to the Weblogic Instance running on a Local host(Just change the details accordingly)

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();
        }
    }
}

The Jmx URL is the important line the whole code,


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

The available Protocols that can be to connect to the remove machines are IIOP,T3,RMI.In the above code we use IIOP to connect to the Weblogic Instance.

The Port and IP details can be obtained from the config file if we don’t know . The config file is located at WL_INSTANCE/config/config.xml file.

The Server details are laid out as
<server>
    <name>basrServer</name>
    <listen-port>7001</listen-port>
    <listen-address>Firsthost</listen-address>
  </server>

Make sure you get the correct details regarding the server. There are information in the config.xml file that says about many servers configured.

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, "weblogic1");

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 "weblogic1" are the credentials associated with the user originating the JMX connection.
The rest of the code says about retrieving ObjectNames for the MBeans registered in the Runtime MBean Server.These will be displayed on console
Compiling the Code
Since we don’t have any Dependency with the weblogic files, the compilations goes fine.

Running the Code
For running the code, we need some classes from the weblogic. 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.

java –classpath .:/root/Oracle/Middleware/wlserver_10.3/server/lib/wljmxclient.jar JMXClient

We can see a lot of Objects being displayed on the console.

Hope This sample code helps us to write more advanced jmx clients in accessing weblogic.

More To Come , Happy Learning J