title: How to implement a custom SamplingService for a custom message processor?log after that an element is retrieved and before the sequence is performed
body: <p>I am pretty new in <strong>WSO2 ESB</strong> and I have the implement a custom message processor with this specific behavior: <strong>perform an operation after that an element is retrieved from the message store and before the sequence related to this message processor is performed</strong>.</p>
  
  <p>I try to explain it in details.</p>
  
  <p>This is my ESB message processor definition:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!---&lt;messageProcessor class="org.apache.synapse.message.processor.impl.sampler.SamplingProcessor" messageStore="transferFromMessageStore" name="transferFromMessageProcessor" xmlns="http://ws.apache.org/ns/synapse"&gt;--&gt;
  &lt;messageProcessor class="com.mycompany.toolkit.messageprocessor.SamplingProcessorHeaderRateLimitation" messageStore="transferFromMessageStore" name="transferFromMessageProcessor" xmlns="http://ws.apache.org/ns/synapse"&gt;
      &lt;parameter name="sequence"&gt;transferProcessorSequence&lt;/parameter&gt;
      &lt;parameter name="interval"&gt;1000&lt;/parameter&gt;
      &lt;parameter name="is.active"&gt;true&lt;/parameter&gt;
      &lt;parameter name="concurrency"&gt;1&lt;/parameter&gt;
  &lt;/messageProcessor&gt;
  </code></pre>
  
  <p>It retrieve some elements (XML documents) form the <strong>transferFromMessageStore</strong> (a queue) and pass this object to the <strong>transferProcessorSequence.xml</strong> sequence that use it. As you can see at this time I have implemented a custom message processor <strong>SamplingProcessorHeaderRateLimit</strong> that simply extends the <strong>org.apache.synapse.message.processor.impl.sampler.SamplingProcessor</strong> WSO2 class. At this time it only show a log when the <strong>init()</strong> method is performed. I deployed it on my Carbon server and it works.</p>
  
  <p><a href="https://github.com/AndreaNobili/SamplingProcessorHeaderRateLimitation" rel="noreferrer">Here</a> you can find the entire project code.</p>
  
  <p>Ok but from what I have understood to obtain the desired behavior I have not to simply extend the <strong>SamplingProcessor</strong> class because in order to do custom implementation between every message consumption and dispatch to the sequence, need to extend the class <strong>SamplingService</strong> class, this <a href="https://github.com/wso2/wso2-synapse/blob/master/modules/core/src/main/java/org/apache/synapse/message/processor/impl/sampler/SamplingService.java" rel="noreferrer">one</a>.</p>
  
  <p>I think that I need to override <strong>execute()</strong> or <strong>fetch(MessageConsumer msgConsumer)</strong>.</p>
  
  <p>At this time should be ok also insert a log, something that write into the log file each time that an element is retrieved from the message store and before that the sequence related to the message processor is performed.</p>
  
  <p>Is it possible?</p>
  
  <p>So my main main doubs are:</p>
  
  <p>1) Have I to create a class extending the <strong>SamplingService</strong> class into the same project in which I am implementing my custom message processor (<strong>this behavior have to be used only for this specific message processor in my WSO2 ESB project, all the other message processor used in this project have to use the standard SamplingService implementation</strong>).</p>
  
  <p>2) Another doubt is related about how this custom <strong>SamplingService</strong> implementation is passed to my custom message processor. Into the the SamplingProcessor WSO2 class (<strong>how to associate a specific custom message processor implementation with a custom SamplingService implementation handling its lifecycle</strong>).</p>