Initialize database in Mule part2

15
By Anirban Sen Chowdhary

Transcript of Initialize database in Mule part2

Page 1: Initialize database in Mule part2

By Anirban Sen Chowdhary

Page 2: Initialize database in Mule part2

Sometimes we require to initialize our database during the start of our application in server. We can also have the same requirement in our Mule application, where we require to initialise the database while our application starts.In my previous slide I have demonstrate initializing database with Groovy Script at the start of application.Here we will see doing the same with Spring beans and calling a Java class at the startup

Page 3: Initialize database in Mule part2
Page 5: Initialize database in Mule part2

We can easily initialize the database by using a Java class that will implement interface InitializingBean.

Let us consider we need to clear all the database value when our Mule application will start in server.

Page 6: Initialize database in Mule part2

Let us consider we have the following Mule flow, which will use the database component to insert a new value into database table.But we also require when this flow will start at server it will initialize the database by clearing (deleting) the old values from the table

Page 7: Initialize database in Mule part2

So, before we will start the flow in the server we will check the existing values in database table which will be cleared when the application will be deployed into server:-

As you can see the current database table has the above rows which we need to clear when our Mule application will be deployed

Page 8: Initialize database in Mule part2

The corresponding Mule flow will be as follows:-

Here is you can we are using Spring beans dbinitialization which is calling a Java class that is responsible for the Database initialization

Page 9: Initialize database in Mule part2

Now let’s see the Java class that will initialize the Database

So, here you can see the class is implementing InitializingBean interface which is implementing afterPropertiesSet() method to clear the old values from Database

Page 10: Initialize database in Mule part2

Now let’s start our application and we will see in the log at the beginning of the application is deployed in server:-

And here you can see that the database is initialized and cleared before the application started completely.

Page 11: Initialize database in Mule part2

You can see now the table is empty and no data is there in database

Page 12: Initialize database in Mule part2

And now if we hit http://localhost:8081/initial in our browser we will get the following

The Mule flow has inserted a new row in the database table

Page 13: Initialize database in Mule part2

And if we check the database now, we will find a new row is inserted there in the Database table

So, this is a simple example of initializing Database with Spring beans in Mule

Page 14: Initialize database in Mule part2

In my next slide I will bring some other techniques in Mule implementation .Hope you have enjoyed this simpler version.Keep sharing your knowledge and let our Mule community grow

Page 15: Initialize database in Mule part2