Initialize database in Mule part2
-
Upload
anirban-sen-chowdhary -
Category
Technology
-
view
153 -
download
0
Transcript of Initialize database in Mule part2
By Anirban Sen Chowdhary
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
Yes .. We can .. I will show you this trick .. How ??
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.
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
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
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
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
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.
You can see now the table is empty and no data is there in database
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
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
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