Coverage Report - org.apache.camel.processor.idempotent.IdempotentConsumer
 
Classes in this File Line Coverage Branch Coverage Complexity
IdempotentConsumer
92% 
100% 
0
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.processor.idempotent;
 18  
 
 19  
 import org.apache.camel.Exchange;
 20  
 import org.apache.camel.Expression;
 21  
 import org.apache.camel.Processor;
 22  
 import org.apache.camel.impl.ServiceSupport;
 23  
 import org.apache.camel.util.ExpressionHelper;
 24  
 import org.apache.camel.util.ServiceHelper;
 25  
 import org.apache.commons.logging.Log;
 26  
 import org.apache.commons.logging.LogFactory;
 27  
 
 28  
 /**
 29  
  * An implementation of the <a
 30  
  * href="http://activemq.apache.org/camel/idempotent-consumer.html">Idempotent
 31  
  * Consumer</a> pattern.
 32  
  * 
 33  
  * @version $Revision: 1.1 $
 34  
  */
 35  
 public class IdempotentConsumer extends ServiceSupport implements Processor {
 36  3
     private static final transient Log LOG = LogFactory.getLog(IdempotentConsumer.class);
 37  
     private Expression<Exchange> messageIdExpression;
 38  
     private Processor nextProcessor;
 39  
     private MessageIdRepository messageIdRepository;
 40  
 
 41  
     public IdempotentConsumer(Expression<Exchange> messageIdExpression,
 42  6
                               MessageIdRepository messageIdRepository, Processor nextProcessor) {
 43  6
         this.messageIdExpression = messageIdExpression;
 44  6
         this.messageIdRepository = messageIdRepository;
 45  6
         this.nextProcessor = nextProcessor;
 46  6
     }
 47  
 
 48  
     @Override
 49  
     public String toString() {
 50  12
         return "IdempotentConsumer[expression=" + messageIdExpression + ", repository=" + messageIdRepository
 51  
                + ", processor=" + nextProcessor + "]";
 52  
     }
 53  
 
 54  
     public void process(Exchange exchange) throws Exception {
 55  18
         String messageId = ExpressionHelper.evaluateAsString(messageIdExpression, exchange);
 56  18
         if (messageId == null) {
 57  0
             throw new NoMessageIdException(exchange, messageIdExpression);
 58  
         }
 59  18
         if (!messageIdRepository.contains(messageId)) {
 60  9
             nextProcessor.process(exchange);
 61  9
         } else {
 62  9
             onDuplicateMessage(exchange, messageId);
 63  
         }
 64  18
     }
 65  
 
 66  
     // Properties
 67  
     // -------------------------------------------------------------------------
 68  
     public Expression<Exchange> getMessageIdExpression() {
 69  3
         return messageIdExpression;
 70  
     }
 71  
 
 72  
     public MessageIdRepository getMessageIdRepository() {
 73  3
         return messageIdRepository;
 74  
     }
 75  
 
 76  
     public Processor getNextProcessor() {
 77  3
         return nextProcessor;
 78  
     }
 79  
 
 80  
     // Implementation methods
 81  
     // -------------------------------------------------------------------------
 82  
 
 83  
     protected void doStart() throws Exception {
 84  3
         ServiceHelper.startServices(nextProcessor);
 85  3
     }
 86  
 
 87  
     protected void doStop() throws Exception {
 88  3
         ServiceHelper.stopServices(nextProcessor);
 89  3
     }
 90  
 
 91  
     /**
 92  
      * A strategy method to allow derived classes to overload the behaviour of
 93  
      * processing a duplicate message
 94  
      * 
 95  
      * @param exchange the exchange
 96  
      * @param messageId the message ID of this exchange
 97  
      */
 98  
     protected void onDuplicateMessage(Exchange exchange, String messageId) {
 99  9
         if (LOG.isDebugEnabled()) {
 100  0
             LOG.debug("Ignoring duplicate message with id: " + messageId + " for exchange: " + exchange);
 101  
         }
 102  9
     }
 103  
 }