Coverage Report - org.mule.routing.IdempotentMessageFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
IdempotentMessageFilter
0%
0/47
0%
0/14
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.routing;
 8  
 
 9  
 import org.mule.api.MessagingException;
 10  
 import org.mule.api.MuleEvent;
 11  
 import org.mule.api.MuleException;
 12  
 import org.mule.api.construct.FlowConstruct;
 13  
 import org.mule.api.construct.FlowConstructAware;
 14  
 import org.mule.api.expression.ExpressionManager;
 15  
 import org.mule.api.lifecycle.Initialisable;
 16  
 import org.mule.api.lifecycle.InitialisationException;
 17  
 import org.mule.api.routing.RoutingException;
 18  
 import org.mule.api.store.ObjectStore;
 19  
 import org.mule.config.i18n.CoreMessages;
 20  
 import org.mule.processor.AbstractFilteringMessageProcessor;
 21  
 import org.mule.util.store.InMemoryObjectStore;
 22  
 
 23  
 import java.text.MessageFormat;
 24  
 
 25  
 /**
 26  
  * <code>IdempotentMessageFilter</code> ensures that only unique messages are passed on. It does this by
 27  
  * checking the unique ID of the incoming message. Note that the underlying endpoint must support unique
 28  
  * message IDs for this to work, otherwise a <code>UniqueIdNotSupportedException</code> is thrown.<br>
 29  
  * <p>
 30  
  * <b>EIP Reference:</b> <a href="http://www.eaipatterns.com/IdempotentReceiver.html">http://www.eaipatterns.com/IdempotentReceiver.html</a>
 31  
  */
 32  
 public class IdempotentMessageFilter extends AbstractFilteringMessageProcessor implements FlowConstructAware, Initialisable
 33  
 {
 34  
     protected volatile ObjectStore<String> store;
 35  
     protected volatile String assignedComponentName;
 36  
     protected FlowConstruct flowConstruct;
 37  
 
 38  0
     protected String idExpression = MessageFormat.format("{0}message:id{1}",
 39  
         ExpressionManager.DEFAULT_EXPRESSION_PREFIX, ExpressionManager.DEFAULT_EXPRESSION_POSTFIX);
 40  
 
 41  0
     protected String valueExpression = MessageFormat.format("{0}message:id{1}",
 42  
         ExpressionManager.DEFAULT_EXPRESSION_PREFIX, ExpressionManager.DEFAULT_EXPRESSION_POSTFIX);
 43  
 
 44  
     public IdempotentMessageFilter()
 45  
     {
 46  0
         super();
 47  0
     }
 48  
 
 49  
     public void initialise() throws InitialisationException
 50  
     {
 51  0
         if (store == null)
 52  
         {
 53  0
             this.store = this.createMessageIdStore();
 54  
         }
 55  0
     }
 56  
 
 57  
     protected ObjectStore<String> createMessageIdStore() throws InitialisationException
 58  
     {
 59  0
         InMemoryObjectStore<String> s = new InMemoryObjectStore<String>();
 60  0
         s.setName(assignedComponentName);
 61  0
         s.setMaxEntries(-1);
 62  0
         s.setEntryTTL(60 * 5 * 1000);
 63  0
         s.setExpirationInterval(6000);
 64  0
         s.initialise();
 65  0
         return s;
 66  
     }
 67  
 
 68  
     @Override
 69  
     protected MuleEvent processNext(MuleEvent event) throws MuleException
 70  
     {
 71  0
         String id = this.getIdForEvent(event);
 72  0
         String value = this.getValueForEvent(event);
 73  
         try
 74  
         {
 75  0
             store.store(id, value);
 76  0
             return super.processNext(event);
 77  
         }
 78  0
         catch (Exception e)
 79  
         {
 80  0
             throw new RoutingException(CoreMessages.failedToWriteMessageToStore(id, assignedComponentName),
 81  
                 event, this, e);
 82  
         }
 83  
     }
 84  
 
 85  
     protected String getValueForEvent(MuleEvent event) throws MessagingException
 86  
     {
 87  0
         return event.getMuleContext().getExpressionManager().parse(valueExpression, event.getMessage(), true);
 88  
     }
 89  
 
 90  
     protected String getIdForEvent(MuleEvent event) throws MessagingException
 91  
     {
 92  0
         return event.getMuleContext().getExpressionManager().parse(idExpression, event.getMessage(), true);
 93  
     }
 94  
 
 95  
     public String getIdExpression()
 96  
     {
 97  0
         return idExpression;
 98  
     }
 99  
 
 100  
     public void setIdExpression(String idExpression)
 101  
     {
 102  0
         this.idExpression = idExpression;
 103  0
     }
 104  
 
 105  
     public ObjectStore<String> getStore()
 106  
     {
 107  0
         return store;
 108  
     }
 109  
 
 110  
     public void setStore(ObjectStore<String> store)
 111  
     {
 112  0
         this.store = store;
 113  0
     }
 114  
 
 115  
     @Override
 116  
     protected boolean accept(MuleEvent event)
 117  
     {
 118  0
         return event != null && acceptMessageForFlowConstruct(event) && isNewMessage(event);
 119  
     }
 120  
 
 121  
     protected boolean acceptMessageForFlowConstruct(MuleEvent event)
 122  
     {
 123  0
         if (flowConstruct.getName().equals(event.getFlowConstruct().getName()))
 124  
         {
 125  0
             return true;
 126  
         }
 127  
         else
 128  
         {
 129  0
             logger.error("This IdempotentMessageFilter was configured on the service: "
 130  
                          + assignedComponentName + " but has received an event for service: "
 131  
                          + flowConstruct.getName() + ". Please check your config to make sure each service"
 132  
                          + "has its own instance of IdempotentMessageFilter.");
 133  0
             return false;
 134  
         }
 135  
     }
 136  
 
 137  
     protected boolean isNewMessage(MuleEvent event)
 138  
     {
 139  
         try
 140  
         {
 141  0
             String id = this.getIdForEvent(event);
 142  0
             if (store == null)
 143  
             {
 144  0
                 synchronized (this)
 145  
                 {
 146  0
                     initialise();
 147  0
                 }
 148  
             }
 149  0
             return !store.contains(id);
 150  
         }
 151  0
         catch (MuleException e)
 152  
         {
 153  0
             logger.error("Exception attempting to determine idempotency of incoming message for "
 154  
                          + event.getFlowConstruct().getName() + " from the endpoint "
 155  
                          + event.getEndpoint().getEndpointURI().getUri(), e);
 156  0
             return false;
 157  
         }
 158  
     }
 159  
 
 160  
     public void setFlowConstruct(FlowConstruct flowConstruct)
 161  
     {
 162  0
         this.flowConstruct = flowConstruct;
 163  0
     }
 164  
 
 165  
     public String getValueExpression()
 166  
     {
 167  0
         return valueExpression;
 168  
     }
 169  
 
 170  
     public void setValueExpression(String valueExpression)
 171  
     {
 172  0
         this.valueExpression = valueExpression;
 173  0
     }
 174  
 
 175  
 }