在 mule4 的 groovy 脚本中传递参数

Passing parameter inside the groovy script in mule4

我正在尝试通过 mule 中的 .properties 文件参数化用户名和密码。 groovy 脚本有 curl 命令。我在执行组件的参数部分定义了 VID 和 VPASS,并将其传递到脚本中,如下所示。在 运行 时,它无法解析 ID 和密码。如何在curl命令中传递这里的参数。

def proctext = 'curl -H "apiAccessKeyId: + vid" -H "apiSecretAccessKey**:+vpass**" -H "Accept:application/json" --form "file=@C:\Files\test.csv" --form "params={Type:Import}" -X POST http://mysuperserver/media/upload/'

您必须将 parameters element of the Scripting Module 中的参数设置为 DataWeave 表达式。

<flow name="groovy-paramsFlow" >
    <http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/"/>
    <set-variable value="#[123]" doc:name="Set Variable" variableName="myVar"/>
    <scripting:execute doc:name="Execute" engine="groovy">
        <scripting:code ><![CDATA[def proctext = 'curl -H "apiAccessKeyId: ' + vid + '" -H "apiSecretAccessKey: ' + vpass +'" -H "Accept:application/json" --form "file=@C:\Files\test.csv" --form "params={Type:Import}" -X POST http://mysuperserver/media/upload/']]></scripting:code>
        <scripting:parameters ><![CDATA[#[{ vid: "abcd", vpass: "efgh" }]
        ]]></scripting:parameters>
    </scripting:execute>
    <logger level="INFO" message="Payload #[payload]"/>
</flow>

输出

Payload curl -H "apiAccessKeyId: abcd" -H "apiSecretAccessKey: efgh" -H "Accept:application/json" --form "file=@C:\Files\test.csv" --form "params={Type:Import}" -X POST http://mysuperserver/media/upload/

Groovy 脚本的参数

有两种方法可以将值传入和传出 Groovy 脚本 https://simpleflatservice.com/mule4/ParametersForGroovyScripts.html

一种方法是使用常规变量。在 Groovy 脚本中,它们是变量 vars 的属性。 另一种方法是设置脚本的属性。通过这种方式,变量可以直接通过它们的名字来使用。 出路类似。在 Groovy 脚本之后,脚本的最后一条语句成为有效负载。要输出变量值,应更改 vars 的属性。 变量 vars 是不可改变的,所以现在可以添加新的属性,但属性本身是可变的,可以执行任何值。

注意事项 - 默认情况下,Mule 会尝试使用流,并且可能会为交互提供一些困难。例如,如果 Mule 中的变量是 JSON,它可能是一个流,因此在 Groovy 脚本中不能更改流中的任何属性。在这种情况下,最好将变量设置为 Java,然后可以更改属性。

Groovy 脚本组件

全流程

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

<mule xmlns:db="http://www.mulesoft.org/schema/mule/db"
    xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting"

 xmlns:file="http://www.mulesoft.org/schema/mule/file"

 xmlns:email="http://www.mulesoft.org/schema/mule/email" xmlns:wsc="http://www.mulesoft.org/schema/mule/wsc" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd

http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd

http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/wsc http://www.mulesoft.org/schema/mule/wsc/current/mule-wsc.xsd
http://www.mulesoft.org/schema/mule/email http://www.mulesoft.org/schema/mule/email/current/mule-email.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">

 <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="f7458033-5606-4521-ae2b-7c309c34bdb2" >

  <http:listener-connection host="0.0.0.0" port="8081" />

 </http:listener-config>

 <http:request-config name="HTTP_Request_configuration" doc:name="HTTP Request configuration" doc:id="13190c91-83f0-40bf-8288-1f1f330c0b48" >

  <http:request-connection host="localhost" port="8081" />

 </http:request-config>
    <email:smtp-config name="Email_SMTP" doc:name="Email SMTP" doc:id="45eb1460-05a9-41c5-80be-344a45b241e0" >
        <email:smtp-connection host="smtp.kclife.net" user="alex.kizub" password="8Abc#135" />
    </email:smtp-config>
    <flow name="workFlow" doc:id="ea9b69db-5516-4940-a81c-b88c3415c75d" >
        <http:listener doc:name="Listener" doc:id="81680002-4376-47d1-ada6-a4f18f978b36" config-ref="HTTP_Listener_config" path="/"/>
        <ee:transform doc:name="set myRegularVariable    and    myCollection" doc:id="e79005e0-6652-4cc6-8f83-807fc0432ed0" >
            <ee:message >
            </ee:message>
            <ee:variables >
                <ee:set-variable variableName="myRegularVariable" ><![CDATA[%dw 2.0
output application/java
---
'Hello']]></ee:set-variable>
                <ee:set-variable variableName="myCollection" ><![CDATA[%dw 2.0
output application/java
---
{
    start: now()
}]]></ee:set-variable>
            </ee:variables>
        </ee:transform>
        <scripting:execute engine="groovy" doc:name="Execute Groovy script" doc:id="b465ab5c-d2b7-4d86-860a-f0097aa30da4" >
            <scripting:code ><![CDATA[def string=vars.myRegularVariable + ' ' + myInScriptVaraiable
vars.myCollection['result']=string
string + '!' // return as payload]]></scripting:code>
            <scripting:parameters ><![CDATA[#[{myInScriptVaraiable:'World'}]]]></scripting:parameters>
        </scripting:execute>
        <logger level="INFO" doc:name="Log payload" doc:id="1f8b99ef-a4ce-42ec-8a2b-0b0e31b4128f" message="#[payload]"/>
        <logger level="INFO" doc:name="Log myCollection" doc:id="2cdd252d-3993-4dd5-b897-2456761ad293" message="#[vars.myCollection]"/>
    </flow>

</mule>