Showing posts with label Class Mediator. Show all posts
Showing posts with label Class Mediator. Show all posts

Wednesday, May 25, 2016

[WSO2][ESB] Class mediator sample

Hi all,

Here I present sample class mediator which I used in JMS local transactions. You can download relevant dependency jars thorough following links:

Dependency jars:

  •  Synapse core - http://www.java2s.com/Code/JarDownload/synapse/synapse-core-2.1.0.jar.zip
  •  Apache axiom - http://www.java2s.com/Code/JarDownload/apache-axiom/apache-axiom-api-1.2.7.jar.zip 
 package org.wso2.carbon.mediator;  
 import org.apache.synapse.MessageContext;  
 import org.apache.synapse.mediators.AbstractMediator;  
 /**  
  * This class counting messages received and use as class mediator
  */  
 public class MessageCounterMediator extends AbstractMediator {  
     private static int MESSAGE_COUNT = 0;  
     public boolean mediate(MessageContext synCtx) {  
       MESSAGE_COUNT++;  
       synCtx.setProperty("MESSAGE_COUNT", MESSAGE_COUNT);  
       return true;  
     }  
 }  

For more information, refer https://docs.wso2.com/display/ESB470/Sample+380%3A+Writing+your+own+Custom+Mediation+in+Java

You can write own java classes and use them within WSO2 ESB. :) :)

Sunday, April 10, 2016

[WSO2][ESB][Class Mediator]How to create an Empty OMElement Array?

Hi all,

Let's find out very interesting topic. While using store mediator in ESB, I just wanted to create an empty OMElement array.

If I clarify inside mediation within ESB, when it receive a message, it may convert into OMElements which will is used to next mediation process. Therefore, I can't send empty OMElement array from outside as it is an inside process of ESB (Just like sending SOAP or REST messages).

So we have to convert the message into OMElements within ESB.In my case, I have to add empty OMElement array to the message.

ESB is a marvellous middleware which will provide magical operations. Here it comes "Class Mediator". The Class mediator will retrieve a class and it may act as custom mediator.

So Class Mediator helped me to do my task.

Let's begin...

1) Write a java class which will set OMElement array to the message.

When writing java class which is going to deploy as a "Class Mediator", it must extend from "Abstract Mediator"

In here I used message context (org.apache.synapse.MessageContext) to add empty array.

As a property set new array without any elements.

Sample Configuration

 package org.wso2.test;  
 import java.util.ArrayList;  
 import org.apache.synapse.MessageContext;  
 import org.apache.synapse.mediators.AbstractMediator;  
 public class OMElementEmptyArray  
 extends AbstractMediator {  
   public boolean mediate(MessageContext context) {  
     context.setProperty("EMPTY_ARRAY", new ArrayList());  
     System.out.println("Routed through Class Mediator");  
     return true;  
   }  
 }  

2) Java class packaged as .jar and should be inside $ESB_HOME/repository/components/lib

3) ESB should be up and running.

4) Login to ESB and create an API which will add class mediator.

Sample Configuration:

 <api context="/SerializeProperty" name="StoreMediatorSerialize">  
     <resource methods="GET" protocol="http" url-mapping="/serializeOMArray">  
       <inSequence>  
         <class name="org.wso2.test.OMElementEmptyArray"/>  
       </inSequence>  
       <outSequence/>  
       <faultSequence/>  
     </resource>  
   </api>  

5) Invoke API.

Sample Invocation through REST Client:

GET      http://10.100.7.120:8280/SerializeProperty

Terminal will print follow log:

Routed through Class Mediator


Happy Coding !!!