Logging mule flow with DerbyDb – Part 1

In one of my project I had needed to keep tracking the mule flow with deeper details than log4j logger.

I wasn’t happy to read a lot of rows at file log in multi-thread enviroment like Mule. So, I decided to use DerbyDb in embedded mode to log the flow of the message in more friendly way and access it by http connector.

I’m going to split this task in two posts. At this first part I’m describing how create Db Derby, integrate it in mule enviroment and, finally, write log message as record.

First, some reference.

Derby Db in embedded mode is described here at official web site. All that you need to do is adding derby.jar and derbytools.jar at your project.

Create Derby Instance

As I was saying I’m going to use Derby Db in embedded way. First set DERBY_INSTALL and export it into your classpath.

export DERBY_INSTALL=/store1/db-derby-10.8.1.2-bin
export CLASSPATH=$DERBY_INSTALL/lib/derby.jar:$DERBY_INSTALL/lib/derbytools.jar: $DERBY_INSTALL/lib/derbyclient.jar:

Launch the ij tool and create Derby Db.

java org.apache.derby.tools.ij
connect 'jdbc:derby:derbyDB;create=true';

I haven’t got lot fantasy, so I created a db called derbyDB …

Now, we create the Log table with its index

CREATE TABLE LOG (UNIQUEID VARCHAR(100),CORRELATE_UNIQUEID VARCHAR(100),ENDPOINT VARCHAR(200),TYPE VARCHAR(20),DATE TIMESTAMP,PAYLOAD CLOB);
CREATE INDEX CORRELATE_UNIQUEID_INDEX ON LOG(CORRELATE_UNIQUEID);
CREATE INDEX DATE ON LOG(DATE);

Use DerbyDb inside Mule 

Now you have to create a jdbc connector inside Mule flow. Before this I defined a Spring bean named “Derby Connection” that it’s used to connecting at the database from Mule and inside custom java controller.

<spring:bean id="DerbyConnection">
 <spring:property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver"/>
 <spring:property name="url" value="jdbc:derby:derbyDB;create=true"/>
 <spring:property name="username" value=""/>
 <spring:property name="password" value=""/>
</spring:bean>

I used a custom control to create a new log record into the Db. This is a normal Dao layer with a method that looks like something this

javax.sql.DataSource dataSource = (javax.sql.DataSource)RequestContext.getEventContext().getMuleContext().getRegistry().lookupObject("DerbyConnection");
   JdbcTemplate template = new JdbcTemplate(dataSource);
    
   String sql ="insert into Log values (?, ?, ?, ?, ?, ?)";
    
   template.update(sql, new Object[]
                                {  
       uniqueId,
       correlation_uniqueId,
       endpoint, 
       type,
       sqlToday,
       payload});

At the next post I’ll show you how automatically write the Mule Log message flow and how get the log from Browser http.

Advertisements

One thought on “Logging mule flow with DerbyDb – Part 1

  1. Pingback: Logging mule flow with DerbyDb – Part 2 « Tech Annotation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s