Mule 2.2.1 – Http + Serialization

A common pattern that I have to face is, once processed the contents, share it between different application.

This pattern is typical something like:

  • Read contents from database;
  • Serialize contents;
  • Expose contents through http.

Let’s go and see how it’s possible do it with mule

Read contents from database

This is a very, common, easy task in mule.

I defined a DataSource and ResultSetHandler to insert the record from database to Jax Bean named Record.

<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 id="DerbyRecord"
 <spring:constructor-arg value="it.example.mule.Record" />
<jdbc:connector name="DerbyConnector"
 <jdbc:query key="GETLOG" value="SELECT DISTINCT
 '' as uniqueid, correlate_uniqueid,
 SUM(CASE WHEN TYPE = 'ERROR' THEN 1 ELSE 0 END) type, '' as payload,
  MIN(LOG.DATE) as date, MAX(LOG.DATE) as enddate


The Record class is generated by xjc tool inside Java Jdk.  From Xsd schema you can get an annotated Jaxb class.

@XmlType(name = "", propOrder = {
@XmlRootElement(name = "record")
public class Record {
    @XmlElement(required = true)
    protected String uniqueid;
    @XmlElement(name = "correlate_uniqueid", required = true)
    private String correlate_uniqueid;
    @XmlElement(required = true)
    @XmlSchemaType(name = "NCName")
    protected String endpoint;
    @XmlElement(required = true)
    @XmlSchemaType(name = "NCName")
    protected String payload;
    @XmlElement(required = true)
    @XmlSchemaType(name = "NCName")
    protected String type;
    @XmlElement(required = true)
    protected String date;

Every records resulted from getlog query will be inserted into this class as a List of Record class.

Serialize contents

Once all records have been collected, we have to serialize in Xml format. Even this task is very used in java language.

I did it inside Mule trasformation.

package it.example.mule.transform;
import it.example.mule.Logview;
import it.example.mule.Record;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractMessageAwareTransformer;
public class SerializeList extends AbstractMessageAwareTransformer{
 public Object transform(MuleMessage message, String outputEncoding)
   throws TransformerException {
  Logview logview = new Logview();
  JAXBContext cntx = null;
  try {
   cntx = JAXBContext.newInstance(it.example.mule.Logview.class);
  } catch (JAXBException e1) {
  Marshaller m = null;
  try {
   m = cntx.createMarshaller( );
  } catch (JAXBException e) {
        StringWriter w = new StringWriter();
        try {
   m.marshal(logview, w);
  } catch (JAXBException e) {
        return w.toString();

Only few aspect in this code should be examinated.

At line 16 you can see Logview class. This class is the parent of record class and it’s used to collecting all the records inside one data structure.

Expose contents through http

In this final task we are going to expose the transformed data via http, so you can access to it using a web browser.

The mule configuration is very easy to configure.

<custom-transformer name="SerializeList"/>
<service name="logview">
    <http:inbound-endpoint host="localhost" port="8009" path="logview" synchronous="true"
      name="logview.http" keep-alive="false"  responseTransformer-refs="SerializeList" />
     <jdbc:outbound-endpoint connector-ref="DerbyConnector" queryKey="GETLOG" synchronous="true" />

The browser calls localhost:8009/logview and mule response will be a Xml file like this.

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    <date>2012-05-23 15:08:06.993</date>
    <date>2012-05-23 15:08:07.634</date>
    <date>2012-05-23 15:08:07.634</date>


In this post I’ve wanted to show how is easy to create a little service and expose it using Mule. As you can see, I’m using this to logging a mule process in a very easy way (without any problems at the present day).


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.