Weblogic JMX – Creating Instances of MBeans in Java

I am working on something similar, and the solution that @Rich gave worked for me. However, I have ended up with a slightly different approach that doesn’t require using the weblogic.management.jmx.MBeanServerInvocationHandler (directly at least).

Here is some code that should do what you are looking for:

package scratch;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Hashtable;

import javax.management.JMX;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;

import weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean;

import com.bea.wli.sb.management.configuration.ALSBConfigurationMBean;
import com.bea.wli.sb.management.configuration.SessionManagementMBean;

public class TestOSB_JMX {

    public static void main(String[] args) throws Throwable {

        // Provide connection information for OSB WebLogic AdminServer on command line
        String host = args[0];
        int    port = Integer.parseInt(args[1]);
        String user = args[2];
        String password = args[3];

        JMXConnector conn = initJmxConnection(host, port, user, password);
        try {
            System.out.println("Opened JMX connection to " + host + ":" + port + " as " + user);

            // get mbean connection
            MBeanServerConnection mbconn = conn.getMBeanServerConnection();

            // Get SessionmanagementMBean
            SessionManagementMBean sm = 
                JMX.newMBeanProxy(mbconn, ObjectName.getInstance(SessionManagementMBean.OBJECT_NAME), SessionManagementMBean.class);

            // Create a session
            String sessionName = "MySession";           
            sm.createSession(sessionName);

            // Get the configuration MBean for the session, do stuff, and then discard the session.
            try
            {
                System.out.println("Session exists? : " + sm.sessionExists(sessionName));

                ALSBConfigurationMBean configMBean = 
                    JMX.newMBeanProxy(
                        mbconn, 
                        ObjectName.getInstance("com.bea:Name=" +ALSBConfigurationMBean.NAME + "." + sessionName +",Type=" + ALSBConfigurationMBean.TYPE), 
                        ALSBConfigurationMBean.class
                    );

                System.out.println("Got the config MBean for session: " + configMBean.getSession());

                // Do stuff in the session here....
            }
            finally
            {
                // use activateSession to commit session changes instead
                sm.discardSession(sessionName);
            }
        } finally {
            conn.close();
            System.out.println("Closed JMX connection");
        }
    }

    public static JMXConnector initJmxConnection(String hostname, int port,String username, String password) 
    throws IOException, MalformedURLException 
    {
        JMXServiceURL serviceURL = new JMXServiceURL("t3", hostname, port, "/jndi/" + DomainRuntimeServiceMBean.MBEANSERVER_JNDI_NAME);

        Hashtable<String, String> h = new Hashtable<String, String>();
        h.put(Context.SECURITY_PRINCIPAL, username);
        h.put(Context.SECURITY_CREDENTIALS, password);
        h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote");

        return JMXConnectorFactory.connect(serviceURL, h);
    }
}

Here are some things to note about my solution:

  1. My classpath directly references the following WebLogic and OSB JAR files: alsb.jar, osb-server-modules-ref.jar, wlfullclient.jar
  2. These JARs will reference (many) other JARs through the Class-Path entries in the Manifest files.
  3. You may need to generate the wlfullclient.jar file as described here: http://docs.oracle.com/cd/E17904_01/web.1111/e13717/jarbuilder.htm
  4. It is better to use wlfullclient.jar instead of weblogic.jar for this type of client application. I was using weblogic.jar first and it caused many class loading headaches for me.
  5. I am using Oracle Service Bus Version: 11.1.1.4 WebLogic version: 10.3.4, and Java 1.6.0_29.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s