Yatish Sonkeshariya
Yatish Sonkeshariya

Reputation: 91

Clear ehcache of remote server

How can we clear ehcache on a remote server?

My application is running in staging environment (host 111.22.3.44 and port 17000) and I want to write an utility method that can connect to a given host:port and clear the ehcache of my App. This utility should to work in Windows as well as Linux.

I use JConsole.exe utility to flush the cache of ehcache created in stage-server, but there is a situation where I need to do it programatically.

Upvotes: 3

Views: 3254

Answers (1)

Yatish Sonkeshariya
Yatish Sonkeshariya

Reputation: 91

Hurrey...:) I got the solution for clearing ehcache on a remote environment. Here, I have written a Java utility method that will flush out ehcache of a given remote machine that is specified by host name and port.

public void flushEhcache() throws IOException, NamingException,  MalformedObjectNameException, NullPointerException, AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException {
    String host = "111.22.3.44";
    String port = "16000";
    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://"
        + host + ":" + port + "/jmxrmi");
    JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
    MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();        
    ObjectName beanName = new ObjectName("net.sf.ehcache:type=CacheManager,name=Your  Application Name Here");        
    mbsc.invoke(beanName, "clearAll", new Object[0], new String[0]);
    System.out.println("Flushed out ehcache succesfully");
}

Upvotes: 5

Related Questions