如何使用Groovy在Mule中设置消息属性?

如何使用Groovy在Mule中设置消息属性?,mule,mule-studio,Mule,Mule Studio,如何使用Groovy在Mule中设置消息属性 我需要在Groovy脚本组件中设置消息属性。关于这个主题的文件似乎不容易找到 在脚本组件中,您可以使用作为实例的消息绑定,因此您可以使用该方法添加所需的任何属性。您可以按如下方式设置各个属性: message.setInvocationProperty('myFlowVariable', 'value') // sets a flow variable, like <set-variable/> message.setOutboundPr

如何使用Groovy在Mule中设置消息属性


我需要在Groovy脚本组件中设置消息属性。关于这个主题的文件似乎不容易找到

在脚本组件中,您可以使用作为实例的消息绑定,因此您可以使用该方法添加所需的任何属性。

您可以按如下方式设置各个属性:

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
message.setInvocationProperty('myFlowVariable','value')//设置一个流变量,如
message.setOutboundProperty('myProperty','value')//设置出站消息属性,如
message.setProperty('myInboundProperty','value',PropertyScope.INBOUND)//设置入站属性

这取决于您使用的是哪个版本的Mule EE(然后是Groovy),但在最新版本的Mule(3.7.x)中,最简单的方法是:

flowVars ['name_of_variable'] = 'value'
flowVars ['name_of_variable'] = 14
这适用于具有
调用
作用域的变量,如果要为
会话
作用域存储变量,则:

sessionVars ['name_of_variable'] = 'value'
sessionVars ['name_of_variable'] = 14
请使用Mulesoft提供的本网站编写脚本作为参考


以下是我是如何找到答案的:

如果缺少,请将架构添加到流: xmlns:scripting=”http://www.mulesoft.org/schema/mule/scripting"

现在,让我们使用Groovy使用自定义Foo对象设置会话变量“account”:

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

上面的脚本将把负载转换为空负载,因为它是一个转换器。如果这是一个问题,请尝试以下方法:

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


享受。:)

我尝试了类似上述的操作,但出现了一个错误。我的Groovy脚本文本如下所示:
message.setProperty('sourceConfig',payload,PropertyScope.OUTBOUND)返回有效负载我得到了以下错误:
根异常堆栈跟踪:groovy.lang.MissingPropertyException:没有这样的属性:类的PropertyScope:Script4
对不起,我只需要在脚本组件中导入PropertyScope。@RyanHoegg对我来说,这应该添加到答案
import org.mule.api.transport.PropertyScope
。。谢谢你们两位