简体   繁体   中英

How get result of query in database in .txt file in mule esb?

My scenario is when I go to localhost:8081/search a simple html file must be loaded that has a text field (input) and a submit button.

I write a subject in text field and by clicking on the submit button, the subject must be transferred to my database for apply some SQL query then the result of query must be returned as .txt or .html file.

But when I look at the text file I just see very unusual character.

This my XMl flow code in mule

<http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" doc:name="HTTP Listener Configuration"/>
<db:mysql-config name="MySQL_Configuration" host="localhost" port="3306" user="root" password="Blue1234" database="news" doc:name="MySQL Configuration"/>
<http:request-config name="HTTP_Request_Configuration" host="localhost"  doc:name="HTTP Request Configuration" port="8081"/>
<file:connector name="file" writeToDirectory="C:\Users\Hersh\Desktop\file" autoDelete="true" streaming="true" validateConnections="true" doc:name="File" />
<flow name="parsetemplateFlow">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/web" doc:name="HTTP"/>
    <set-payload value="#[message.inboundProperties.'http.query.params'.q]" doc:name="Set Payload" mimeType="text/html"/>
    <db:select config-ref="MySQL_Configuration" doc:name="Database">
        <db:parameterized-query><![CDATA[select txt from news.nws where sub=#[message.payload]]]></db:parameterized-query>
    </db:select>
    <response>
        <http:static-resource-handler resourceBase="C:\Users\Hersh\Desktop\attach" defaultFile="index.html" doc:name="HTTP Static Resource Handler"/>
    </response>
    <logger message="#[message.payload]" level="INFO" doc:name="Logger"/>
    <set-payload value="#[message.payload]" doc:name="Set Payload"/>
    <response>
        <file:outbound-endpoint path="C:\Users\Hersh\Desktop\file" outputPattern="output.txt" connector-ref="file" responseTimeout="10000" doc:name="File"/>
    </response>
</flow>
</mule>

and this is a screenshot of flow

enter image description here

Ideally the structure of your flow should be something like this,

  • You take input through html using parse template/ HTTP Static Resource Handler
  • Use those values in DB
  • Write the response from DB into file using file component component.

If this is not your case comment what you need.

Sample configuration xml-

 <http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" doc:name="HTTP Listener Configuration"/>
<db:mysql-config name="MySQL_Configuration" host="localhost" port="3306" user="root" password="Blue1234" database="news" doc:name="MySQL Configuration"/>
<http:request-config name="HTTP_Request_Configuration" host="localhost"  doc:name="HTTP Request Configuration" port="8081"/>
<file:connector name="file" writeToDirectory="C:\Users\Hersh\Desktop\file" autoDelete="true" streaming="true" validateConnections="true" doc:name="File" />
<flow name="parsetemplateFlow">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/web" doc:name="HTTP"/>
        <http:static-resource-handler resourceBase="C:\Users\Hersh\Desktop\attach" defaultFile="index.html" doc:name="HTTP Static Resource Handler"/>
        <set-payload value="#[message.inboundProperties.'http.query.params'.q]" mimeType="text/html" doc:name="Set Payload"/>
    <db:select config-ref="MySQL_Configuration" doc:name="Database">
        <db:parameterized-query><![CDATA[select txt from news.nws where sub=#[message.payload]]]></db:parameterized-query>
    </db:select>
    <logger message="#[message.payload]" level="INFO" doc:name="Logger"/>
    <set-payload value="#[message.payload]" doc:name="Set Payload"/>
        <file:outbound-endpoint path="C:\Users\Hersh\Desktop\file" outputPattern="output.txt" connector-ref="file" responseTimeout="10000" doc:name="File"/>
</flow>

Database connector returns the data in the form of java.util.LinkedList (Java Collection format) that's why u are getting unusual characters. for getting plain text , u first have to convert the data from java object to plain text. u can use transform message for the conversion.

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