On Jun 23, 2010, at 8:33 PM, David Blevins wrote:

> 
> On Jun 23, 2010, at 7:58 PM, viola.lu (JIRA) wrote:
> 
>> Hi, david: i have one question about this test case: currently, it just 
>> listed all mbean attributes/operations information.
>> But i should get its all attributes value, so i have to connect to a jmx 
>> connector server which has a jmxurl(such as 
>> service:jmx:rmi://localhost/jndi/rmi://localhost:1099/jmxrmi somthing like 
>> what we did in jconsole, but in this unit test case, we just use openejb 
>> assembler to create a container, i am not sure whether this already started 
>> a jmx connector server that i can connect using code:
>> JMXServiceURL jmxUrl = new 
>> JMXServiceURL("service:jmx:rmi://localhost/jndi/rmi://localhost:1099/jmxrmi");
>> JMXConnector connector =JMXConnectorFactory.connect(jmxUrl);
>> MBeanServerConnection mbsc = connector.getMBeanServerConnection();
>> ObjectName poolName = new 
>> ObjectName("openejb.management:J2EEServer=openejb,J2EEApplication=null,EJBModule=StatsModule,StatelessSessionBean=CounterBean,j2eeType=Pool,name=CounterBean");
>> Long avail = (Long)mbsc.getAttribute(poolName,"Available");
>> 
>> If it doesn't start a jmx connector server port 1099, how to? is there any 
>> method that i can use to start this port?thanks
> 
> Hi Viola!
> 
> I'm sort of a JMX beginner myself.  Ideally we'd be able to invoke the mbean 
> in the test case without a port open -- one would hope that this is possible 
> as the mbean server is right there in the VM.

Looks like we can do like this:

    ObjectName objectName = ...
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    Object attribute = server.getAttribute(objectName, "Available");

Tested it out to see if it works.  Seems to be fine.  Not sure if this is 
useful, but I thought of neat method:

    private Map<String, Object> snapshot(ObjectName objectName) throws 
Exception {
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
        Map<String, Object> data = new HashMap<String, Object>();
        MBeanInfo mBeanInfo = server.getMBeanInfo(objectName);
        for (MBeanAttributeInfo info : mBeanInfo.getAttributes()) {
            data.put(info.getName(), server.getAttribute(objectName, 
info.getName()));
        }
        return data;
    }

Might make testing easier to deal with maps instead of all the JMX stuff 
directly.  Cook's choice though :)  Whatever makes for good tests.


-David

Reply via email to