Usage of Fault mediator in WSO2 ESB

This post explains, an usage of Fault mediator in wso2 ESB.

The sample scenario, refuses and sends an exception back to the client based on the Content-Type header property.

This example is tested in ESB 4.7.0

First validate the content-type - using filter mediator

If true, send back an exception to the client - using fault mediator

If false, continue the flow

Here is the sample proxy,

Continue reading

How to send an Email using WSO2 ESB

This post explains how to send an email in WSO2 ESB using Payload Factory mediator . You can find plenty of examples which do the same by using Script mediator.

This example is tested in WSO2 ESB 4.8

1.Uncomment and configure the SMTP server information ESB_HOME/repository/conf/axis/axis2.xml file as below

<transportSender name="mailto" class="org.apache.axis2.transport.mail.MailTransportSender">
   <parameter name=""></parameter>
   <parameter name="mail.smtp.port">587</parameter>
   <parameter name="mail.smtp.starttls.enable">true</parameter>
   <parameter name="mail.smtp.auth">true</parameter>
   <parameter name="mail.smtp.user">esbadmin</parameter>
   <parameter name="mail.smtp.password">*****</parameter>
   <parameter name="mail.smtp.from"></parameter>

Continue reading

WSO2 DSS Scheduled Task Example

This post explains how the scheduled task works in WSO2 DSS

This example is tested in WSO2 DSS 3.0.1

1. Write the following class which extends the org.wso2.carbon.dataservices.task.DataTask interface.

package com.example.DssSchedlueTask;

import java.util.HashMap;
import java.util.Map;
import org.wso2.carbon.dataservices.core.DataServiceFault;
import org.wso2.carbon.dataservices.core.engine.ParamValue;
import org.wso2.carbon.dataservices.task.DataTask;
import org.wso2.carbon.dataservices.task.DataTaskContext;

public class ScheduledDataTask_abcCompany implements DataTask {

	public void execute(DataTaskContext ctx) {
		System.out.println("Data Task executing...");
		String soldItems = getSoldItems();
		Map<String, ParamValue> params = new HashMap<String, ParamValue>();
		params.put("soldItems", new ParamValue(soldItems));
		params.put("productCode", new ParamValue("0001a3"));
		try {
			ctx.invokeOperation("abcCompany", "UpdateSales", params);
			System.out.println("Successfully updated the database. \tSoldItems = "+soldItems);
		} catch (DataServiceFault e) {

	private String getSoldItems(){
		int soldItems = (int )(Math.random() * 10000 + 1);
		return Integer.toString(soldItems);

Assume that you are getting sold items from another service, here you will get a random number for the sold items, and you are going to update your system with that value.

2.Then create the jar of the above class and copy it to the DSS_HOME/repository/components/lib folder and start the WSO2 DSS server.

3. Create a Keyspace name ‘abcCompany’ in cassandra db.  Then create a columnfamily name ‘salesDetails’ in abcCompany keyspace.

cqlsh:abccompany> CREATE COLUMNFAMILY salesDetails ( productCode varchar PRIMARY KEY , productName varchar , soldItems int, instock int) ;

4. Insert following dummy values to the above created column family.

cqlsh:abccompany> INSERT INTO abcCompany.salesDetails (productCode, productName, soldItems, instock) VALUES ( '0001a3', 'phone a3', 9999, 10000);
cqlsh:abccompany> INSERT INTO abcCompany.salesDetails (productCode, productName, soldItems, instock) VALUES ( '0001b2', 'phone b2', 99, 100);
cqlsh:abccompany> INSERT INTO abcCompany.salesDetails (productCode, productName, soldItems, instock) VALUES ( '0002x5', 'phone x5', 9, 10);

Continue reading

WSO2 ESB Rule Mediator Example

This post is to demonstrate how to write Rule mediator in WSO2 ESB.
This example is tested in WSO2 ESB 4.7.0
The example, get the list of students with marks for their subjects and, inside the proxy it check whether the each student has passed the each subject and print the result. To define type of the input data POJO adapters are used in this example.

1.Write the following POJO classes.

Student class

package com.example.rulemediator;

public class Student {

	private String indexNumber;
	private String name;
	private Subject[] subjects;

	public String getIndexNumber() {
		return indexNumber;
	public void setIndexNumber(String indexNumber) {
		this.indexNumber = indexNumber;
	public String getName() {
		return name;
	public void setName(String name) { = name;
	public Subject[] getSubjects() {
		return subjects;
	public void setSubjects(Subject[] subjects) {
		this.subjects = subjects;

Subject class

package com.example.rulemediator;

public class Subject {

	private String subjectCode;
	private String subjectName;
	private int score;

	public String getSubjectCode() {
		return subjectCode;
	public void setSubjectCode(String subjectCode) {
		this.subjectCode = subjectCode;
	public String getSubjectName() {
		return subjectName;
	public void setSubjectName(String subjectName) {
		this.subjectName = subjectName;
	public int getScore() {
		return score;
	public void setScore(int score) {
		this.score = score;

2. Then create a Jar file containing above classes and copy it to ESB_HOME/repository/components/lib folder and start the ESB.

Continue reading

WSO2 ESB Scheduled Task Example

This post is to demonstrate how to write a Scheduled task and using it inside the WSO2 ESB.

This example is test in WSO2 ESB 4.7.0

1. Write a Class and extend the Task interface. And override the execute method with the task you need to do.

import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.synapse.startup.Task;

public class ESBTask implements Task{
   private String parameter;

   public void execute() {
      SimpleDateFormat sdf = new SimpleDateFormat("");
      Date date = new Date();
      String time = sdf.format(date);
      System.out.println(time+" - Scheduled Task Running....");
      System.out.println("parameter: "+parameter);

   public void setParameter(String parameter) {
      this.parameter = parameter;


The setter method for the parameter variable let us to set a value to it, in the ESB Console.

2. Build a jar and copy it to the WSO2ESB_HOME/repository/components/lib folder and restart (if already started) the WSO2ESB server.

Continue reading

WSO2 ESB Spring Mediator Example

This post is to demonstrate how to initialize and use a SpringBean as a mediator.

We are going to print the HELLO ‘Argument’ in this example. And this ‘argument’ is passed using the spring configuration file. This example is tested in WSO2ESB 4.6.0

1. Build the jar file of the following class and place it in ESB_HOME/repository/components/lib folder.

package com.test.spring.mediators.springmediator;

import org.apache.synapse.MessageContext;
import org.apache.synapse.mediators.AbstractMediator;

public class HelloWorld extends AbstractMediator{
   private String message;

   public void setMessage(String message){
      this.message = message;

   public boolean mediate(MessageContext arg0) {
      System.out.println("HELLO "+message);
      return true;

2. Start the WSO2ESB and add the following spring configuration file (springConfig.xml) under /_system/config/repository/springConfig.xml in the ESB Registry.

spring configuration file (springConfig.xml)

<?xml version="1.0" encoding="UTF-8"?>
   <bean id="springtest" class="com.test.spring.mediator.springmediator.HelloWorld" singleton="false">
      <property name="message"><value>ISURU</value></property>

Continue reading