This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

Statistics Collector Sample

Statistics Collectors and Declarative Service Components are two of many well-defined extension points supported by the WSO2 Governance Registry. Read more on Supported Extension Points for a complete list of extension points supported by WSO2 Governance Registry.

This sample explains how to:

  1. Create a statistics collector to collect statistics of operation invocations on WSO2 Governance Registry
  2. Publish that information from WSO2 Governance Registry to WSO2 Business Activity Monitor.
  3. Creating and deploying OSGi bundles that expose Declarative Services using the Apache Maven SCR Plugin.

This sample is similar to the Notifications Subscriber Sample and uses the same BAM Toolbox.

Once successfully deployed this sample will report operation invocation statistics of WSO2 Governance Registry on the WSO2 Business Activity Monitor's dashboard. We will be reusing the code of the Handler Sample in this example. This sample requires Apache Maven and WSO2 Business Activity Monitor. See Installation Prerequisites for more information on downloading this. Also see Installing Business Activity Monitor on Windows from Binary Distribution or Installing Business Activity Monitor  on Linux and Solaris from Binary Distribution.

Instructions

1. Open to BAM_HOME/ repository/conf/carbon.xml and set the port offset to 1.

<Offset>1</Offset>

2. Start the WSO2 Business Activity Monitor. See Installing Business Activity Monitor on Windows from Binary Distribution or Installing  Business Activity Monitor  on Linux and Solaris from Binary Distribution.

3. Deploy the KPI_Registry_Activity.tbox BAM Toolbox to WSO2 Business Activity Monitor.

A BAM Toolbox is an installable archive which contains stream definitions, dashboard components and analytics for WSO2 Business Activity Monitor. The KPI_Registry_Activity.tbox is a pre-built BAM Toolbox based on the KPI Monitoring Sample example of WSO2 Business Activity Monitor, which is specifically designed for this sample. Read the KPI Monitoring Sample example of WSO2 Business Activity Monitor to learn how to make changes to this BAM Toolbox or create your own.

Select the ToolBox From File System option and fill in the URL of the KPI_Registry_Activity.tbox Toolbox. Then click on the Installbutton.

If the BAM Toolbox fails to install from the URL download the KPI_Registry_Activity.tbox Toolbox to your local file system and upload it.

4. Navigate to GREG_HOME/ samples/handler/src to find the source code of the Handler Sample.

5. Add the following dependencies to your POM file:

Project Object Model (POM) is an XML representation of a Maven project held in a file named pom.xml. You should find POM file by the name pom.xml inside GREG_HOME/ samples/handler/src.

<dependency>
    <groupId>org.wso2.carbon</groupId>
    <artifactId>org.wso2.carbon.databridge.agent.thrift</artifactId>
    <version>4.0.1</version>
</dependency>
<dependency>
    <groupId>org.wso2.carbon</groupId>
    <artifactId>org.wso2.carbon.databridge.commons</artifactId>
    <version>4.0.0</version>
</dependency>

6. Comment-out the following exclusions in your POM file:

<!--<exclusion>
    <groupId>org.eclipse.osgi</groupId>
    <artifactId>org.eclipse.osgi.services</artifactId>
</exclusion>-->
<!--<exclusion>
    <groupId>org.wso2.carbon</groupId>
    <artifactId>org.wso2.carbon.context</artifactId>
</exclusion>-->

7. Add the following plugin to your POM file:

<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-scr-plugin</artifactId>
</plugin>

8. Add a new Java Class named StatisticsCollectorServiceComponent at 

GREG_HOME/samples/handler/src/src/main/java/org/wso2/carbon/registry/samples/statistics/StatisticsCollectorServiceComponent.java with the following source:

package org.wso2.carbon.registry.samples.statistics;
 
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.databridge.agent.thrift.Agent;
import org.wso2.carbon.databridge.agent.thrift.DataPublisher;
import org.wso2.carbon.databridge.agent.thrift.conf.AgentConfiguration;
import org.wso2.carbon.databridge.commons.Event;
import org.wso2.carbon.databridge.commons.exception.NoStreamDefinitionExistException;
import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.registry.core.statistics.StatisticsCollector;
import org.wso2.carbon.registry.core.utils.RegistryUtils;
import org.wso2.carbon.utils.NetworkUtils;
 
/**
 * @scr.component name="org.wso2.carbon.registry.samples.statistics" immediate="true"
 * @scr.reference name="registry.service" interface="org.wso2.carbon.registry.core.service.RegistryService"
 * cardinality="1..1" policy="dynamic" bind="setRegistryService" unbind="unsetRegistryService"
 */
public class StatisticsCollectorServiceComponent {
 
    public static final String REGISTRY_ACTIVITY_STREAM = "org.wso2.bam.registry.activity.kpi";
    public static final String VERSION = "1.0.0";
 
    private ServiceRegistration serviceRegistration;
 
    protected void activate(ComponentContext context) {
        serviceRegistration = context.getBundleContext().registerService(
                StatisticsCollector.class.getName(), new StatisticsCollector() {
            public void collect(Object... objects) {
                try {
                    // Create Data Publisher
                    RegistryUtils.setTrustStoreSystemProperties();
                    DataPublisher dataPublisher = new DataPublisher(
                            "tcp://" + NetworkUtils.getLocalHostname() + ":7612", "admin", "admin",
                            new Agent(new AgentConfiguration()));
 
                    // Find Data Stream
                    String streamId;
                    try {
                        streamId = dataPublisher.findStream(REGISTRY_ACTIVITY_STREAM, VERSION);
                    } catch (NoStreamDefinitionExistException ignored) {
                        streamId = dataPublisher.defineStream("{" +
                                "  'name':'" + REGISTRY_ACTIVITY_STREAM + "'," +
                                "  'version':'" + VERSION + "'," +
                                "  'nickName': 'Registry_Activity'," +
                                "  'description': 'Registry Activities'," +
                                "  'metaData':[" +
                                "          {'name':'clientType','type':'STRING'}" +
                                "  ]," +
                                "  'payloadData':[" +
                                "          {'name':'operation','type':'STRING'}," +
                                "          {'name':'user','type':'STRING'}" +
                                "  ]" +
                                "}");
                    }
 
                    if (!streamId.isEmpty()) {
                        // Publish Event to Stream
                        dataPublisher.publish(new Event(
                                streamId, System.currentTimeMillis(),
                                new Object[]{"external"}, null, new Object[]{
                                Thread.currentThread().getStackTrace()[3].getMethodName(),
                                CarbonContext.getCurrentContext().getUsername()}));
                        dataPublisher.stop();
                        System.out.println("Successfully Published Event");
                    }
 
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, null);
    }
 
    protected void deactivate(ComponentContext context) {
        serviceRegistration.unregister();
    }
 
    protected void setRegistryService(RegistryService registryService) {
        // The Maven SCR Plugin Needs These 
    }
 
    protected void unsetRegistryService(RegistryService registryService) {
        // The Maven SCR Plugin Needs These
    }
}

See also Declarative Services and Apache Maven SCR Plugin.

9. Compile the source code by running the following command inside GREG_HOME/ samples/handler/src:

mvn clean install

The command mvn clean install will trigger an Apache Maven Build in your command line. This requires you having installed Apache Maven. See Installation Prerequisites for more information on downloading this.

A successful run of Apache Maven will generate a report similar to the following:

10. Copy the GREG_HOME/ samples/handler/src/target/ org.wso2.carbon.registry.samples.handler-4.5.0.jar into GREG_HOME/repository/components/dropins.

11. Start the server and observe the command prompt. See Running the Product on Windows or Running the Product on Linux and Solaris.

12. After the server has started, log into the Management Console and perform various operations such such Managing WSDLs, Managing Subscriptions and Search. This should generate several events for BAM, and you should see lines similar to the following printed on your command prompt, which indicates that you events were successfully generated. For best results, create multiple user accounts and log in using different credentials while you perform operations.

Successfully Published Event

13. Navigate to the Gadget Portal of WSO2 Business Activity Monitor to see the statistics corresponding to your operations.

A Statistics Collector must implement the org.wso2.carbon.registry.core.statistics.StatisticsCollector interface. You can have more than one active Statistics Collector at any given time where each of them can be useful in one or many scenarios. For example, Statistics Collectors are used to capture Invocation Statistics visible via JMX.