Coverage Report - org.mule.providers.jms.DefaultRedeliveryHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultRedeliveryHandler
0%
0/25
0%
0/6
4
 
 1  
 /*
 2  
  * $Id: DefaultRedeliveryHandler.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.providers.jms;
 12  
 
 13  
 import org.mule.providers.jms.i18n.JmsMessages;
 14  
 import org.mule.umo.MessagingException;
 15  
 
 16  
 import java.util.Collections;
 17  
 import java.util.Map;
 18  
 
 19  
 import javax.jms.JMSException;
 20  
 import javax.jms.Message;
 21  
 
 22  
 import org.apache.commons.collections.map.LRUMap;
 23  
 import org.apache.commons.logging.Log;
 24  
 import org.apache.commons.logging.LogFactory;
 25  
 
 26  
 /**
 27  
  * <code>DefaultRedeliveryHandler</code> TODO
 28  
  */
 29  
 public class DefaultRedeliveryHandler implements RedeliveryHandler
 30  
 {
 31  
     /**
 32  
      * logger used by this class
 33  
      */
 34  0
     protected static final Log logger = LogFactory.getLog(DefaultRedeliveryHandler.class);
 35  
 
 36  0
     private Map messages = null;
 37  
 
 38  
     protected JmsConnector connector;
 39  
 
 40  
     public DefaultRedeliveryHandler()
 41  0
     {
 42  0
         messages = Collections.synchronizedMap(new LRUMap(256));
 43  0
     }
 44  
 
 45  
     /**
 46  
      * The connector associated with this handler is set before
 47  
      * <code>handleRedelivery()</code> is called
 48  
      * 
 49  
      * @param connector the connector associated with this handler
 50  
      */
 51  
     public void setConnector(JmsConnector connector)
 52  
     {
 53  0
         this.connector = connector;
 54  0
     }
 55  
 
 56  
     /**
 57  
      * process the redelivered message. If the Jms receiver should process the
 58  
      * message, it should be returned. Otherwise the connector should throw a
 59  
      * <code>MessageRedeliveredException</code> to indicate that the message should
 60  
      * be handled by the connector Exception Handler.
 61  
      * 
 62  
      * @param message
 63  
      */
 64  
     public void handleRedelivery(Message message) throws JMSException, MessagingException
 65  
     {
 66  0
         if (connector.getMaxRedelivery() <= 0)
 67  
         {
 68  0
             return;
 69  
         }
 70  
 
 71  0
         String id = message.getJMSMessageID();
 72  0
         Integer i = (Integer)messages.remove(id);
 73  0
         if (i == null)
 74  
         {
 75  0
             if (logger.isDebugEnabled())
 76  
             {
 77  0
                 logger.debug("Message with id: " + id + " has been redelivered for the first time");
 78  
             }
 79  0
             messages.put(id, new Integer(1));
 80  0
             return;
 81  
         }
 82  0
         else if (i.intValue() == connector.getMaxRedelivery())
 83  
         {
 84  0
             if (logger.isDebugEnabled())
 85  
             {
 86  0
                 logger.debug("Message with id: " + id + " has been redelivered " + (i.intValue() + 1)
 87  
                              + " times, which exceeds the maxRedelivery setting on the connector");
 88  
             }
 89  0
             JmsMessageAdapter adapter = (JmsMessageAdapter)connector.getMessageAdapter(message);
 90  0
             throw new MessageRedeliveredException(
 91  
                 JmsMessages.tooManyRedeliveries(id, String.valueOf(i.intValue() + 1)), adapter);
 92  
 
 93  
         }
 94  
         else
 95  
         {
 96  0
             messages.put(id, new Integer(i.intValue() + 1));
 97  0
             if (logger.isDebugEnabled())
 98  
             {
 99  0
                 logger.debug("Message with id: " + id + " has been redelivered " + i.intValue() + " times");
 100  
             }
 101  
         }
 102  0
     }
 103  
 }