2017-04-24 75 views
0

我試圖一個ArrayList保存到flowVar &然後在flowVar在foreach語句中迭代,代碼如下:騾:遍歷的ArrayList

有效載荷[2]是一個陣列的ArrayList部分。

 <foreach collection="#[flowVars['ID']]" doc:name="For Each"> 

     .... 


    </foreach> 

不過,我得到以下錯誤,當我嘗試運行這段代碼:

org.mule.exception.DefaultMessagingExceptionStrategy:


消息:未聲明( org.mule.mvel2.ScriptRuntimeException)。消息有效負載是類型:鏈接列表

任何想法?

回答

2

你可以做到這一點容易如下面的示例聲明一個變量java.util.ArrayList如下: -

<set-variable variableName="ID" value="#[new java.util.ArrayList(Arrays.asList('abc','def',66))]" doc:name="Variable"/> 

<foreach collection="#[flowVars.ID]" counterVariableName="i" doc:name="For Each"> 
<logger message="Value:- #[message.payload]" level="INFO" doc:name="Logger"/> 
</foreach> 

注意:如果你想獲得ArrayList的一個具體指標特別,你可以做到這一點: -

<logger message="value at index 2: #[flowVars.ID[2]]" level="INFO" doc:name="Logger"/>   

或者表達式成分

<set-variable variableName="ID" value="#[new java.util.ArrayList(Arrays.asList('abc','def',66))]" doc:name="Variable"/> 

<expression-component doc:name="Expression"><![CDATA[ 
    for(int i=0;i<flowVars.ID.size();i++) 
     { 
     System.out.println("Value "+ flowVars.ID[i]); 
     } 

    ]]></expression-component> 
+0

快速問題Anirban,如果我正在處理一個數組的數組做#[flowVars.ID [i] [2]]應該正確嗎?我有一個數組數組,其中我需要檢查數組中的每個第二個元素是否存在於數據庫中 – insaneyogi

0

@insaneyogi,這裏是一個流程示例,超過的ArrayList作爲flowVar迭代:

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" 
    xmlns:spring="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd 
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd 
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd"> 
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/> 
    <flow name="test-foreachFlow"> 
     <http:listener config-ref="HTTP_Listener_Configuration" path="/foreach" doc:name="HTTP"/> 
     <expression-component doc:name="Expression"><![CDATA[myArray = new java.util.ArrayList(); 
myArray.add("el1"); 
myArray.add("el2"); 
myArray.add("el3"); 

flowVars.myArray = myArray;]]></expression-component> 
     <foreach collection="#[flowVars.myArray]" doc:name="For Each"> 
      <logger level="INFO" doc:name="Logger" message="#[payload]"/> 
     </foreach> 
    </flow> 
</mule>