Reputation: 832
I am going to call multiple WSO2 ESB Sequence mediator using under the script mediator. This will loop by following XML tag format. Based on the sequencename, it will loop. I need to call sequence according to this xml tag value.
<message>
<postCallSequences>
<order>1</order>
<sequencename>gov:/repository/sequences/AB_SQ.xml</sequencename>
</postCallSequences>
<postCallSequences>
<order>2</order>
<sequencename>gov:/repository/sequences/XY_SQ.xml</sequencename>
</postCallSequences>
</message>
I have loop above XML like following script mediator.
var xmlDoc = new XML("XML_STRING_HERE");
for each (var p in xmlDoc..*::postCallSequences){
var sequencename = p.sequencename.toString();
var seq = mc.getSequence(sequencename);
seq.mediate(mc);
//seq.get(0).mediate(mc);
}
But it not fully running, it showing following error message.
[2019-03-25 14:39:31,960] [EI-Core] ERROR - SequenceMediator Error while building message. null
java.lang.ClassCastException
[2019-03-25 14:39:31,960] [EI-Core] ERROR - CommonScriptMessageContext Error while building message. null
java.lang.ClassCastException
[2019-03-25 14:39:31,973] [EI-Core] ERROR - ScriptMediator The script engine returned an error executing the inlined js script function mediate
com.sun.phobos.script.util.ExtendedScriptException: org.mozilla.javascript.WrappedException: Wrapped org.apache.synapse.SynapseException: Error while building message. null (<Unknown Source>#21) in <Unknown Source> at line number 21
at com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:68)
at javax.script.CompiledScript.eval(CompiledScript.java:92)
at org.apache.synapse.mediators.bsf.ScriptMediator.mediateForInlineScript(ScriptMediator.java:394)
at org.apache.synapse.mediators.bsf.ScriptMediator.invokeScript(ScriptMediator.java:289)
at org.apache.synapse.mediators.bsf.ScriptMediator.mediate(ScriptMediator.java:257)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:108)
I have refered following link as well. Still issue there. link
Upvotes: 1
Views: 1391
Reputation: 549
Since the script mediator uses different types of MessageContext objects (CommonScriptMessageContext for javascript and NashornJavaScriptMessageContext for Nashorn), we cannot directly pass the same messageContext object for the sequence mediator which expects an instance of org.apache.synapse.MessageContext. To avoid this ClassCastException issue, we can create a new messageContext of the expected type within the script mediator, and pass that to the sequence mediator, as below.
var seq = mc.getSequence(sequencename);
var newMC = mc.getEnvironment().createMessageContext();
newMC.setEnvelope(mc.getEnvelope());
seq.mediate(newMC);
Upvotes: 2
Reputation: 580
We cannot call the mediate method of a sequence mediator inside a script because in the script mediator, the message context object used is of CommonScriptMessageContext. We can access a sequence mediator using this object. But when the sequence mediator executes, there is a place where the message context object is cast to Axis2MessageContext [1]. CommonScriptMessageContext cannot be cast to Axis2MessageContext, which will result in a class cast exception.
As an alternative to your requirement, instead of script mediator, we can use foreach mediator[2]. Following is a sample configuration which can achieve the same as above.
<foreach expression="//postCallSequences">
<sequence>
<property name="seq" expression="//sequencename"/>
<sequence key="{$ctx:seq}"/>
</sequence>
</foreach>
Upvotes: 3