Coverage Report - org.mule.transport.jms.CountingRedeliveryHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
CountingRedeliveryHandler
0%
0/37
0%
0/28
0
 
 1  
 /*
 2  
  * $Id: CountingRedeliveryHandler.java 19191 2010-08-25 21:05:23Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.transport.jms;
 12  
 
 13  
 import org.mule.api.MuleException;
 14  
 import org.mule.api.MuleMessage;
 15  
 import org.mule.transport.jms.i18n.JmsMessages;
 16  
 
 17  
 import java.text.MessageFormat;
 18  
 import java.util.Collections;
 19  
 import java.util.Map;
 20  
 
 21  
 import javax.jms.JMSException;
 22  
 import javax.jms.Message;
 23  
 
 24  
 import org.apache.commons.collections.map.LRUMap;
 25  
 import org.apache.commons.logging.Log;
 26  
 import org.apache.commons.logging.LogFactory;
 27  
 
 28  
 /**
 29  
  * This redelivery handler will keep counting the redelivery attempts for each message redelivered. Used for
 30  
  * providers not implementing the {@code JMSXDeliveryCount} property support.
 31  
  */
 32  
 public class CountingRedeliveryHandler extends AbstractRedeliveryHandler
 33  
 {
 34  
     /**
 35  
      * logger used by this class
 36  
      */
 37  0
     protected static final Log logger = LogFactory.getLog(CountingRedeliveryHandler.class);
 38  
 
 39  0
     private Map<String, Integer> messages = null;
 40  
 
 41  
     @SuppressWarnings("unchecked")
 42  
     public CountingRedeliveryHandler()
 43  
     {
 44  0
         super();
 45  0
         messages = Collections.synchronizedMap(new LRUMap(256));
 46  0
     }
 47  
 
 48  
     /**
 49  
      * process the redelivered message. If the Jms receiver should process the
 50  
      * message, it should be returned. Otherwise the connector should throw a
 51  
      * <code>MessageRedeliveredException</code> to indicate that the message should
 52  
      * be handled by the connector Exception Handler.
 53  
      * 
 54  
      */
 55  
     @Override
 56  
     public void handleRedelivery(Message message) throws JMSException, MuleException
 57  
     {
 58  0
         final int connectorRedelivery = connector.getMaxRedelivery();
 59  0
         if (connectorRedelivery == JmsConnector.REDELIVERY_IGNORE || connectorRedelivery < 0 ) // just in case, for manual setting)
 60  
         {
 61  0
             if (logger.isDebugEnabled())
 62  
             {
 63  0
                 logger.debug("We were asked to ignore the redelivery count, nothing to do here.");
 64  
             }
 65  0
             return;
 66  
         }
 67  
 
 68  0
         String id = message.getJMSMessageID();
 69  
 
 70  0
         if (id == null)
 71  
         {
 72  0
             if (logger.isDebugEnabled())
 73  
             {
 74  0
                 logger.debug("Message doesn't have a JMSMessageID set, Mule can't handle redelivery for it. " + message);
 75  
             }
 76  0
             return;
 77  
         }
 78  
 
 79  0
         Integer redeliveryCount = messages.remove(id);
 80  0
         if (redeliveryCount != null)
 81  
         {
 82  0
             redeliveryCount += 1; // inc the count
 83  
         }
 84  
 
 85  0
         if (redeliveryCount == null)
 86  
         {
 87  0
             if (logger.isDebugEnabled())
 88  
             {
 89  0
                 logger.debug("Message with id: " + id + " has been redelivered for the first time");
 90  
             }
 91  0
             messages.put(id, 1);
 92  
         }
 93  0
         else if (redeliveryCount == 1)
 94  
         {
 95  0
             if (logger.isDebugEnabled())
 96  
             {
 97  0
                 logger.debug("Message with id: " + id + " has been redelivered for the first time");
 98  
             }
 99  
 
 100  0
             if (connectorRedelivery == JmsConnector.REDELIVERY_FAIL_ON_FIRST)
 101  
             {
 102  0
                 MuleMessage msg = createMuleMessage(message);
 103  0
                 throw new MessageRedeliveredException(
 104  
                     JmsMessages.tooManyRedeliveries(id, String.valueOf(redeliveryCount), 
 105  
                         connectorRedelivery, connector.getName()), msg);
 106  
             }
 107  
         }
 108  0
         else if (redeliveryCount > connectorRedelivery)
 109  
         {
 110  0
             if (logger.isDebugEnabled())
 111  
             {
 112  0
                 logger.debug(MessageFormat.format(
 113  
                         "Message with id: {0} has been redelivered {1} times, which exceeds the maxRedelivery setting " +
 114  
                         "of {2} on the connector {3}", id, redeliveryCount, connectorRedelivery, connector.getName()));
 115  
             }
 116  0
             MuleMessage msg = createMuleMessage(message);
 117  0
             throw new MessageRedeliveredException(
 118  
                 JmsMessages.tooManyRedeliveries(id, "" + redeliveryCount, connectorRedelivery,
 119  
                     connector.getName()), msg);
 120  
         }
 121  
         else
 122  
         {
 123  0
             messages.put(id, redeliveryCount);
 124  0
             if (logger.isDebugEnabled())
 125  
             {
 126  0
                 logger.debug("Message with id: " + id + " has been redelivered " + redeliveryCount + " times");
 127  
             }
 128  
         }
 129  0
     }
 130  
 }