This documentation is for WSO2 Governance Registry 5.2.0. View documentation for the latest release.
Skip to end of metadata
Go to start of metadata

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 links on how to install Maven and Installing the Product for links on how to install WSO2 Business Activity Monitor.


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


2. Start the WSO2 Business Activity Monitor. See Starting the server.

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 Install button.

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.


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


7. Add the following plugin to your POM file:


8. Add a new Java Class named StatisticsCollectorServiceComponent to 

<GREG_HOME>/samples/handler/src/src/main/java/org/wso2/carbon/registry/samples/statistics/ 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="registryService.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
                    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[]{
                        System.out.println("Successfully Published Event");
                } catch (Exception e) {
        }, null);
    protected void deactivate(ComponentContext context) {
    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 links on how to install it.

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

10. Copy the <G-REG_HOME>/ samples/handler/src/target/ org.wso2.carbon.registry.samples.handler-5.2.0.jar file into the <G-REG_HOME>/repository/components/dropins/ directory.

11. Start the server and observe the command prompt. See Running the Product for more information.

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.

  • No labels