-
Notifications
You must be signed in to change notification settings - Fork 82
JavaScriptSender
The JavascriptSender can be used to execute javascript functions from a given javascript file. The result of the specified javascript function will be the output of the sender. The parameters of the javascript function to run are given as parameters by the adapter configuration. The javascript sender uses the following attributes:
- jsFileName: Give the path to the javascript file containing the functions to run.
- jsFunctionName: Give the name of the function from the javascript file to be run (first), the default value is "main".
- engineName: Give the name of the javascript engine to use. There are currently two options, Rhino and J2V8, the default value is "J2V8".
The sender can use either Rhino or J2V8 to execute the javascript function, J2V8 gives better performance results meaning faster execution times, but if it is not possible to use J2V8 using Rhino is still an option. The sender doesn't accept nor uses the given input, instead for each argument for the jsFunctionName method, you will need to create a parameter on the sender.
The following is a simple example configuration for using the JavascriptSender:
Configuration:
<Pipe name="Javascript">
<Sender className="nl.nn.adapterframework.senders.JavascriptSender"
jsFileName="JavaScript/JavascriptTest.js"
jsFunctionName="f1"
engineName="Rhino">
<Param name="x" type="INTEGER" sessionKey="originalMessage"/>
<Param name="y" type="INTEGER" value="2"/>
</Sender>
<Forward name="success" path="EXIT" />
<Forward name="exception" path="EXIT" />
</Pipe>With JavascriptTest.js:
function f1(x,y){
var result = x + y;
return ""+result;
}The file JavascriptTest.js found in the folder JavaScript contains the function f1(x,y). This function will add the values of x and y and return the result as a string. It is recommended to have the result of the javascript function be of type String, as the ouput of the sender will be of type String. The engine name is set as "Rhino" so the Rhino engine will be used. The value of x is an integer set using the originalMessage sessionKey and the value of y is given as an integer with value 2. The result of the sender will be the input message incremented by 2.
When using the V8 engine it is also possible to call other senders given in the JavascriptSender. The following example will illustrate this:
Configuration:
<Pipe name="JavascriptWithCallback">
<Sender className="nl.nn.adapterframework.senders.JavascriptSender"
jsFileName="JavaScript/JavascriptTest.js"
jsFunctionName="f2"
engineName="J2V8">
<Param name="x" type="INTEGER" sessionKey="originalMessage"/>
<Param name="y" type="INTEGER" value="2"/>
<Sender name="echoFunction" className="nl.nn.adapterframework.senders.EchoSender"/>
</Sender>
<Forward name="success" path="EXIT" />
<Forward name="exception" path="EXIT" />
</Pipe>With JavascriptTest.js:
function f2(x, y) {
var a = x * y;
var b = echoFunction(a);
return b;
}The name of the specified sender will be the name of the function used to call the sender, in this example "echoFunction". When executing the javascript function the EchoSender will be called and the result will be stored in var b. Note that this functionality is only possible when using J2V8.