简体   繁体   English

如何在WSO2 ESB中的脚本中介器中调用Sequence中介器

[英]How to call Sequence mediator inside the script mediator in WSO2 ESB

I am going to call multiple WSO2 ESB Sequence mediator using under the script mediator. 我将使用脚本介体下的多个WSO2 ESB序列介体进行调用。 This will loop by following XML tag format. 这将通过遵循XML标签格式来循环。 Based on the sequencename, it will loop. 基于序列名,它将循环。 I need to call sequence according to this xml tag value. 我需要根据此xml标签值调用序列。

<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. 我在XML上面有循环,例如以下脚本介体。

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 链接

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. 我们无法在脚本内调用序列中介器的中介方法,因为在脚本中介器中,所使用的消息上下文对象是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 ]. 但是,当序列中介程序执行时,会有一个消息上下文对象被强制转换为Axis2MessageContext [ 1 ]的地方。 CommonScriptMessageContext cannot be cast to Axis2MessageContext, which will result in a class cast exception. CommonScriptMessageContext无法转换为Axis2MessageContext,这将导致类转换异常。

As an alternative to your requirement, instead of script mediator, we can use foreach mediator [2] . 作为您要求的替代方法,我们可以使用foreach中介器[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>

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 . 由于脚本调解器使用不同类型的MessageContext对象(JavaScript使用ComonScriptMessageContext,而Nashorn使用NashornJavaScriptMessageContext),因此我们无法直接为序列调解器传递相同的messageContext对象,后者需要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. 为避免此ClassCastException问题,我们可以在脚本介体中创建期望类型的新messageContext,并将其传递给序列介体,如下所示。

 var seq = mc.getSequence(sequencename); var newMC = mc.getEnvironment().createMessageContext(); newMC.setEnvelope(mc.getEnvelope()); seq.mediate(newMC); 
Please note that, since a new messageContext is created, we will have to set the expected attributes to the newly created message context. 请注意,由于创建了新的messageContext ,因此我们必须将期望的属性设置为新创建的消息上下文。 We can refer the methods exposed in CommonScriptMessageContext or NashornJavaScriptMessageContext to retrieve the values from the previous messageContext. 我们可以引用CommonScriptMessageContextNashornJavaScriptMessageContext中公开的方法来从先前的messageContext中检索值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM