简体   繁体   中英

Save value from database in a session variable Mule esb

Im developing a sample bookstare in mule esb and Im using mysql as database. My database has a table named 'Stock' whose has 2 attr (isbn and quantity) for the registered books I offer on the form. What i want is checking the number of books the client ask by the form dont increase the existence books (quantity) on my database to do the order.

I have that flow:

<flow name="Facturacion" doc:name="Facturacion">
    <composite-source doc:name="Composite Source">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8086" doc:name="HTTP" path="Facturacion" transformer-refs="HttpToPedido"/>
        <file:inbound-endpoint responseTimeout="10000" doc:name="File" moveToDirectory="tmp" path="tmp/pedidos"/>
    </composite-source>
    <component doc:name="Generar Pedido" class="org.mule.components.GenerarPedido"/>
    <set-session-variable variableName="dataTemp" value="#[message.payload.cantidadPedida]" doc:name="Session Variable"/>
    <db:select config-ref="MySQL_Configuration" doc:name="Database">
        <db:parameterized-query><![CDATA[SELECT cantidad FROM stock WHERE isbn = #[payload.isbn]]]></db:parameterized-query>
    </db:select>
    <collection-splitter doc:name="Collection Splitter"/>
    <choice doc:name="Choice">
        <when expression="#[sessionVars['dataTemp'] &gt;= payload.cantidadPedida]">
            <vm:outbound-endpoint exchange-pattern="one-way" path="procesarPedido" doc:name="Procesar Pedido Disponible"/>
        </when>
        <otherwise>
            <vm:outbound-endpoint exchange-pattern="one-way" path="rechazarStock" doc:name="Rechazar por falta en Stock"/>
        </otherwise>
    </choice>
    <collection-aggregator failOnTimeout="true" doc:name="Collection Aggregator"/>
    <set-payload value="#[payload];" doc:name="Set Payload"/>
</flow>

Any solve to that? What i need is compare de value of the quantity of the book in order from payload with the value in my 'stock table' from my database.

Thanks!

I don't know exactly what you're trying to achieve, but to store the result of the database query in a session var, all you need to use is an enricher, where you specify the target to be the session var:

        <enricher target="#[sessionVars.test]" doc:name="Message Enricher">
           <!-- database call -->
        </enricher> 

If this is not what you're after, you can divulge further information about your usecase.

What about to use a filter?

The following app:

<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" />
<db:mysql-config name="MySQL_Configuration" url="jdbc:mysql://localhost:3306/esb?user=root&amp;amp;password=" />
<flow name="databasetestsFlow">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/" />
    <db:select config-ref="MySQL_Configuration" >
        <db:parameterized-query><![CDATA[select name from test where id=#[message.inboundProperties.'http.query.params'.id]]]></db:parameterized-query>
    </db:select>
    <expression-filter expression="#[payload.size()==1 &amp;&amp; payload[0].name==&quot;one&quot;]" />
    <set-payload value="PROCESSED" />
</flow>

returns "PROCESSED" only when the id is 1, for example:

curl http://localhost:8081\?id\=1

You could change the filter expression to something like:

#[sessionVars['dataTemp'] &gt;= payload[0].cantidadPedida]

You also can store the database results in a session var by using the target attribute in the db:select processor, for example:

target="#[sessionVars.results]"

HTH, Marcos

<db:select config-ref="MySQL_Configuration" doc:name="Database">
    <db:parameterized-query><![CDATA[SELECT cantidad FROM stock WHERE isbn = #[payload.isbn]]]></db:parameterized-query>
</db:select>
<set-session-variable variableName="dataTemp" value="#[payload.cantidad]" doc:name="dataTemp"/>

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