Thursday, December 15, 2011

Using WSO2 Registry Eventing for notification through an Executor

We will gonna do this through an Executor associated to a registry LC (Life Cycle) .

What we gonna do: We are going to create a simple registry LC associate a resource to it and write an Executor class which notify via email when that resource promote to its next state. To send email we use registry eventing functionality.

Executors are one of the facilitators which helps to extend the WSO2 GReg functionality. Here an Executor is associated to a registry life cycle. Life Cycle is basically a set of pre-defined states which can be associated to a registry Resource and allows that associated resource to evolve through that LC as defined. There users can basically do "Promote" and "Demote" a resource from one state to another.

You can write your own WSO2 GReg Executor class by implementing the org.wso2.carbon.governance.registry.extensions.interfaces.Execution interface. There you can override the execution method.

public class LCStateChangeNotificationExecutor implements Execution {
public void init(Map map) { }
public boolean execute(RequestContext requestContext, String s, String s1) {
// DO what you want to do when the resource change its LC state. Here we try to send an email notification to a subscriber.
sendEmail(requestContext);
return true;
}
}
Now lest see how to achieve the notification through registry eventing. Here we create a ResourceUpdateEvent and send that event to a subscriber resource which is a dummy resource where we manually subscribed to its update mode via my email. ( when state change happens to a resource, the executor gets HIT and it will send a Update Event to a dummy resource to which user is subscribed via email.)

UserRegistry registry registry = (UserRegistry) requestContext.getSystemRegistry();
ResourceUpdatedEvent resourceUpdatedEvent = new ResourceUpdatedEvent(CUSTOM_EMAIL_MESSAGE);

resourceUpdatedEvent.setResourcePath(SUBSCRIPTION_RESOURCE_PATH);
if (registry != null) {
resourceUpdatedEvent.setTenantId(registry.getTenantId());
}


Now Event instance is ready and only needs to sent it through registry NotificationService.

 org.wso2.carbon.registry.common.eventing.NotificationService.notify(resourceUpdatedEvent);

NOTE: Notification service is exposed as an OSGi service where that service included component class has getters and setters so that you can call the getRegistryNotificationService() method on that class which will return the NotificationService set by the eventing service.


Then you are done with the Executor class. Please add an entry inside to a LC state from which you want the Executor to be HIT when state change takes place.

So to define reference to Executor.
Now we have to define the Executor in our LC. Find the EventLifeCycle.xml.xml LC file under GREG_HOME/samples/sportmeet-scenario/src/resources.
(In the EventLifeCycle.xml.xml file we have added an entry for our executor inside the first state, because here, we want the executor to hit as soon as LC state promote from its initial state).
Here is the sample LC XML.
<aspect name="WSO2SportMeetEventHandler" class="org.wso2.carbon.governance.registry.extensions.aspects.DefaultLifeCycle">
<configuration type="literal">
<lifecycle>
<scxml xmlns="http://www.w3.org/2005/07/scxml"
version="1.0"
initialstate="Create">
<state id="Create">
<datamodel>
<data name="checkItems">
<item name="Created the Event" forEvent="Promote">
</item>
<item name="Announced the Event" forEvent="Promote">
</item>
</data>
<data name="transitionValidation">
<validation forEvent="Promote"
class="org.wso2.carbon.registry.samples.sportmeet.validators.PromoteValidator">
</validation>
</data>
<data name="transitionExecution">
<execution forEvent="Promote"
class="org.wso2.carbon.registry.samples.sportmeet.executors.LCStateChangeNotificationExecutor">
</execution>
</data>
</datamodel>
<transition event="Promote" target="Edit"/>
</state>
<state id="Edit">
<datamodel>
<data name="checkItems">
<item name="Add participants" forEvent="Promote">
</item>
<item name="Collect participant details" forEvent="Promote">
<validations>
<validation forEvent="Promote"
class="org.wso2.carbon.registry.samples.sportmeet.validators.ParticipantValidator">
</validation>
</validations>
</item>
</data>
<data name="transitionValidation">
<validation forEvent="Promote"
class="org.wso2.carbon.registry.samples.sportmeet.validators.PromoteValidator">
</validation>
</data>
<data name="transitionExecution">
<execution forEvent="Promote"
class="org.wso2.carbon.registry.samples.sportmeet.executors.LCStateChangeNotificationExecutor">
</execution>
</data>
</datamodel>
<transition event="Promote" target="Review"/>
<transition event="Demote" targedt="Create"/>
</state>
<state id="Review">
<datamodel>
<data name="checkItems">
<item name="Not eligible participants removed" forEvent="Promote">
</item>
<item name="Extra participants removed(if maximum number exceeds)" forEvent="Promote">
</item>
<item name="All the participhants are not eligible for the Event" forEvent="Demote">
</item>
</data>
<data name="transitionExecution">
<execution forEvent="Promote"
class="org.wso2.carbon.registry.samples.sportmeet.executors.LCStateChangeNotificationExecutor">
</execution>
</data>
<data name="transitionValidation">
<validation forEvent="Promote"
class="org.wso2.carbon.registry.samples.sportmeet.validators.PromoteValidator">
</validation>
</data>
</datamodel>
<transition event="Promote" target="Accept"/>
<transition event="Demote" target="Edit"/>
</state>
<state id="Accept">
<transition event="Demote" target="Review"/>
<datamodel>
<data name="transitionExecution">
<execution forEvent="Promote"
class="org.wso2.carbon.registry.samples.sportmeet.executors.LCStateChangeNotificationExecutor">
</execution>
</data>
</datamodel>
</state>
</scxml>
</lifecycle>
</configuration>
</aspect>



For the notification part you must enable the axis2 TransportSender in GREG_HOME/reposiroty/conf/axis2.xml.


1. Now everything is ready and start the server.

2. Go to admin console.

3.Add a resource (which you provided as SUBSCRIPTION_PATH) and subscribe to it via email under update resource mode.

4 . Now add another resource and associate the service LC to it and do a promote.

5 When promoted, an email notification will be sent to your previously
subscribed email address.


References: