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.spi.IdempotentRepository;
021 import org.apache.camel.spi.Synchronization;
022 import org.apache.camel.util.ExchangeHelper;
023 import org.slf4j.Logger;
024 import org.slf4j.LoggerFactory;
025
026 /**
027 * On completion strategy for {@link org.apache.camel.processor.idempotent.IdempotentConsumer}.
028 * <p/>
029 * This strategy adds the message id to the idempotent repository in cast the exchange
030 * was processed successfully. In case of failure the message id is <b>not</b> added.
031 */
032 public class IdempotentOnCompletion implements Synchronization {
033 private static final transient Logger LOG = LoggerFactory.getLogger(IdempotentOnCompletion.class);
034 private final IdempotentRepository<String> idempotentRepository;
035 private final String messageId;
036 private final boolean eager;
037 private final boolean removeOnFailure;
038
039 public IdempotentOnCompletion(IdempotentRepository<String> idempotentRepository, String messageId, boolean eager, boolean removeOnFailure) {
040 this.idempotentRepository = idempotentRepository;
041 this.messageId = messageId;
042 this.eager = eager;
043 this.removeOnFailure = removeOnFailure;
044 }
045
046 public void onComplete(Exchange exchange) {
047 if (ExchangeHelper.isFailureHandled(exchange)) {
048 // the exchange did not process successfully but was failure handled by the dead letter channel
049 // and thus moved to the dead letter queue. We should thus not consider it as complete.
050 onFailedMessage(exchange, messageId);
051 } else {
052 onCompletedMessage(exchange, messageId);
053 }
054 }
055
056 public void onFailure(Exchange exchange) {
057 onFailedMessage(exchange, messageId);
058 }
059
060 /**
061 * A strategy method to allow derived classes to overload the behavior of
062 * processing a completed message
063 *
064 * @param exchange the exchange
065 * @param messageId the message ID of this exchange
066 */
067 protected void onCompletedMessage(Exchange exchange, String messageId) {
068 if (!eager) {
069 // if not eager we should add the key when its complete
070 idempotentRepository.add(messageId);
071 }
072 idempotentRepository.confirm(messageId);
073 }
074
075 /**
076 * A strategy method to allow derived classes to overload the behavior of
077 * processing a failed message
078 *
079 * @param exchange the exchange
080 * @param messageId the message ID of this exchange
081 */
082 protected void onFailedMessage(Exchange exchange, String messageId) {
083 if (removeOnFailure) {
084 idempotentRepository.remove(messageId);
085 LOG.debug("Removed from repository as exchange failed: {} with id: {}", exchange, messageId);
086 }
087 }
088
089 @Override
090 public String toString() {
091 return "IdempotentOnCompletion[" + messageId + ']';
092 }
093 }