Sample 362: Perform Database Lookups and Updates in the Same Mediation Sequence

<definitions xmlns="http://ws.apache.org/ns/synapse"> <sequence name="main"> <in> <send> <endpoint> <address uri="http://localhost:9000/services/SimpleStockQuoteService"/> </endpoint> </send> </in> <out> <log level="custom"> <property name="text" value="** Reporting to the Database **"/> </log> <dbreport> <connection> <pool> <driver>org.apache.derby.jdbc.ClientDriver</driver> <url>jdbc:derby://localhost:1527/synapsedb;create=false</url> <user>synapse</user> <password>synapse</password> </pool> </connection> <statement> <sql>update company set price=? where name =?</sql> <parameter xmlns:m0="http://services.samples" expression="//m0:return/m0:last/child::text()" type="DOUBLE"/> <parameter xmlns:m0="http://services.samples" expression="//m0:return/m0:symbol/child::text()" type="VARCHAR"/> </statement> </dbreport> <log level="custom"> <property name="text" value="** Looking up from the Database **"/> </log> <dblookup> <connection> <pool> <driver>org.apache.derby.jdbc.ClientDriver</driver> <url>jdbc:derby://localhost:1527/synapsedb;create=false</url> <user>synapse</user> <password>synapse</password> </pool> </connection> <statement> <sql>select * from company where name =?</sql> <parameter xmlns:m0="http://services.samples" expression="//m0:return/m0:symbol/child::text()" type="VARCHAR"/> <result name="stock_price" column="price"/> </statement> </dblookup> <log level="custom"> <property name="text" expression="fn:concat('Stock price - ',get-property('stock_price'))"/> </log> <send/> </out> </sequence> </definitions>

Objective

Sample 360 and sample 361 shows how to use the dblookup mediator and dbreport mediator separately. This sample combines them in a single mediation sequence to perform both database lookup and update operations.

Pre-requisites

  • Setup a Derby database as described in the database setup guide
  • Deploy the SimpleStockQuoteService in the sample Axis2 server and start Axis2
  • Start Synapse using the configuration numbered 362 (repository/conf/sample/synapse_sample_362.xml)
    Unix/Linux: sh synapse.sh -sample 362
    Windows: synapse.bat -sample 362

Executing the Client

In this sample, the dbreport mediator works the same way as in sample 361. It updates the price for the given company using the response messages content. Then the dblookup mediator reads the last updated value from the company database and logs it to the console.

Run the sample client as follows.

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=IBM

Synapse will update the database using the stock quote value available in the response sent by Axis2. Then the same value will be retrieved from the database and logged as follows.

INFO LogMediator text = ** Reporting to the Database ** ... INFO LogMediator text = ** Looking up from the Database ** ... INFO LogMediator text = Stock price - 153.47886496064808

Back to Catalog