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>
<spring:bean id="DerbyRecord"
 class="org.apache.commons.dbutils.handlers.BeanListHandler">
 <spring:constructor-arg value="it.example.mule.Record" />
</spring:bean>
<jdbc:connector name="DerbyConnector"
  dataSource-ref="DerbyConnection"
  resultSetHandler-ref="DerbyRecord">
 
 <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
 FROM LOG
 GROUP BY CORRELATE_UNIQUEID
 ORDER BY DATE" /> 

</jdbc:connector>

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

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "uniqueid",
    "correlate_uniqueid",
    "endpoint",
    "payload",
    "type",
    "date",
    "enddate"
})
@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)
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    @XmlSchemaType(name = "NCName")
    protected String endpoint;
    @XmlElement(required = true)
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    @XmlSchemaType(name = "NCName")
    protected String payload;
    @XmlElement(required = true)
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    @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.io.StringWriter;
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{
 @Override
 public Object transform(MuleMessage message, String outputEncoding)
   throws TransformerException {
  Logview logview = new Logview();
  logview.setRecord((List<Record>)message.getPayload());
  
  JAXBContext cntx = null;
  try {
   cntx = JAXBContext.newInstance(it.example.mule.Logview.class);
  } catch (JAXBException e1) {
   
   e1.printStackTrace();
  }
  
  Marshaller m = null;
  try {
   m = cntx.createMarshaller( );
  } catch (JAXBException e) {
   
   e.printStackTrace();
  }
        StringWriter w = new StringWriter();
        try {
   m.marshal(logview, w);
  } catch (JAXBException e) {
   
   e.printStackTrace();
  }
        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">
  <inbound>
    <http:inbound-endpoint host="localhost" port="8009" path="logview" synchronous="true"
      name="logview.http" keep-alive="false"  responseTransformer-refs="SerializeList" />
  </inbound>
  <outbound>
    <chaining-router>
     <jdbc:outbound-endpoint connector-ref="DerbyConnector" queryKey="GETLOG" synchronous="true" />
    </chaining-router>
  </outbound>
</service>

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" ?>
<logview>
  <record>
    <uniqueid>HTMLCleaner.StartByQuartz.2</uniqueid>
    <correlate_uniqueid>5687d849-a4d8-11e1-8d65-1dcd82d1503d</correlate_uniqueid>
    <endpoint>quartz://HTMLCleaner.start</endpoint>
    <payload>{NullPayload}</payload>
    <type>INBOUND</type>
    <date>2012-05-23 15:08:06.993</date>
  </record>
  <record>
    <uniqueid>HTMLCleaner.StartByQuartz.2</uniqueid>
    <correlate_uniqueid>5687d849-a4d8-11e1-8d65-1dcd82d1503d</correlate_uniqueid>
    <endpoint>vm://HTMLCleaner</endpoint>
    <payload>{NullPayload}</payload>
    <type>OUTBOUND</type>
    <date>2012-05-23 15:08:07.634</date>
  </record>
  <record>
    <uniqueid>HTMLCleaner.StartByQuartz.2</uniqueid>
    <correlate_uniqueid>5687d849-a4d8-11e1-8d65-1dcd82d1503d</correlate_uniqueid>
    <endpoint>file://C:/progetti/Orchestrator/</endpoint>
    <payload>{NullPayload}</payload>
    <type>OUTBOUND</type>
    <date>2012-05-23 15:08:07.634</date>
  </record>
</logview>

Summary

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).

Advertisements

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