WSO2 CEP - Output Mapping Using Registry Resource

Publishing the output is an important requirement of CEP. WSO2 CEP allows to convert an event to TEXT, XML or JSON, which is known as output mapping . This article explains how a registry resource can be used for custom event mapping in WSO2  CEP 4.2.0.

Step 1:
Start the WSO2 CEP and login to the management console.

Step 2:
Navigate to Home → Manage → Events → Streams → Add Event Stream.

Step 3:
Define an event stream as given below and click on ‘Add Event Stream’ button.
Name: sensorstream
Version: 1.0.0
Meta Data Attributes: sensorId, sensorName and language
Correlation Data Attributes: longitude and latitude
Payload Data Attributes: sensorValue

Step 4:
Navigate to Home → Registry → Browse.

Step 5:
Expand the /_system/config tree view and click on the config folder.

Step 6:
Click on the Add Collection link and add a new collection named template.

Step 7:
Same as Step 6, create another sub folder en inside the template folder.

Step 8:
Click on the ‘Add Resource’ button and select ‘Create Text Content’ as the method. This means that we are going to provide a content that is readable for human.
Provide the name as message and leave the Media type as text/plain. Suppose you want to create an XML or JSON resource, change the media type to application/xml or application/json respectively.

Step 9:
Add the content that matches your media type and click the Add button.

The message used in this screenshot is given below:
Sensor {{meta_sensorName}} reports {{sensorValue}} from {{correlation_longitude}}:{{correlation_latitude}}

Step 10:
Navigate to the Home → Manage → Event → Publishers and click on the Add Event Publisher button.

Step 11:
Provide samplelogger as the publisher name, sensorstream:1.0.0 as the event source, logger as the output event adapter type and text as the message format. If your registry resource is XML or JSON, change the message format to your media type.

Then expand the Advanced configuration and select ‘Pick from registry’.
Click on the ‘Configuration Registry’ button, browse and select the message resource.

Leave the Cache Timeout with the default value.

Compare your configuration with the following screenshot.

Finally click the Add button.

Step 12:
Now move to the  Home → Tools → Event Simulator and select the sensorstream:1.0.0 as the events stream name.
Fill the form as shown below and click the send button.
sensorId: 10
sensorName: Temperature
language: en
longitude: 79.861256
latitude: 6.927131
sensorValue: 23.0

Once you clicked the Send button, you should get the following output in the terminal of CEP.

Parametrized Registry Path
WSO2 CEP allows you to parameterize the registry path as well. For example, assume that we want to select a registry resource depending on the meta_language. For this purpose, we need another registry resource.

As you did in Step 4 – 9, cerate a new folder named fr in /_system/config/template and a resource named message in /_system/config/template/fr folder.

The message used in this screenshot is given below:
Sensor {{meta_sensorName}} reports {{sensorValue}} from {{correlation_longitude}}:{{correlation_latitude}}

Now the registry should contain both en/message and fr/message.

Step 13:
Delete the existing samplelogger publisher and create a new one using the same name as shown in Step 11. But this time, provide the following path as the registry path.
Compare this path with the path in Step 11. Instead of hard coding the language folder name en or fr, the attribute meta_language is used. At the runtime, the meta_language will be replaced by the actual language name and the resource at that path will be used by the publisher.

Step 14:
Repeat the Step 12 with languages en, fr and something other than English and French.
In the terminal, CEP will print the message in English and French according to the input and thrown an exception if the resource for the given language does not exists.

If the registry does not contain a resource at the run time evaluated path, an exception will be thrown.

For more details about output mapping, visit to the official document: Output Mapping Types
Next Post »

Contact Form


Email *

Message *