获取消息入站属性

时间:2013-10-11 18:01:15

标签: mule

我无法获取邮件入站属性,不知何故它以null为空,这里是代码:

<scripting:transformer name="noopLoggingTransformer">
    <scripting:script engine="groovy">
        log.info "${message.getInboundProperty('user-agent')}"
        muleContext.client.dispatch('vm://log-request.in', message)
        message
     </scripting:script>
</scripting:transformer>

<pattern:web-service-proxy name="service" transformer-refs="noopLoggingTransformer" inboundAddress="${serverName}/services/Logradouros/LogradouroServico" outboundAddress="${targetServer}/servicos/v2/service.svc" wsdlFile="LogradouroServicos.wsdl">
</pattern:web-service-proxy>

<jdbc-ee:postgresql-data-source name="WSA" user="${database.user}" password="${database.pass}" url="${database.url}" transactionIsolation="UNSPECIFIED" doc:name="PostgreSQL Data Source">
</jdbc-ee:postgresql-data-source>

<jdbc-ee:connector name="jdbcConnector" dataSource-ref="WSA" validateConnections="false" transactionPerMessage="true" queryTimeout="10" pollingFrequency="10000" doc:name="JDBC"> 
    <jdbc-ee:query key="wsadb" value="insert into inbound_messages (payload, timestamp, agent, ip_from, endpoint, soap_operation) values ('', now(), #[groovy: return message.getInboundProperty('user-agent').toString()], #[groovy: return message.getInboundProperty('MULE_REMOTE_CLIENT_ADDRESS').toString()], #[groovy: return message.getInboundProperty('http.request').toString()], '');"></jdbc-ee:query>  
</jdbc-ee:connector>

<flow name="log-request" doc:name="log-request"> 
    <vm:inbound-endpoint path="log-request.in" />
    <logger message="#[message.inboundProperties['user-agent']]" level="INFO" doc:name="Logger"/>
    <jdbc-ee:outbound-endpoint exchange-pattern="one-way" queryKey="wsadb" responseTimeout="10000" queryTimeout="-1" connector-ref="jdbcConnector" doc:name="Persist raw message" />
</flow>

打印出来的是“null”,我不知道如何让它从消息中打印属性...

2 个答案:

答案 0 :(得分:9)

无需使用Groovy:

<logger message="#[message.inboundProperties['user-agent']]" level="INFO" doc:name="Logger"/>

但更大的问题是,在发送到vm://log-request.in之前,您没有将入站属性传播到出站范围。

您需要传播要继续使用的属性:

<scripting:transformer name="noopLoggingTransformer">
    <scripting:script engine="groovy">
        def props = [:]
        props['User-Agent'] = message.getProperty('User-Agent', org.mule.api.transport.PropertyScope.INBOUND)
        muleContext.client.dispatch('vm://log-request.in', payload, props)
        message
     </scripting:script>
</scripting:transformer>

当然,您可以迭代入站属性名称以将其全部复制。

答案 1 :(得分:1)

属性名称为User-Agent,正如大卫指出的那样,#message.inboundProperties['User-Agent']]是获得它的MEL方式。

当您尝试访问User-Agent时,是否确定mule message属性设置了inbound。它并不总是存在

除非您手动复制属性,否则不会传播<copy-properties propertyName="*"/>属性

{{1}}