The Artima Developer Community
Sponsored Link

Java Buzz Forum
Testing JMX Stuff

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Brian McCallister

Posts: 1282
Nickname: frums
Registered: Sep, 2003

Brian McCallister is JustaProgrammer who thinks too much.
Testing JMX Stuff Posted: Jun 15, 2007 5:38 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Brian McCallister.
Original Post: Testing JMX Stuff
Feed Title: Waste of Time
Feed URL: http://kasparov.skife.org/blog/index.rss
Feed Description: A simple waste of time and weblog experiment
Latest Java Buzz Posts
Latest Java Buzz Posts by Brian McCallister
Latest Posts From Waste of Time

Advertisement

I was writing something to aggregate a bunch of JMX exports today and was really annoyed at adding environment variables to the test runner. Luckily, Martin pointed me to a fantastic howto on exporting an MBeanServer without supplying env variables to the jvm when it starts up. I specialized what he did a bit to orient it towards testing stuff and wound up with:

package org.skife.test;

import javax.management.MBeanServer;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.rmi.registry.LocateRegistry;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * Handy-dandy tool to help with JMX Testing 
 */
public class JMXHelper
{
    private static int port = -1;

    /**
     * Ensure JMX is all set up, returns port for JMX RMI Registry
     */
    public static int initialize()
    {
        if (port != -1) return port;
        try {
            final ServerSocket sock = new ServerSocket();
            sock.bind(new InetSocketAddress(0));
            port = sock.getLocalPort();
            sock.close();

            LocateRegistry.createRegistry(port);

            MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
            String s = String.format("service:jmx:rmi:///jndi/rmi://:%d/jmxrmi", port);
            JMXServiceURL url = new JMXServiceURL(s);
            JMXConnectorServer cs = 
                JMXConnectorServerFactory.newJMXConnectorServer(url, null , mbs);
            cs.start();
        }
        catch (Exception e) {
            throw new IllegalStateException("Unable to bind JMX", e);
        }
        return port;
    }
}

Now you can connect to it like normal:

String url = String.format("service:jmx:rmi:///jndi/rmi://localhost:%s/jmxrmi", port);
JMXConnector c = JMXConnectorFactory.connect(new JMXServiceURL(url));

and there is a JMXConnector to your "test" MBeanServer. As with most of JMX, it is kinda werid looking and obscure, but it works well :-)

Update: Martin pointed out that I could use less code. Changed.

Read: Testing JMX Stuff

Topic: JRuby 1.0: Congrats to the team Previous Topic   Next Topic Topic: I've posted the second beta of XOM 1.2, my free-as-in-speech (LGPL) library for processing XML...

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use