Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can to create a JavaScript Hello World in a Mule component

I am trying to build a simple script inside a Mule component but I can't seem to find any documentation on how to get me started.

The JavaScript Component Reference shares no ideas on how to get something simple to run.

like image 272
Valentin Despa Avatar asked Feb 13 '23 19:02

Valentin Despa


1 Answers

Here is a simple example:

JavaScript in a Mule Component

You send a JSON encoded array and the script will return you the sum. Simple!

There is the flow:

<flow name="calculateFlow1" doc:name="calculateFlow1">
    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8089" doc:name="HTTP"/>
    <byte-array-to-string-transformer doc:name="Byte Array to String"/>
    <scripting:component doc:name="JavaScript">
        <scripting:script engine="JavaScript">
            <scripting:text><![CDATA[
                var a = eval('(' + payload + ')');
                for (var i = 0, sum = 0; i < a.length; sum += a[i++]);
                message.setPayload(sum + "");
                result = message;
        ]]></scripting:text>

        </scripting:script>
    </scripting:component>
</flow>

Variables already given by Mule: message, payload.

like image 131
Valentin Despa Avatar answered Apr 30 '23 15:04

Valentin Despa