在Data weave中访问服务器和mule上下文对象

时间:2017-07-20 07:28:39

标签: mule-component dataweave

我正在尝试访问DW中的服务器和mule版本信息但是我收到以下消息。

DWL如下所示。如果我在下面犯了任何错误,请告诉我。我已经尝试了两种语法来访问它。

%dw 1.0
%output application/json
---
{
errorType : payload.errorType,
env: server['host'],
host: server['host'],
ip: server['ip'],
javaVersion: server['javaVersion'],
javaVendor: server['javaVendor'],
osName: server['osName'],
osVersion: server['osVersion'],
muleVersion: mule.version,
clusterId: mule.clusterId,
nodeId: mule.nodeId
}

Message               : Exception while executing: 
env: server['host'],
     ^
There is no variable named 'server'.
Payload               : {correlationId=046b6c7f-0b8a-43b9-b35d-6489e6daee91, message=This is the test message to test structured log, errorType=ERROR, applicationName=common wrappers - logging wrapper}
Payload Type          : java.util.HashMap
Element               : /wrapper-logger/processors/1 @ common-wrappers:wrapper-logger.xml:16 (Transform Message)
Element XML           : <dw:transform-message doc:name="Transform Message" metadata:id="38c29630-7d7c-48fc-a692-2407d0105cab">
                        <dw:input-payload doc:sample="sample_data\list_HashMap.dwl"></dw:input-payload>
                        <dw:set-payload>%dw 1.0%output application/json---{errorType : payload.errorType,env: server['host'],host: server['host'],ip: server['ip'],javaVersion: server['javaVersion'],javaVendor: server['javaVendor'],osName: server['osName'],osVersion: server['osVersion'],muleVersion: mule.version,clusterId: mule.clusterId,nodeId: mule.nodeId,applicationName : payload.applicationName,correlationId: payload.correlationId,correlationSequence: "To be decided",correlationGroupSize: 5,timeZone: server.timeZone,timeStamp: server.dateTime,muleFlow: "get-user-record",stackTrace: "A complete Stack Trace",message: payload.message}</dw:set-payload>
                        </dw:transform-message>
--------------------------------------------------------------------------------
Root Exception stack trace:
com.mulesoft.weave.mule.exception.WeaveExecutionException: Exception while executing: 
env: server['host'],
     ^
There is no variable named 'server'.
    at com.mulesoft.weave.mule.exception.WeaveExecutionException$.apply(WeaveExecutionException.scala:10)

1 个答案:

答案 0 :(得分:0)

您可以定义Global函数以获取服务器详细信息并从dataweave调用它。

<configuration doc:name="Configuration">
    <expression-language>
    <import class = "org.mule.util.NetworkUtils"/>
    <import class="java.lang.System" />
        <global-functions>
            def getServerDetails() { 
            String ip = NetworkUtils.getLocalHost().getHostAddress();
            String name = NetworkUtils.getLocalHost().getHostName();
            String osName = System.getProperty("os.name");
            String osVersion = System.getProperty("os.version");
            String javaVersion = System.getProperty("java.version");
            String javaVendor = System.getProperty("java.vendor");
            return [ip, name, osName, osVersion, javaVersion, javaVendor];
            }
        </global-functions>
    </expression-language>
</configuration>

从dataweave调用

{
    ip: getServerDetails()[0],
    host: getServerDetails()[1],
    osName: getServerDetails()[2],
    osVersion: getServerDetails()[3],
    javaVersion: getServerDetails()[4],
    javaVendor: getServerDetails()[5]
}