简体   繁体   中英

MULE ESB: Saving result from Sql query in Session Variable

I have created a sample mule application that fetches one row from my database. It fetches USER_NAME and USER_ID from the Database. when I convert the result to JSON or XML I get the output as

[{"USER_ID":"U001","USER_NAME":"Dharmin"}]

Now i want to save USER_ID and USER_NAME in Session variables . Can someone guide me ?

edit: updated the basic flow image

http://imgur.com/PzvG5eW

After converting to JSON Add this :-

<json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>

and after that put the value into session variable using the following :-

<set-session-variable doc:name="Session Variable" value="message.payload.USER_ID" variableName="USER_ID"/> 

and

<set-session-variable doc:name="Session Variable" value="message.payload.USER_NAME" variableName="USER_NAME"/>

Do you need to use the results from the DB as a Json?

If not, don't even bother converting the values to JSON before saving them to the sessionVars. Access them directly from the Payload after the DB call:

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

And here's the configuration XML:

<flow name="testsFlow">

    <db:select config-ref="ORacle_DBCP_Config" doc:name="inputdata">
        <db:parameterized-query><![CDATA[SELECT 'U001' AS USER_ID, 'Dharmin' AS USER_NAME FROM DUAL]]></db:parameterized-query>
    </db:select>
    <set-session-variable variableName="userName" value="#[payload[0].USER_NAME]" doc:name="userName"/>
    <set-session-variable variableName="userID" value="#[payload[0].USER_ID]" doc:name="userID"/>
    <logger message="#[&quot;UserID: &quot; + sessionVars.userID + &quot; | UserName: &quot; + sessionVars.userName]" level="INFO" doc:name="Output the test"/>
</flow>

The output of the logger is:

processor.LoggerMessageProcessor: UserID: U001 | UserName: Dharmin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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