JavaScript Operator
The JavaScript engine allows you to execute JavaScript snippets within a graph. It is based on the JavaScript operator. This operator increments and outputs a counter value with the input.
Configuration Parameters
|
Parameter |
Type |
Description |
|---|---|---|
|
codelanguage |
string |
The programming language used in the snippet.
Default: "javascript" |
|
script |
string |
Mandatory. The inline script to be executed. If the script starts with "file://", then the script file is executed. Default: "" |
Input
|
Input |
Type |
Description |
|---|---|---|
|
input |
string |
Any string that will trigger the counter output. |
Output
|
Output |
Type |
Description |
|---|---|---|
| output |
string |
The counter value. |
Creating a JavaScript Extension
- Create an extension of the JavaScript operator and add the desired input and output ports.
- Add your JavaScript snippet as an inline "script" parameter in the extension configuration or put the code into a separate file and use the prefix "file://" to specify the file name.
Basic Example
var counter = 0
$.setPortCallback("input",onInput)
function onInput(ctx,s) {
counter++
$.output(counter)
}
Generators
Generators are functions that are executed before the event processing loop starts.
var counter = 0
$.addGenerator(gen)
$.addGenerator(gen)
function gen(ctx) {
for (var i = 0; i < 3; i++ ) {
$.output(counter)
counter++
}
}
This example produces the values 0,1,2,3,4,5 on the output port "output".
Timers
This example produces the values 0,1,2,3,4,5 on the output port "output".
var counter = 0
$.addTimer("1s",t1)
function t1(ctx) {
$.output(counter)
counter++
}
This example produces the values 0,1,2... on port "output" until graph shutdown.
Shutdown Handlers
This example produces the values 0,1,2... on port "output" until graph shutdown.
var counter = 0
$.setPortCallback("input",onInput)
function onInput(ctx,s) {
counter++
}
function shutdown(ctx) {
$.output(counter)
}
$.addShutdownHandler(shutdown)
$.addShutdownHandler(shutdown)
This example produces values 7,7 if 7 values have been provided on the "input" port.
Configuration API
Configuration API provides the ability to read the configuration provided via a graph description.
function gen(ctx) {
$.str($.config.getString("str"))
$.int($.config.getInt("int"))
$.bool(""+$.config.getBool("bool"))
$.msg({Attributes: $.config.getObject("obj")})
$.msg({Attributes: $.config.asObject()})
}
$.addGenerator(gen)
This example reads the configuration of the operator and provides the given values on the output ports.
Error Handling
The fail function provides the ability to terminate a processing loop in case of an internal error.
var counter = 0
$.setPortCallback("input",onInput)
function onInput(ctx,s) {
counter++
if (counter > 1 ) {
$.fail("Failure in counter")
}
$.output(counter)
}
This example terminates the loop while processing the second input on "input" port.
Message Response Handling
The sendResponse function provides the ability to send either a response or error message to the request-response initiating operator. This function is only valid if the incoming data is of type message and it contains the response callback as part of a request-response message exchange.
var counter = 0
function onInput(ctx,s) {
counter++
if (counter < 100) {
var msg = {};
msg.Body = "pong " + counter;
$.sendResponse(s, msg, null);
} else {
$.sendResponse(s, null, Error("Usage limit reached " + counter));
}
}
This example sends a response or error depending on the current counter value.
Send Message to Logging App
-
logSeverity.ERROR
-
logSeverity.INFO
-
logSeverity.WARNING
Another optional parameters are user, messageCode, and details.
The function awaits the logging API call execution, returning a message if the log message was successfully sent to the logging app.
$.log("message to log", $.logSeverity.INFO, "", "10003", "this is a detailed message to the log message");
This example sends a message with INFO severity to the logging app, an empty value for the user parameter and a value for the messageCode and details parameters.

