【问题标题】:Mule - how to parse the JSON response of REST webservice and update databaseMule - 如何解析 REST Web 服务的 JSON 响应并更新数据库
【发布时间】:2017-02-05 03:56:58
【问题描述】:

我想编写一个 mule 应用程序,它将读取数据库中未处理的记录,将它们放入 JSON 有效负载中,然后点击 REST Web 服务,REST Web 服务(托管在不同的服务器上)将处理记录并返回 JSON 输出。现在我必须解析 JSON 并更新已处理标志的数据库。

我能够从 REST web 服务获得响应,我已经使用字节数组到字符串转换来登录记录器。

是否需要编写foreach组件一个一个地处理payload并更新数据库?

这是我的配置 XML

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

<mule xmlns:oauth2="http://www.mulesoft.org/schema/mule/oauth2" xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:json="http://www.mulesoft.org/schema/mule/json" 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:spring="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.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/oauth2 http://www.mulesoft.org/schema/mule/oauth2/current/mule-oauth2.xsd">
    <db:mysql-config name="MySQL_Configuration" host="localhost" port="3306" user="root" database="my_db_name" doc:name="MySQL Configuration"/>
    <http:request-config name="HTTP_Request_Configuration" protocol="HTTPS" host="example.net" port="8000" basePath="Salvage" doc:name="HTTP Request Configuration"/>
    <flow name="cwg_clientFlow">
        <poll doc:name="Poll">
            <db:select config-ref="MySQL_Configuration" doc:name="Database">
                <db:parameterized-query><![CDATA[SELECT * FROM cwg_ws_data WHERE SyncFlag = 0]]></db:parameterized-query>
            </db:select>
        </poll>
        <logger message="#[payload]" level="INFO" doc:name="Logger"/>
        <json:object-to-json-transformer   doc:name="Object to JSON"  encoding="UTF-8" mimeType="application/json"/>
        <logger message="JSON Payload is #[payload]" level="INFO" doc:name="Logger"/>
        <http:request config-ref="HTTP_Request_Configuration" path="/muleCWG" method="POST" doc:name="HTTP">
            <http:request-builder>
                <http:header headerName="access_token" value="U9P4CjhCsadIQzfJi13dHYdQLCfhmAi9OxYM8d7c"/>
            </http:request-builder>
            <http:success-status-code-validator values="200"/>
        </http:request>
        <byte-array-to-string-transformer doc:name="Byte Array to String"/>
        <logger message="webservice response #[payload]" level="INFO" doc:name="Logger"/>
    </flow>
</mule>

请指教。

-Paresh (kendreparesh@gmail.com)

【问题讨论】:

    标签: database parsing foreach mule response


    【解决方案1】:

    不管记录的数量是多少,批量更新都可以处理数千条记录。您可以尝试以下操作

    <flow name="testdbFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
        <set-payload value="{&quot;1234567890123456&quot;:&quot;Y&quot;,&quot;1234567890123457&quot;:&quot;Y&quot;}" mimeType="application/json" doc:name="Set Payload"/>
        <dw:transform-message doc:name="Transform Message">
            <dw:input-payload />
            <dw:set-payload><![CDATA[%dw 1.0
                %output application/json
                ---
                (payload mapObject {    
                    x: {
                        key : $$,
                        value : $
                    }
                }).*x]]>
            </dw:set-payload>
        </dw:transform-message>
        <json:json-to-object-transformer returnClass="java.util.List" doc:name="JSON to Object"/>
        <db:update config-ref="MySQL_Configuration" bulkMode="true" doc:name="Database">
            <db:dynamic-query><![CDATA[UPDATE cwg_ws_data SET SyncFlag = '#[payload.value]' WHERE IMEI = '#[payload.key]']]></db:dynamic-query>
        </db:update>
    </flow>
    

    我已经用相同的输入对其进行了测试,并且工作正常。 FYI dataweave 用于将 json 转换为有意义且可访问的有效负载。

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      您可以将 Bulk Update 与 mule bulk mode = "true"parameterized queryBatch processing 一起使用。但在您的情况下,批量更新会有所帮助。无需使用 for-each 范围,它会降低性能。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-13
        • 1970-01-01
        • 2011-02-10
        • 1970-01-01
        • 2015-12-14
        • 1970-01-01
        • 2014-02-04
        相关资源
        最近更新 更多