2013-11-21 61 views

回答

11

可以按照如下设置单独的属性:

message.setInvocationProperty('myFlowVariable', 'value') // sets a flow variable, like <set-variable/> 
message.setOutboundProperty('myProperty', 'value') // sets an outbound message property, like <set-property/> 
message.setProperty('myInboundProperty', 'value', PropertyScope.INBOUND) // sets an inbound property 
+0

我尝试了类似上面的内容,但出现错误。我的Groovy脚本文本如下所示:'message.setProperty('sourceConfig',payload,PropertyScope.OUTBOUND) return payload;'我得到以下错误:'Root Exception stack trace: groovy.lang.MissingPropertyException:No such property :PropertyScope for class:Script4' – GarySharpe

+3

对不起,我只需要在脚本组件中导入PropertyScope。 – GarySharpe

+1

@RyanHoegg对我来说,这应该被添加到回答'import org.mule.api.transport.PropertyScope' ..谢谢你们俩 – mCeviker

2

这取决于你使用的是哪个版本的骡子EE的(等于是Groovy的),但在最新版本的骡子(3.7.x)最简单的方法是:

flowVars ['name_of_variable'] = 'value' 
flowVars ['name_of_variable'] = 14 

这与Invocation范围内的变量,如果你婉存储变量Session范围,则:

sessionVars ['name_of_variable'] = 'value' 
sessionVars ['name_of_variable'] = 14 

请使用本网站从Mulesoft脚本作为参考。

https://docs.mulesoft.com/mule-user-guide/v/3.7/script-component-reference

1

这里是我想通了:

如果缺少添加架构到您的流量: 的xmlns:脚本=“http://www.mulesoft.org/schema/mule /脚本” http://www.mulesoft.org/schema/mule/scriptinghttp://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd

现在让我们用设置会话变量与自定义的Foo对象‘账户’的Groovy:

<scripting:transformer doc:name="Script"> 
    <scripting:script engine="groovy"><![CDATA[ 
     com.test.Foo f = new com.test.Foo(); 
     f.setAccountId('333'); 
     return message.setSessionProperty('account',f);]]> 
     </scripting:script> 
    </scripting:transformer> 

上面的脚本会将您的Payload转换为NullPayload,因为它是一个变压器。如果这是一个问题,请试试这个:

<enricher target="#[sessionVars['account']]"> 
    <scripting:transformer doc:name="Script"> 
    <scripting:script engine="groovy"><![CDATA[ 
     com.test.Foo f = new com.test.Foo(); 
     f.setAccountId('333'); 
     return f;]]> 
    </scripting:script> 
    </scripting:transformer> 
</enricher> 

享受。 :)