1

I am trying to access a MBean service deployed into JBOSS AS 7.1.1. My MBean service is also a Queue Listener. I am trying to get an instance of this MBean service to register it as a Queue Listener in another SAR.

I tried out this code but it is not working,

MBeanServer server  = ManagementFactory.getPlatformMBeanServer(); 
ObjectName mbeanObject =
        new ObjectName("myproject.service.Test:service=com.mytest.program");
TestServiceMBean handler = MBeanServerInvocationHandler.newProxyInstance(
      server, mbeanObject, TestServiceMBean.class, false);

I also tried out this

TestServiceMBean testMBeanService =
       (TestServiceMBean)server.getAttribute(mbeanObject,  "Instance");

In both the cases I am not getting the instance of the TestServiceMBean. Can anyone please help me in getting the access to MBean Test service.

<mbean code="com.mytest.program.TestService"
      name="myproject.service.Test:service=com.mytest.program">
</mbean>

Here's the code:

public class TestService implements TestServiceMBean, MessageListener {
Gray
  • 115,027
  • 24
  • 293
  • 354
Muthu
  • 67
  • 3
  • 9
  • Hello Muthu; When you say you're not getting an instance of the TestServiceMBean, what exactly happens ? Can you supply a stack trace of the error (assuming you get one). – Nicholas Jun 12 '12 at 14:55
  • When I try for getting instance as an attribute, this is what I am getting javax.management.AttributeNotFoundException: No such attribute: Instance at com.sun.jmx.mbeanserver.PerInterface.getAttribute(PerInterface.java:63) at com.sun.jmx.mbeanserver.MBeanSupport.getAttribute(MBeanSupport.java:216) – Muthu Jun 12 '12 at 15:06
  • In case of using proxy instance, its not getting me the "TestServiceMBean " instance it is getting me a class name "$Proxy0", when I debugged it is returning back the MBeanServerInvocationHandler class itslef. I tried to type cast it to TestServiceMBean. The compiler didnt complained but the objct I am getting is not the Queue Listener service object. – Muthu Jun 12 '12 at 15:13

1 Answers1

0

Muthu;

For the AttributeNotFoundException: At least we know that the MBean is actually registered :) So the question is, does your TestService mbean define an attribute called Instance and does it have a return a type of TestServiceMBean and does it actually return this ? If not, that's what you need to do.

TestService:

public TestServiceMBean getInstance() {
    return this;
}

TestServiceMBean:

public TestServiceMBean getInstance();

For the invocation handler, you should not need to cast, but the returned class will not have the name you expect. It's called Proxy0 because it is a synthetic dynamic proxy, but you should find that it does implement the TestServiceMBean interface and you should be able to invoke operations against it.

Nicholas
  • 15,916
  • 4
  • 42
  • 66
  • Hi Nicholas, Yes I am able to get the instance. I am faced with one more issue. I am trying to access the service class in another MBean service, means I call for the getInstance() in another SAR MBean service. Both the SAR's are deployed in the same JBOSS AS server. But during deployment says ClassNotFound Exception. Thats understandable as this MBean service is not in the visibility of the other SAR. Will you please let me know, how to access this MBean in another SAR MBean deployed in the same JBOSS AS. – Muthu Jun 13 '12 at 17:30
  • Hey Muthu; Your best bet is to either put the interface in a third "common" jar and put that jar in /server//lib so that both your sars class load it from the same classloader, or deploy both your sars in the same EAR, in which case the same applies. – Nicholas Jun 13 '12 at 18:03