简体   繁体   English

在Mule流中使用SQL结果变量

[英]Use an SQL result variable in a Mule flow

I got a Mule flow with 2 queries. 我有2条查询的Mule流。 The first simply validates if a username is duplicated. 第一个简单地验证用户名是否重复。

Does something like this: SELECT count(*) AS TOTAL FROM user_tests WHERE username = #[json:username] AND test_message_title = #[json:test_message_title] 做这样的事情:从user_tests中选择COUNT(*)个总数,其中username =#[json:username] AND test_message_title =#[json:test_message_title]

The second one does an INSERT if TOTAL = 0. 如果TOTAL = 0,第二个将执行INSERT。

Now, i want to use the variable "TOTAL" in a choice inside the same flow. 现在,我想在同一流程内的选择中使用变量“ TOTAL”。 How do i do it? 我该怎么做?

Tried #[TOTAL=0], tried #[variable:TOTAL=0], and many others but couldn't make it work. 尝试#[TOTAL = 0],尝试#[variable:TOTAL = 0]和其他许多方法,但无法使其正常工作。

EDITED: I made a java component that retrieve the "0" or whatever number from the payload. 编辑:我做了一个Java组件,从有效载荷中检索“ 0”或任何数字。 How do i make the choice expression to compare it? 如何做出选择表达式进行比较?

Tried using Integer.valueOf(message.payload) = 0 in the choice expression but i get an InvalidExpressionException. 在选择表达式中尝试使用Integer.valueOf(message.payload)= 0,但我得到了InvalidExpressionException。

This is my entire flow: 这是我的整个流程:

<flow name="ADMIN_INSERT_TEST_FILE" doc:name="ADMIN_INSERT_TEST_FILE">
    <ajax:servlet-inbound-endpoint channel="/admin/save_test_message" responseTimeout="10000" doc:name="Ajax"/>
    <set-variable variableName="#['saved_payload']" value="#[payload]" doc:name="Save original payload"/>
    <jdbc:outbound-endpoint exchange-pattern="request-response" queryKey="validate_test_name" queryTimeout="-1" connector-ref="ExtendedRoutingConnector" doc:name="validate duplicated test name">
        <jdbc:query key="validate_test_name" value="SELECT count(*) AS TOTAL FROM user_tests WHERE username = #[json:username] AND test_message_title = #[json:test_message_title]"/>
    </jdbc:outbound-endpoint>
    <logger message="#[message.payload[0].TOTAL]" level="INFO" category="Total" doc:name="Logger"/>
    <choice doc:name="Choice">
        <when expression="#[message.payload[0].TOTAL == 0]">
            <processor-chain>
                <set-payload value="#[variable:saved_payload]" doc:name="Save Payload"/>
                <jdbc:outbound-endpoint exchange-pattern="request-response" queryKey="insert_user_test_message" queryTimeout="-1" connector-ref="ExtendedRoutingConnector" doc:name="insert user test message">
                    <jdbc:query key="insert_user_test_message" value="INSERT INTO user_tests (username, test_message_title, user_test_message) VALUES (#[json:username], #[json:test_message_title], #[json:message])"/>
                </jdbc:outbound-endpoint>
            </processor-chain>
        </when>
        <otherwise>
            <processor-chain>
                <logger message="Name #[json:test_message_title] already exists" level="INFO" doc:name="Logger"/>
            </processor-chain>
        </otherwise>
    </choice>
</flow>

Thanks in advance. 提前致谢。

Use MEL: 使用MEL:

#[message.payload[0].TOTAL == 0]

This will retrieve the value of the TOTAL column from the first row returned by your SELECT query. 这将从SELECT查询返回的第一行中检索TOTAL列的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM