001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.camel.processor.idempotent; 018 019 import org.apache.camel.Exchange; 020 import org.apache.camel.Expression; 021 import org.apache.camel.Processor; 022 import org.apache.camel.impl.ServiceSupport; 023 import org.apache.camel.util.ExpressionHelper; 024 import org.apache.camel.util.ServiceHelper; 025 import org.apache.commons.logging.Log; 026 import org.apache.commons.logging.LogFactory; 027 028 /** 029 * An implementation of the <a 030 * href="http://activemq.apache.org/camel/idempotent-consumer.html">Idempotent 031 * Consumer</a> pattern. 032 * 033 * @version $Revision: 303 $ 034 */ 035 public class IdempotentConsumer extends ServiceSupport implements Processor { 036 private static final transient Log LOG = LogFactory.getLog(IdempotentConsumer.class); 037 private Expression<Exchange> messageIdExpression; 038 private Processor nextProcessor; 039 private MessageIdRepository messageIdRepository; 040 041 public IdempotentConsumer(Expression<Exchange> messageIdExpression, 042 MessageIdRepository messageIdRepository, Processor nextProcessor) { 043 this.messageIdExpression = messageIdExpression; 044 this.messageIdRepository = messageIdRepository; 045 this.nextProcessor = nextProcessor; 046 } 047 048 @Override 049 public String toString() { 050 return "IdempotentConsumer[expression=" + messageIdExpression + ", repository=" + messageIdRepository 051 + ", processor=" + nextProcessor + "]"; 052 } 053 054 public void process(Exchange exchange) throws Exception { 055 String messageId = ExpressionHelper.evaluateAsString(messageIdExpression, exchange); 056 if (messageId == null) { 057 throw new NoMessageIdException(exchange, messageIdExpression); 058 } 059 if (!messageIdRepository.contains(messageId)) { 060 nextProcessor.process(exchange); 061 } else { 062 onDuplicateMessage(exchange, messageId); 063 } 064 } 065 066 // Properties 067 // ------------------------------------------------------------------------- 068 public Expression<Exchange> getMessageIdExpression() { 069 return messageIdExpression; 070 } 071 072 public MessageIdRepository getMessageIdRepository() { 073 return messageIdRepository; 074 } 075 076 public Processor getNextProcessor() { 077 return nextProcessor; 078 } 079 080 // Implementation methods 081 // ------------------------------------------------------------------------- 082 083 protected void doStart() throws Exception { 084 ServiceHelper.startServices(nextProcessor); 085 } 086 087 protected void doStop() throws Exception { 088 ServiceHelper.stopServices(nextProcessor); 089 } 090 091 /** 092 * A strategy method to allow derived classes to overload the behaviour of 093 * processing a duplicate message 094 * 095 * @param exchange the exchange 096 * @param messageId the message ID of this exchange 097 */ 098 protected void onDuplicateMessage(Exchange exchange, String messageId) { 099 if (LOG.isDebugEnabled()) { 100 LOG.debug("Ignoring duplicate message with id: " + messageId + " for exchange: " + exchange); 101 } 102 } 103 }