Upload
drajesh-kumar
View
303
Download
4
Embed Size (px)
Citation preview
MuleESB : Data Retrieval from DB
In this tutorial we are going to fetch the records from Mysql Table by using MySql Database connector with select operation
Pre-requisites :1.Anypoint Studio 5.2.02.Mule Server 3.7.0/3.7.13.Mysql Database v6.x4.Database Table script
MySql Table Script :
CREATE DATABASE IF NOT EXISTS `muleesb`;USE `muleesb`;
DROP TABLE IF EXISTS `ESB_version`;CREATE TABLE `ESB_version` (`server_id` int(11) NOT NULL AUTO_INCREMENT,`server_version` varchar(45) DEFAULT NULL,`release_date` datetime DEFAULT NULL,PRIMARY KEY (`server_id`),KEY `index1` (`server_id`) USING BTREE) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
LOCK TABLES `ESB_version` WRITE;
INSERT INTO `ESB_version` VALUES (1,'3.7.0','2015-07-07 00:00:00'),(2,'3.7.0','2015-07-07 00:00:00'),(3,'3.7.0','2015-07-07 00:00:00'),(4,'3.7.0','2015-07-07 00:00:00');
UNLOCK TABLES;
Mule Flow :
In the above flow i am fetching the table data from database connector as ArrayList and this is list is converted to JSON string in the last transformer and the JSON will be returned to the calling http request .
Mule Source xml :
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<db:mysql-config name="MySQL_Configuration" host="127.0.0.1" port="3306" user="anil" password="anil" database="muleesb" doc:name="MySQL Configuration"/>
<flow name="fetchmysqldataFlow"><http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/><db:select config-ref="MySQL_Configuration" doc:name="Database"><db:parameterized-query><![CDATA[select * from muleesb.ESB_version]]></db:parameterized-query></db:select><logger message="no of records fetched : #[payload.size()]" level="INFO" doc:name="Logger"/><json:object-to-json-transformer mimeType="application/json" doc:name="Object to JSON"/></flow>
Flow Request : http://localhost:8081
Flow Response:
[{"server_version":"3.7.0","release_date":1436207400000,"server_id":1},
{"server_version":"3.7.0","release_date":1436207400000,"server_id":2},
{"server_version":"3.7.0","release_date":1436207400000,"server_id":3},
{"server_version":"3.7.0","release_date":1436207400000,"server_id":4}]