In my previous blog, we discussed how to locate MBeans within the custom tree. We're going to piggy back that discussion and show how to purge an EHCache MBean by executing the removeAll() operation.

For reference, let's see the findMBean() helper method we defined previously:

// function to help locate a mbean(s) in the provided list
// that match a specific name
def findMBean(prefix, name):
 // get a listing of everything in the current directory
 mydirs = ls(returnMap='true');
 found = [];
 // we're going to use a regular expression for our test
 pattern = java.util.regex.Pattern.compile(str(escape(prefix)) + str('.*name=') + str(escape(name)) + str('.*$'));
 // loop through the listing
 for mydir in mydirs:
 x = java.lang.String(mydir);
 matcher = pattern.matcher(x);
 // if we find a match, add it to the found list
 while matcher.find():
 return found;

In review, this method returns a list of MBean names that meet the regular expression in the current custom tree node (non-recursively). Once we have this list of names, we can then act on them. Here is our method:

def purgeCache(cacheDirNames):
 if len(cacheDirNames) > 0:
 // for each match, let's purge it
 for cacheDirName in cacheDirNames:
 print 'Purging cache ' + str(cacheDirName);
 params = jarray.array([], java.lang.Object);
 sig = jarray.array([],java.lang.String);
 invoke('removeAll', params, sig);
 //important, must do before we move on to the next cache
 print 'No cache found for ' + str(cache);

Let's decompose this method. The parameter cacheDirNames is the list of MBean names return from the previous findMBean() call. We're going to loop through each one of these names and execute the appropriate removal method. The meat of the purging process happens in lines 6 through 11.

On line 6, we perform the cd() API call on the MBean name. Within WLST, WebLogic treats MBean as a node of the tree. By "changing directory" into this node, we'll have access to all of the attributes and operations on this MBean.

On lines 7 through 9, we are preparing to and invoking the operation on the MBean. The invoke() API requires that we provide the method name to be called,removeAll, the signature of the parameter list for the operation (in our case nothing, thus a blank list), and the actual parameters passed to the operation (in our case a blank list of java.lang.Objects). Finally we call the invoke API with the appropriate parameters, invoke('removeAll', params, sig).

On line 11, we call cd('..') to change to the parent of the currently operated on MBean node. This is important if the next loop in the cycle is to work appropriately. Remember on line 6, we entered the custom MBean node, so we need to exit to the parent before beginning work on the next MBean in the list.

Putting this all together, finding the custom EHCache MBean and purging it of it's contents can be performed as such:

// connect to our server
connect('weblogic', 'welcome1', 't3://localhost:7001');
// change to our custom tree
// navigate to the appropriate location in our custom tree
// find our mbean(s)
mybean = findMBean('net.sf.ehcache:type=Cache,CacheManager', '');
// purge our mbean
// be nice and tidy up


Purge WLST Script