6
MuleESB : Data Retrieval from DB

Mule esb db component

Embed Size (px)

Citation preview

Page 1: Mule esb db component

MuleESB : Data Retrieval from DB

Page 2: Mule esb db component

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

Page 3: Mule esb db component

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;

Page 4: Mule esb db component

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 .

Page 5: Mule esb db component

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>

Page 6: Mule esb db component

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}]