torsdag 23 januari 2014

Quick tip: Howto log business events to console inside of Mule Studio

As you might have noticed there is no default way of logging Business events to the console log during development of Mule flows in Mule Studio without having to deploy and monitor them through the Mule management console.

This issue is described in the following JIRA:

I tried many things and discussed the matter with my colleague Pontus Ullgren who came up with a solution I could use.

The tip of the day is hence howto implement your own Listener which can handle the matter.

First you need to setup the flow with your custom business event. Something like this:

      <flow doc:name="Business_event_logging" name="Business_event_logging">  
           <http:inbound-endpoint host="localhost" port="8090" path="test" doc:name="HTTP" exchange-pattern="request-response"/>  
           <message-filter doc:name="Filter favicon">  
                <not-filter>  
                     <wildcard-filter pattern="/favicon.ico" caseSensitive="true"/>  
                </not-filter>  
           </message-filter>  
           <tracking:custom-event event-name="MyUserBusinessEvent" doc:name="Custom Business event">  
                <tracking:meta-data key="User_Firstname" value="Jon" />  
                <tracking:meta-data key="User_Lastname" value="Åkerström" />  
                <tracking:meta-data key="User_Action" value="#[message.payload]"/>    
           </tracking:custom-event>  
      </flow>       

Then you need to in your configuration xml also configure a notification listener referencing to your own Listener component.

   <spring:beans>   
     <spring:bean class="se.redpill.mulecomponents.BusinessEventHandlerListener" id="notificationListener">   
   </spring:bean></spring:beans>   
   <notifications>   
     <notification-listener ref="notificationListener">   
   </notification-listener></notifications>   

Then to be able to log the events when they happen, simply implement your own Listener by implementing the EventNotificationListener interface and take appropriate actions i.e log when the onNotification is called.

 package se.redpill.mulecomponents;  
 import org.apache.commons.logging.Log;  
 import org.apache.commons.logging.LogFactory;  
 import org.mule.api.MuleContext;  
 import org.mule.api.context.MuleContextAware;  
 import com.mulesoft.mule.tracking.event.EventNotification;  
 import com.mulesoft.mule.tracking.event.EventNotificationListener;
  
 public class BusinessEventHandlerListener implements EventNotificationListener<EventNotification>, MuleContextAware {  
   MuleContext context;  
   protected static final Log LOGGER = LogFactory.getLog("MEEE");  
      @Override  
      public void onNotification(EventNotification notification) {  
           LOGGER.debug("Event notification: " + notification.toString());  
           LOGGER.debug("Event DATA:" + notification.getMetaDatas().toString());  
      }  
      @Override  
      public void setMuleContext(MuleContext context) {  
           this.context = context;  
      }  
 }  


Finally make sure that you in your log4j.properties file specify log level DEBUG globally or for the appropriate class to actually see the Business event and it's data.

The code is now also availble on Pontus Ullgrens GIT repo:
https://github.com/ullgren/my-mule-examples/tree/master/trackbuissnessevents

2 kommentarer:

  1. Hi,

    Do you have an example or doc for Mule Customer Listener to Queue (IBM MQ).

    I need write my own listener/Inbound Endpoint to listen to IBM MQ.

    Problem in using JMS/WMQ connectors is, I am unable to read all MQMD/MQRFH2 header values (like applicationIdData). They are not included in Mule Message as Inbound Properties.

    Any help is highly appreciated.

    SvaraRadera
  2. Great Article. its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

    Mulesoft online training

    SvaraRadera