Ok. I’m extremely pumped. I’ve just used commons modeler to deploy an MBean to JBoss and it’s actually working! Commons Modeler makes JMX so incredibly simple compared to doing this stuff by hand, that I had to see the thing running before I believed it.

Now, JBoss has its own way of deploying MBeans via a declaritive XML mechanism, which is cool, but I’m after something that I can play with on JBoss then deploy on Websphere, so portable APIs work for me.

I’ve been playing around with Commons Modeler after reading the early review copy of Manning’s Jakarta Commons In Action (top book!), but the examples show creating your own Http Adaptor. I knew JBoss had a nice Html Adaptor out of the box, but was wondering how I could registerer my MBean with it.

Well here’s some sample JMX, JBoss and Commons Modeler action:

    void register() {
        URL url =
            this.getClass().getResource(
                "/MyCoolServiceStatistics.xml");
        if (url == null) {
            log.error("Bugger. Couldn't find config file. JMX disabled.");
            return;
        }

        try {

            Registry registry = Registry.getRegistry(null, null);

            /* Details of this procedure on http://www.jboss.org/wiki/Wiki.jsp?page=FindMBeanServer */
            MBeanServer server =
                (MBeanServer) MBeanServerFactory.findMBeanServer(null).get(0);

            registry.setMBeanServer(server);

            registry.loadMetadata(url);

            statsAppName =
                new ObjectName("MyCoolService:name=statistics");

            registry.registerComponent(
                this,
                statsAppName,
                "au.com.bytecode.ejb.MyCoolServiceStatistics");

        } catch (Exception e) {
            log.error("Errors registering component", e);
            throw new RuntimeException(e);
        }
    }

I create my little Stats object when the App Server creates the related EJB, and then make some calls to it when methods are invoked on the EJB to keep track of useful stats. The magic sauce was the findMBeanServer() gear which I nicked off the JBoss Wiki here. Basically once you add it to the MBean server, it appears in the Http Adaptor that JBoss gives you out of the box. Sensational!

Not sure if it works on Websphere, I’ll give it a go tomorrow. Not even sure Websphere 5.1 has a Http Adaptor for JMX. I had a quick look in the Admin console and only saw Soap and RMI adaptors, but it all looks pluggable, so I should be able to slot one in.