简体   繁体   中英

How to expose a Stateless EJB method as MBean (on Jboss)?

I have a stateless EJB (3) that uses internal cache which is refreshed automatically every 24 hours. I would like to expose a MBean method to be able to force cache expiration or even cache reload on this EJB via JMX console on Jboss 4.2.

Can someone share an example on how to code this scenario? I'm totally new to JMX when it comes to creating my own beans.

Should I create an MBean that calls my EJB or is it possible to expose a specific EJB method as an Mbean interface by using annotation on EJB itself?

EJB looks like this:

@Stateless
@Local(BusinessCalendar.class)
public class BusinessCalendarBean implements BusinessCalendar {

    synchronized private LocalDateKitCalculatorsFactory getCalculatorFactory() {

        LocalDateKitCalculatorsFactory ldkc = (LocalDateKitCalculatorsFactory) CacheService.get(CACHE_KEY);

        if (ldkc == null) {
            ldkc = getCalculatorFactory();
            CacheService.put(CACHE_KEY, ldkc);
        }

        return ldkc;

    }

    public function expireCache() {
         // I would like to expose this as JMX managed method
    }

    ...

}

Update:

This is surely valid for WildFly 10+, jBOSS EAP 6.x or 7.x. But I suspect the mechanisms are no longer proprietary and shall work very similarly in other app servers.

JBoss specific annotations @Service / @Management were removed when JavaEE 6 standardized Singletons. A MBean (always a singleton so that all JMX clients see the same consistent JMX data application-wide) becomes an EE6+ Singleton exposed via JMX as follows:

  • define an interface with a name ending in "...MXBean" (compulsory)
  • create a @Singleton and @Startup class that implements this interface
  • define @PostConstruct and @PreDestroy methods to register/unregister the MBean

the register/unregister code is like:

objectName = new javax.management.ObjectName("com.acme.example.jmx:type=" + this.getClass().getName());
platformMBeanServer = java.lang.management.ManagementFactory.getPlatformMBeanServer();
platformMBeanServer.registerMBean(this, objectName);

The getters/setters defined in your "...MXBean" interface become JMX attributes, other methods are mapped to operations as specified in JMX Specifications under "lexical design patterns"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM