I have an inbound-endpoint that retrieves messages from a RabbitMQ queue and then triggers a sequence. In this sequence, I want to use the script mediator to run a JavaScript script.
However, I’ve been struggling with this for two days. I don’t know where to place the JavaScript scripts. I’ve followed some examples found here, but nothing seems to work. A little help would be greatly appreciated.
Here are the details: Iam using docker, with WSO2MI 4.3
My sequence :
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="TestScriptSequence" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<!-- Log initial -->
<log level="custom">
<property name="Status" value="Message received"/>
<property expression="json-eval($)" name="Payload"/>
</log>
<!-- Appel du Script Mediator -->
<localEntry key="DisplayNameScript" src="file:scripts/JS/test..js"/>
<script function="testFunction" key="DisplayNameScript" language="js"/>
<!-- Log après le traitement -->
<log level="custom">
<property name="Status" value="Message processed by script"/>
<property expression="json-eval($)" name="Payload"/>
</log>
</sequence>
My script Javascript
function testFunction(mc) {
// Récupération du logger
var log = mc.getServiceLog();
log.info("Script test.js exécuté.");
// Récupération du message JSON
var payload = mc.getPayloadJSON();
log.info("Payload initial : " + JSON.stringify(payload));
// Modification du message
payload.processed = true;
payload.message = "Traitement réussi par test.js";
mc.setPayloadJSON(payload);}
I have always this logs
[2025-01-01 16:58:58,723] DEBUG {ScriptMediator} - {inboundendpoint:RabbitMQInboundEndpoint} Start : Script mediator
[2025-01-01 16:58:58,723] DEBUG {ScriptMediator} - {inboundendpoint:RabbitMQInboundEndpoint} Scripting language : js source loaded with key : Value {name ='null', keyValue ='DisplayNameScript'} function : testFunction
[2025-01-01 16:58:59,118] ERROR {ScriptMediator} - {inboundendpoint:RabbitMQInboundEndpoint} The script engine returned a NoSuchMethodException executing the external js script : Value {name ='null', keyValue ='DisplayNameScript'} function testFunction java.lang.NoSuchMethodException: testFunction
at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.noSuchMethod(GraalJSScriptEngine.java:583)
at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.invokeFunction(GraalJSScriptEngine.java:571)
at org.apache.synapse.mediators.bsf.ScriptMediator.mediateWithExternalScript(ScriptMediator.java:390)
at org.apache.synapse.mediators.bsf.ScriptMediator.invokeScript(ScriptMediator.java:327)
at org.apache.synapse.mediators.bsf.ScriptMediator.mediate(ScriptMediator.java:290)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:126)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:74)
at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:158)
at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectInbound(Axis2SynapseEnvironment.java:495)
at org.wso2.carbon.inbound.endpoint.protocol.rabbitmq.RabbitMQInjectHandler.onMessage(RabbitMQInjectHandler.java:92)
at org.wso2.carbon.inbound.endpoint.protocol.rabbitmq.RabbitMQConsumer.handleDelivery(RabbitMQConsumer.java:213)
at com.rabbitmq.client.impl.ConsumerDispatcher$5.run(ConsumerDispatcher.java:149)
at com.rabbitmq.client.impl.ConsumerWorkService$WorkPoolRunnable.run(ConsumerWorkService.java:111)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:833)
[2025-01-01 16:58:59,128] WARN {Axis2SynapseEnvironment} - {inboundendpoint:RabbitMQInboundEndpoint} Exception encountered but no fault handler found - message dropped
[2025-01-01 16:58:59,128] ERROR {ForgivingExceptionHandler} - Consumer org.wso2.carbon.inbound.endpoint.protocol.rabbitmq.RabbitMQConsumer@96832c1 (amq.ctag-rneIgy3iIpd2GtLvsBKbIA) method handleDelivery for channel AMQChannel(amqp://guest@192.168.65.254:5672/,1) threw an exception for channel AMQChannel(amqp://guest@192.168.65.254:5672/,1) org.apache.synapse.SynapseException: The script engine returned a NoSuchMethodException executing the external js script : Value {name ='null', keyValue ='DisplayNameScript'} function testFunction
at org.apache.synapse.mediators.AbstractMediator.handleException(AbstractMediator.java:376)
at org.apache.synapse.mediators.bsf.ScriptMediator.invokeScript(ScriptMediator.java:340)
at org.apache.synapse.mediators.bsf.ScriptMediator.mediate(ScriptMediator.java:290)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:126)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:74)
at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:158)
at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectInbound(Axis2SynapseEnvironment.java:495)
at org.wso2.carbon.inbound.endpoint.protocol.rabbitmq.RabbitMQInjectHandler.onMessage(RabbitMQInjectHandler.java:92)
at org.wso2.carbon.inbound.endpoint.protocol.rabbitmq.RabbitMQConsumer.handleDelivery(RabbitMQConsumer.java:213)
at com.rabbitmq.client.impl.ConsumerDispatcher$5.run(ConsumerDispatcher.java:149)
at com.rabbitmq.client.impl.ConsumerWorkService$WorkPoolRunnable.run(ConsumerWorkService.java:111)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NoSuchMethodException: testFunction
at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.noSuchMethod(GraalJSScriptEngine.java:583)
at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.invokeFunction(GraalJSScriptEngine.java:571)
at org.apache.synapse.mediators.bsf.ScriptMediator.mediateWithExternalScript(ScriptMediator.java:390)
at org.apache.synapse.mediators.bsf.ScriptMediator.invokeScript(ScriptMediator.java:327)
... 12 more
[2025-01-01 16:58:59,132] INFO {RabbitMQConsumer} - The connection to the messaging server was shut down. Consumer tag: amq.ctag-rneIgy3iIpd2GtLvsBKbIA
And then i a am not able to find a way
My folder script are in /scrips/Js i follow the example : WSO2 ESB Can't run Groovy script stored in registry
Thousand tanks for you help
If you want to use Script mediator
in way you describe below:
<script function="testFunction" key="DisplayNameScript" language="js"/>
You need to add localEntry artifact file: ../repository/deployment/server/synapse-configs/default/local-entries/DisplayNameScript.xml
like this:
<?xml version="1.0" encoding="UTF-8"?>
<localEntry xmlns="http://ws.apache.org/ns/synapse" key="DisplayNameScript">
function testFunction(mc) {
var log = mc.getServiceLog();
log.info("Script test.js execute.");
var payload = mc.getPayloadJSON();
log.info("Payload initial : " + JSON.stringify(payload));
//...
<description/>
</localEntry>
The key
corresponds to the key of the localEntry artifact file.
Second way is to store file in registry as XML file. For example:
../registry/config/scripts/testJS.xml
<root>
function testFunction(mc) {
var log = mc.getServiceLog();
log.info("Script test.js execute.");
var payload = mc.getPayloadJSON();
log.info("Payload initial : " + JSON.stringify(payload));
//...
</root>
and you can use it like below:
<script language="js" key="conf:/scripts/testJS.xml" function="testFunction">