开发者

Updating JMX MBean attributes without exposing methods to client

开发者 https://www.devze.com 2023-02-17 22:48 出处:网络
I am busy implementing a monitoring solution using JMX. I need to expose certain attributes which will mostly be counters to the JMX client. I have used Spring to hook it all up which works out great.

I am busy implementing a monitoring solution using JMX. I need to expose certain attributes which will mostly be counters to the JMX client. I have used Spring to hook it all up which works out great.

Below is my MBean class:

@Component
@ManagedResource(objectName="org.samples:type=Monitoring,name=Sample")
public class JmxMonitorServiceImpl implements JmxMonitorService {

  private AtomicInteger counter = new AtomicInteger(0);

  @Override
  public int incrementCounter() {
    return counter.incrementAndGet();
  }

  @ManagedAttribute(description="Current Counter value")
  public int getCounter() {
    return counter.intValue();
  }

  @Override
  @ManagedOperation(description="Reset the Counter to Zero")
  public void resetCounter() {
    counter.set(0);
  }

}

The MBean attributes get exposed as expected, so I have no issues there. My problem comes in at the point where I want to increment the counter.

From the snippet above you would see that the "incrementCounter" method does not have a @ManagedOperation annotation on it. Reason for this is that I do not want to expose this to the JMX client and only want to use it inside my components.

The only way I could get it to work with the MBean from multiple components were to create a proxy object. Here I am also using Spring, extract from context below:

<bean id="jmxMonitorServiceProxy" class="org.springframework.jmx.access.MBeanProxyFactoryBean">
    <property name="objectName" value="org.samples:type=Monitoring,name=Sample" />
    <property name="proxyInterface" value="org.samples.monitoring.JmxMonitorService" />
</bean>

With this proxy I can now interact with my MBean but in order to increment the counter I need to put a @ManagedOperation annotation on the method otherwise I get an exception that

Operation incrementCounter not in ModelMBeanInfo

If this MBean was only being used inside 1 component I could overcome this issue because Spring exposes the actual class instance for me as well, but as soon as you use the same MBean in multiple components it instantiates its own instance.

So after the long explanation :), my question is if exposing these sensitive methods through 开发者_开发知识库a proxy is the only way to go for using a MBean across components or is there someone that can point me in the right direction?

Look forward to the responses :)


Move the counter to another bean and inject that in all the MBean instances.

0

精彩评论

暂无评论...
验证码 换一张
取 消