Using Registry Resources for Output Mapping
WSO2 CEP allows you to map custom content from registry for the XML, JSON and Text mapping types. Custom mapping is stored as a registry resource to be used by the publisher instead of being specified as inline input. WSO2 CEP caches this resource at runtime, and the cache is updated after every cache timeout specified in minutes. If caching is not required, specify the cache timeout as 0.
The following tutorial illustrates how to carry out custom mapping using a registry resource.
- Log into the WSO2 CEP Management Console.
- Define an event stream as follows. For detailed instructions to create event streams, see Understanding Event Streams.
Enter basic information for the stream as follows.
Parameter Value Event Stream Name sensorsteam
Event Stream Version 1.0.0
Add attributes as follows.
Attribute Category Attribute Name Attribute Type Meta Data sensorId
int
sensorName
string
language
string
Correlation Data longitude
double
latitude
double
Payload Data sensorValue
double
- Add an output mapping template as a registry resource as follows. For more information about the registry, see Registry.
- In the Main tab expand the Registry section and click Browse.
- Under Tree View, navigate to
/_system/config
directory and click Detail View. Click Add Collection. In the Name field, enterTemplate
and click Add. This creates a new sub directory namedtemplate
in the/_system/config
directory. Navigate to the
/_system/config/template
sub directory and click Add Resource. Then enter information as shown in the table below, and click Add.Field Value Method Create Text Content
Name message Media Type text/plain
Content Welcome
Sensor {{meta_sensorName}} reports {{sensorValue}} from {{correlation_longitude}}:{{correlation_latitude}}
Create a new publisher as follows. For more information, see Configuring CEP to Create Alerts - Creating event publishers.
Field Value Event Publisher Name samplelogger
Event Source sensorstream
Output Event Adapter Type logger
Under Mapping Configuration, select Text for the Message Format field. Then click Advanced. This expands the Create a New Event Publisher page to display the Text Mapping section. Select the Pick from Registry option. In the Registry Path field, navigate to the registry location where you saved the registry resource created in step 3.
In this example, the default value of
15
is left unchanged for the Cache Timeout (in minutes) field. Therefore, this resource will be cached every 15 minutes.- Click Add Event Publisher.
In the Tools tab, click Event Simulator. In the Event Stream Name field, select sensorstream. Enter the following values for the parameters displayed, and then click Send to simulate a single event.
Parameter Value sensorid 10
sensorName Temperature
language en longitude 79.861256
latitude 6.927131
sensorValue 23.0
Using custom registry paths
The registry resource path itself can be parameterized using runtime attribute values. e.g., If the event stream to which the logger
publisher in the example above is connected has an attribute named meta_language
, a different registry source is selected depending on the runtime value of the meta_language
attribute.
The following tutorial illustrates how to change the registry resource path from which a custom output mapping is picked based on the value of an attribute.
The sensor stream
event stream used in the previous tutorial is also used in this tutorial.
Log into the CEP Management Console.
- Create two sub directories in the
Template
directory you created in the previous tutorial, and add a resource to each of these sub directories as described below. For more information about the registry, see Registry.- In the Main tab expand the Registry section and click Browse.
- Under Tree View, navigate to the
/_system/config/template
sub directory. Create two more sub directories in it nameden
andfr
. Navigate to the
/_system/config/template/en
sub directory and click Add Resource. Then enter information as shown in the table below, and click Add.Field Value Method Create Text content
Name message
Media Type text/plain
Content Welcome Sensor {{meta_sensorName}} reports {{sensorValue}} from {{correlation_longitude}}:{{correlation_latitude}}
Navigate to the
/_system/config/template/fr
sub directory and click Add Resource. Then enter information as shown in the table below, and click Add.Field Value Method Create Text content
Name message Media Type text/plain
Content Bienvenue Sensor {{meta_sensorName}} reports {{sensorValue}} from {{correlation_longitude}}:{{correlation_latitude}}
Delete the publisher named
sample logger
and redefine it as follows. For more information, see Configuring CEP to Create Alerts - Creating event publishers.Field Value Event Publisher Name samplelogger
Event Source sensorstream
Output Event Adapter Type logger
Under Mapping Configuration, select Text for the Message Format field. Then click Advanced. This expands the Create a New Event Publisher page to display the Text Mapping section. Select the Pick from Registry option. Then enter information as follows.
Field Value Registry Path conf:/template/{{meta_language}}/message
Cache Timeout (in minutes) 15 Log into the CEP Management Console.
In the Tools tab, click Event Simulator. In the Event Stream Name field, select
sensorstream
. Send three events with the following values.Parameter Value for Event 1 Value for Event 2 Value for Event 3 sensorid 10
10
10
sensorName Temperature
Temperature
Temperature
language en
fr
ru
longitude 79.861256
79.861256
79.861256
latitude 6.927131
6.927131
6.927131
sensorValue 23.0
23.0
23.0
The three events are logged as follows.
Note that an error is returned for the third event. This is because no output mapping was done for theru
language.