javacachingjmxwro4j

How to update wro cache/model programmatically via JMX?


Could you please give an example, how I can invoke the WroConfiguration.reloadCache() method via JMX? I use Wildfly, singleton startup ejb, in case it does matter.

JMX is switched on: jmxEnabled=true


Solution

  • Here is an example and requirements in Java EE environment:

    1. in the wro.properties the following properties are applied:
    cacheUpdatePeriod=0
    modelUpdatePeriod=0
    debug=false
    disableCache=true
    jmxEnabled=true
    ...
    
    1. Don't try to update wro cache model in ejb bean with @Singleton/@Startup annotations applied in the initial method with the @PostConstruct annotation applied. Wro MBean is not registred yet, so, it will not work.
    2. Example itself:
    try
    {
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        ObjectName name = new ObjectName("wro4j-ROOT:type=WroConfiguration");
        mbs.invoke(name, "reloadCache", null, null);
        mbs.invoke(name, "reloadModel", null, null);
    }
    catch (InstanceNotFoundException e)
    {
        logger.warn("Could not find wro4j MBean. It has not been initiated yet");
    }
    catch (Exception e)
    {
        logger.error(e);
    }