Followup on Running Tabular Reports with Coherence 12.1.3

About a year ago I published a post on Running Tabular Reports with the Reporter.

I was doing some JMX related work recently and found a cleaner way to iterate through the TabularData which is generated form these reports. This way also allows you to retrieve the column names which can be quite useful.  See my updated code below.


import com.tangosol.net.CacheFactory;
import com.tangosol.net.Cluster;
import com.tangosol.net.NamedCache;
import com.tangosol.net.management.MBeanHelper;

import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.TabularData;
import java.util.Collection;
import java.util.Set;

public class ReporterExample
    {
    public static void main(String[] args)
            throws InstanceNotFoundException, MalformedObjectNameException,
            ReflectionException, MBeanException
        {
        // enable management before we start/join a cluster
        System.setProperty("tangosol.coherence.management", "all");
        System.setProperty("tangosol.coherence.log.level", "2");
        System.setProperty("tangosol.coherence.distributed.localstorage", "true");

        Cluster cluster = CacheFactory.ensureCluster();
        MBeanServer server  = MBeanHelper.findMBeanServer();

        // create caches and add some data
        NamedCache nc1 = CacheFactory.getCache("dist-tim1");
        NamedCache nc2 = CacheFactory.getCache("dist-tim2");

        nc1.put("key1", "value1");
        nc2.put("key1", "value1");

        // get the local member to run the report on
        int nLocalMemberId = cluster.getLocalMember().getId();

        TabularData reportData = (TabularData) server.invoke(
              new ObjectName("Coherence:type=Reporter,nodeId=" + nLocalMemberId),
                             "runTabularReport",
                             new Object[] {"reports/report-cache-size.xml"},
                             new String[] {"java.lang.String"});

        // navigate the data
        if (reportData != null)
            {
            Collection<CompositeData> values = (Collection<CompositeData>) reportData.values();

            for (CompositeData compositeData : values)
                {
                Set<String> keys = compositeData.getCompositeType().keySet();
                keys.forEach( (key) -> System.out.println(key + ":\t " + compositeData.get(key)));
                System.out.println("");
                }
            }
        }
    }

Output is below:

AvgSize:	 168.0
BatchCounter:	 0
CacheSize:	 1.0
MemoryBytes:	 168.0
MemoryMB:	 1.6021728515625E-4
Name:	 dist-tim1
Service:	 PartitionedCache
rowID:	 1

AvgSize:	 168.0
BatchCounter:	 0
CacheSize:	 1.0
MemoryBytes:	 168.0
MemoryMB:	 1.6021728515625E-4
Name:	 dist-tim2
Service:	 PartitionedCache
rowID:	 2

(For more explanation, please refer to the original post)

Note: You will need to run this on Java 8 as I’m using the forEach default method from the Iterable interface.

Advertisements
This entry was posted in Examples and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s