为什么在从 MEL 访问 ArrayList 的大小时会出现运行时异常?

Why do I get a Runtime Exception Accessing the size of an ArrayList from MEL?

在 Mulesoft 中,我将 ArrayList 存储为具有一个元素的出站 属性。它看起来像这样:

但是,如果我尝试访问此数组的大小,则会出现错误并且无法弄清楚原因:

错误是

[Error: object is not an instance of declaring class]
[Near : {... message.outboundProperties.crm ....}]
             ^
[Line: 1, Column: 1]

下面是抛出相同错误的流程:

    <?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:quartz="http://www.mulesoft.org/schema/mule/quartz" 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/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd
http://www.mulesoft.org/schema/mule/quartz http://www.mulesoft.org/schema/mule/quartz/current/mule-quartz.xsd">
    <quartz:connector name="Quartz" validateConnections="true" doc:name="Quartz"/>
    <flow name="tempFlow">
        <quartz:inbound-endpoint jobName="job1" repeatInterval="1" repeatCount="0" connector-ref="Quartz" name="runOnce" doc:name="Quartz">
            <quartz:event-generator-job>
                <quartz:payload>foo</quartz:payload>
            </quartz:event-generator-job>
        </quartz:inbound-endpoint>
        <message-properties-transformer doc:name="Message Properties">
            <add-message-property key="crmRequests" value="#[[]]"/>
        </message-properties-transformer>
        <dw:transform-message doc:name="Copy_of_buildUpdateRequest">
            <dw:set-payload><![CDATA[
%dw 1.0
%output application/java
---
{
    statecode: 0
}
]]></dw:set-payload>
        </dw:transform-message>
        <expression-component doc:name="Copy_of_Expression"><![CDATA[#[message.outboundProperties.crmRequests.add(payload)]]]></expression-component>
        <set-payload value="#[message.outboundProperties.crmRequests.size()]" doc:name="Set Payload"/>
        <logger level="INFO" doc:name="Logger"/>
    </flow>
</mule>

如果您已将 ArrayList 设置为存储在 crmRequests 中。为什么在调试视图中显示整个 #[[message.outboundProperties.crmRequest] 似乎有问题。它应该出现在 outboundProperties 选项卡中,名称为 crmRequests,类型为 ArrayList。检查一下。

也可以尝试使用 #[message.outboundProperties.'crmRequests'.size()]#[message.outboundProperties.['crmRequests'].size()] 进行设置。

编辑:示例流程,做了一个小改动,在 groovy 本身内部设置了 outboundProperties,而不是使用 MessageProperties,然后使用表达式组件。

<flow name="checkFlow">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
    <dw:transform-message doc:name="Transform Message">
        <dw:set-payload><![CDATA[%dw 1.0
 %output application/java
 ---
{
 statusCode:0
}]]></dw:set-payload>
    </dw:transform-message>
    <scripting:component doc:name="Groovy">
        <scripting:script engine="Groovy"><!  [CDATA[message.setOutboundProperty('crmRequests',payload);
return payload;]]></scripting:script>
    </scripting:component>
     <set-payload value="#[message.outboundProperties.crmRequests.size()]" doc:name="Set Payload"/>

    <logger  level="INFO" doc:name="Logger"/>
</flow>
#[message.outboundProperties.crmRequests.size()]

Mu​​le 无法解决高于 MEL 的问题。按照此两步法获得所需结果。使用:

<set-variable variableName="crmRequestsList" value="#[message.outboundProperties.crmRequests]" doc:name="Variable" />
<set-payload value="#[flowVars.crmRequestsList.size()]" doc:name="Set Payload"/>

而不是:

<set-payload value="#[message.outboundProperties.crmRequests.size()]" doc:name="Set Payload"/>

将表达式括在括号内:

<set-payload value="#[(message.outboundProperties.crmRequests).size()]" doc:name="Set Payload"/>

如需进一步测试,您可以尝试其他 java.util.ArrayList 方法,例如:(message.outboundProperties.crmRequests).get(0)

您可以设置一个变量并为其赋值,然后在您的负载中调用它,您可以在那里查看它。