Create Custom Transfomer Using Mule Studio

Niladri.Biswas
Posted by in Mule category on for Beginner level | Points: 250 | Views : 11027 red flag

In this article we will look into creation of a custom transformer component using Mule Studio

Introduction

Though Mule Studio has a lot of built in transformers , however, it does provides opportunities to create our own. This article will talk about that.

What we need to have for doing the experiment?

We need

  1. Download Mule ESB
  2. Everything should be up and running properly

Let us start the experiment

We will basically take our Create Custom Component Using Mule Studio example as a case study for this experiment. If we look into the final output , we find that it is

Our custom transformer will remove the slash before the name i.e. we are looking for

Welcome Niladri.....

Since this exercise is an extension of the previous one , so basically the initial flow will be as under (refer the earlier article)

Let us create another class file from the "org.mule.customcomponentexamples" package.

Enter "MyCustomTransformer" in the Name field and "org.mule.transformer.AbstractTransformer" in the SuperClass field.Click "Finish" button

Write the below code in "MyCustomTransformer.Java"

package org.mule.customcomponentexamples;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractTransformer;
public class MyCustomTransformer extends AbstractTransformer {
 @Override
 protected Object doTransform(Object src, String enc)
   throws TransformerException {
  // TODO Auto-generated method stub

  if(src instanceof String)
  {
   String name = ((String) src);
   if(name.charAt(0) == '/')
    return name.substring(1);
  }
  return src;
 }
}

Now from the "Transformer" palette, choose "Java" transformer component

Drag and drop it on the "Message Flow" container.Change the name to "MyTransformer".Double click on the "MyTransformer" component and from the properties window that appears, enter "org.mule.customcomponentexamples.MyCustomTransformer" in the "Class Name".Click "OK" once done.

The final flow looks as under

Run the application

We are now almost done.Next we need to run the application.Right click on "CustomComponentExperiment.mflow" and from the context menu, choose Run As->Mule Application.

We will encounter the below screen at some point of time

Because it is looking for the input data.So open up the browser, and type "http://localhost:8082/Niladri".Press Enter.

The output in the browser is as under

References

Working with Transformers

Conclusion

Hope you have enjoyed the tutorial.More to come soon.

Page copy protected against web site content infringement by Copyscape

About the Author

Niladri.Biswas
Full Name: Niladri Biswas
Member Level: Platinum
Member Status: Member
Member Since: 10/25/2010 11:04:24 AM
Country: India
Best Regards, Niladri Biswas
http://www.dotnetfunda.com
Technical Lead at HCL Technologies

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)