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