3

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.

Arnaud
  • 7,259
  • 10
  • 50
  • 71
  • 1
    looks like you need some method of calling remote jxm bean. start looking here http://stackoverflow.com/questions/1751130/calling-jmx-mbean-method-from-a-shell-script – octo Sep 27 '12 at 13:01
  • Thanks Joachim, I went through specified url but I have following doubts because I need to implement it in Java, In my case what will be the url (111.22.3.45) and what will be the bean name? – Yatish Sonkeshariya Sep 27 '12 at 16:28

1 Answers1

5

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");
}
Arnaud
  • 7,259
  • 10
  • 50
  • 71
  • 2
    If you are unsure of what ObjectName to use. It's easy to connect and get the names of all existing MBeans, like this: `Set names = mbsc.queryNames(null, null);` – Gunslinger Sep 02 '13 at 09:42