Datasource connection in Mule Domain project

          Today, I want to share the most important concept in the mule domain project. In my previous post I already explained step by step procedure of how to create a mule domain project, and how to execute mule domain project, etc.. In that post how to get a datasource connection is also explained.
          First, I will explain you what is datasource. In Anypoint Studio mostly Spring Datasource connection will be used. A factory for connections to the physical data source this Datasource object represents. An alternative to the DrivarManager facility, a Datasource object is the preferred of getting a connection.
There are few steps as follows

Step 1:

       Create Mule Domain Project

       For reference purpose, click the below link and create domain project. 
 
                                                     Create Mule Domain Project

Step 2:

          After successfully creating the domain project, open mule-domain-config.xml file. In that XML file you need to add database credential and also import xmlns.
CodingBeginners_MuleDomainConfigFile
You can see screen shot below. In this I have mention Datasource connection which is highlighted using square and arrows.
CodingBeginners_DataSourceConnectionMapping
Open mule-domain-config.

 

Step 3:

          Open domain client project (Already created in step 1). See folder structure below screen.

Configure HTTP connector with main domain project. Drag global java connector in existing flow. Using this global connector you can define your own java code. You need to configure this connector using java class otherwise it’s indicate error.

Create new class.

File->
      New->
              Class->

Add  class name and package name.

Open this class and add below java code.

Simple_Datasource_Connection.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.clientdomain1_datasource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;

import org.enhydra.jdbc.standard.StandardDataSource;
import org.mule.api.MuleContext;
import org.mule.api.context.MuleContextAware;
import org.mule.api.annotations.expressions.Lookup;
import org.springframework.stereotype.Component;


@Component
public class Simple_Datasource_Connection implements MuleContextAware {

 @Lookup
 private MuleContext muleContext;
 private StandardDataSource dataSource;

 public StandardDataSource getconn() {
  return dataSource;
 }

 public void setConn(StandardDataSource conn) {
  this.dataSource = conn;
 }

 @Override
 public void setMuleContext(MuleContext context) {
  // TODO Auto-generated method stub
  this.muleContext = context;

 }

 public String retriveClientData() throws SQLException
  //public HashMap getClient1Data() throws SQLException
  {
   if (this.dataSource == null) {
    this.dataSource = this.muleContext.getRegistry().lookupObject("jdbcdatasource");
   }

   Connection conn = dataSource.getConnection();
   PreparedStatement ps = null;
   String sql;
   try {
    /* Write sql query as per your database .Replace xxx to table name and column.
     */

    sql = "select  * from Table Name where status='test'";
    //above query write as per your database information
    ps = conn.prepareStatement(sql);
    ResultSet rs = ps.executeQuery();

    List rowValues = new ArrayList();
    while (rs.next()) {

     int i = 1;
     while (i & lt; = 20) {
      rowValues.add(rs.getString(i++));

      System.out.println("======" + rs.getString(i));
     }

     System.out.println("rowValues...." + rowValues);

     System.out.println("Length---" + rs.toString().length());
     String a1 = rs.getString(1);
     String a2 = rs.getString(2);
     String a3 = rs.getString(3);
     String a4 = rs.getString(4);
     String a5 = rs.getString(5);


     System.out.println("Display Data--" + a1 + "-" + a2 + "-" + a3 + "-" + a4 + "-" + a5 + "-");
     ps.execute();
    }

   } catch (Exception e) {
    e.printStackTrace();
   } finally {
    //conn.dataSource.closeDBConnection();
   }
   return null;

  }

}


Configure this java class with Global connector. See below screen shot.

Step 4:

        Run domain project in anypoint studio; go to anypoint studio menu bar.

File->

      Run As->
 
              Mule Application (Configure)->
 
New panel window open edit domain project configuration. Suppose you would like to run both client1 and client2  at the same time then click on the tick, press Apply button. Finally press Run button.

See the anypoint studio console window, both project are deployed successfully.

Step 5:

        Open any browser window or postman client. Enter main domain HTTP connection like as host, port and main path. Then again enter client base path on the browser header and press enter button on the keyboard.

Step 6:

          See the console window on anypoint studio tool; you can see the output of mule domain project.

Using above steps you can easily create Datasource Domain Project in Anpoint Studiotool.

Sample of Data connection in mule domain project example are also provided. Just Click on below link…

                                                   Download, Datasource connection in Mule Domain
        If you like this blog, then please subscribe and suppose you have any queries or suggestions then comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *