Coverage Report - org.apache.camel.processor.DeadLetterChannel
 
Classes in this File Line Coverage Branch Coverage Complexity
DeadLetterChannel
75% 
83% 
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;
 19  
 
 20  
 import org.apache.camel.Exchange;
 21  
 import org.apache.camel.Processor;
 22  
 import org.apache.camel.Message;
 23  
 import org.apache.camel.impl.ServiceSupport;
 24  
 import org.apache.camel.util.ServiceHelper;
 25  
 import org.apache.commons.logging.Log;
 26  
 import org.apache.commons.logging.LogFactory;
 27  
 
 28  
 /**
 29  
  * Implements a
 30  
  * <a href="http://activemq.apache.org/camel/dead-letter-channel.html">Dead Letter Channel</a>
 31  
  * after attempting to redeliver the message using the {@link RedeliveryPolicy}
 32  
  *
 33  
  * @version $Revision: 534145 $
 34  
  */
 35  
 public class DeadLetterChannel extends ServiceSupport implements ErrorHandler {
 36  
     public static final String REDELIVERY_COUNTER = "org.apache.camel.RedeliveryCounter";
 37  
     public static final String REDELIVERED = "org.apache.camel.Redelivered";
 38  
 
 39  1
     private static final transient Log log = LogFactory.getLog(DeadLetterChannel.class);
 40  
     private Processor output;
 41  
     private Processor deadLetter;
 42  
     private RedeliveryPolicy redeliveryPolicy;
 43  
     private Logger logger;
 44  
 
 45  
     public static <E extends Exchange> Logger createDefaultLogger() {
 46  84
         return new Logger(log, LoggingLevel.ERROR);
 47  
     }
 48  
 
 49  
     public DeadLetterChannel(Processor output, Processor deadLetter) {
 50  0
         this(output, deadLetter, new RedeliveryPolicy(), DeadLetterChannel.createDefaultLogger());
 51  0
     }
 52  
 
 53  90
     public DeadLetterChannel(Processor output, Processor deadLetter, RedeliveryPolicy redeliveryPolicy, Logger logger) {
 54  90
         this.deadLetter = deadLetter;
 55  90
         this.output = output;
 56  90
         this.redeliveryPolicy = redeliveryPolicy;
 57  90
         this.logger = logger;
 58  90
     }
 59  
 
 60  
     @Override
 61  
     public String toString() {
 62  139
         return "DeadLetterChannel[" + output + ", " + deadLetter + ", " + redeliveryPolicy + "]";
 63  
     }
 64  
 
 65  
     public void process(Exchange exchange) throws Exception {
 66  785
         int redeliveryCounter = 0;
 67  785
         long redeliveryDelay = 0;
 68  
 
 69  
         do {
 70  788
             if (redeliveryCounter > 0) {
 71  
                 // Figure out how long we should wait to resend this message.
 72  3
                 redeliveryDelay = redeliveryPolicy.getRedeliveryDelay(redeliveryDelay);
 73  3
                 sleep(redeliveryDelay);
 74  
             }
 75  
 
 76  
             try {
 77  787
                 output.process(exchange);
 78  783
                 return;
 79  
             }
 80  4
             catch (RuntimeException e) {
 81  4
                 logger.log("On delivery attempt: " + redeliveryCounter + " caught: " + e, e);
 82  
             }
 83  4
             redeliveryCounter = incrementRedeliveryCounter(exchange);
 84  
         }
 85  4
         while (redeliveryPolicy.shouldRedeliver(redeliveryCounter));
 86  
 
 87  
         // now lets send to the dead letter queue
 88  1
         deadLetter.process(exchange);
 89  1
     }
 90  
 
 91  
     // Properties
 92  
     //-------------------------------------------------------------------------
 93  
 
 94  
     /**
 95  
      * Returns the output processor
 96  
      */
 97  
     public Processor getOutput() {
 98  18
         return output;
 99  
     }
 100  
 
 101  
     /**
 102  
      * Returns the dead letter that message exchanges will be sent to if the redelivery attempts fail
 103  
      */
 104  
     public Processor getDeadLetter() {
 105  0
         return deadLetter;
 106  
     }
 107  
 
 108  
     public RedeliveryPolicy getRedeliveryPolicy() {
 109  0
         return redeliveryPolicy;
 110  
     }
 111  
 
 112  
     /**
 113  
      * Sets the redelivery policy
 114  
      */
 115  
     public void setRedeliveryPolicy(RedeliveryPolicy redeliveryPolicy) {
 116  0
         this.redeliveryPolicy = redeliveryPolicy;
 117  0
     }
 118  
 
 119  
     public Logger getLogger() {
 120  0
         return logger;
 121  
     }
 122  
 
 123  
     /**
 124  
      * Sets the logger strategy; which {@link Log} to use and which {@link LoggingLevel} to use
 125  
      */
 126  
     public void setLogger(Logger logger) {
 127  0
         this.logger = logger;
 128  0
     }
 129  
 
 130  
     // Implementation methods
 131  
     //-------------------------------------------------------------------------
 132  
 
 133  
     /**
 134  
      * Increments the redelivery counter and adds the redelivered flag if the message has been redelivered
 135  
      */
 136  
     protected int incrementRedeliveryCounter(Exchange exchange) {
 137  4
         Message in = exchange.getIn();
 138  4
         Integer counter = in.getHeader(REDELIVERY_COUNTER, Integer.class);
 139  4
         int next = 1;
 140  4
         if (counter != null) {
 141  1
             next = counter + 1;
 142  
         }
 143  4
         in.setHeader(REDELIVERY_COUNTER, next);
 144  4
             in.setHeader(REDELIVERED, true);
 145  4
         return next;
 146  
     }
 147  
 
 148  
     protected void sleep(long redeliveryDelay) {
 149  3
         if (redeliveryDelay > 0) {
 150  3
             if (log.isDebugEnabled()) {
 151  0
                 log.debug("Sleeping for: " + redeliveryDelay + " until attempting redelivery");
 152  
             }
 153  
             try {
 154  3
                 Thread.sleep(redeliveryDelay);
 155  
             }
 156  0
             catch (InterruptedException e) {
 157  0
                 if (log.isDebugEnabled()) {
 158  0
                     log.debug("Thread interupted: " + e, e);
 159  
                 }
 160  2
             }
 161  
         }
 162  2
     }
 163  
 
 164  
     protected void doStart() throws Exception {
 165  69
         ServiceHelper.startServices(output, deadLetter);
 166  69
     }
 167  
 
 168  
     protected void doStop() throws Exception {
 169  67
         ServiceHelper.stopServices(deadLetter, output);
 170  67
     }
 171  
 }