Coverage Report - org.mule.transport.jms.redelivery.JmsXRedeliveryHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
JmsXRedeliveryHandler
0%
0/26
0%
0/16
0
 
 1  
 /*
 2  
  * $Id: JmsXRedeliveryHandler.java 20501 2010-12-08 00:52:19Z 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.redelivery;
 12  
 
 13  
 import org.mule.api.MuleException;
 14  
 import org.mule.api.MuleMessage;
 15  
 import org.mule.api.MuleRuntimeException;
 16  
 import org.mule.api.construct.FlowConstruct;
 17  
 import org.mule.api.endpoint.ImmutableEndpoint;
 18  
 import org.mule.config.i18n.MessageFactory;
 19  
 import org.mule.transport.jms.JmsConnector;
 20  
 import org.mule.transport.jms.JmsConstants;
 21  
 
 22  
 import javax.jms.JMSException;
 23  
 import javax.jms.Message;
 24  
 
 25  
 import org.apache.commons.logging.Log;
 26  
 import org.apache.commons.logging.LogFactory;
 27  
 
 28  
 /**
 29  
  * A redelivery handler which relies on JMS provider's redelivery count facilities.
 30  
  * @see org.mule.transport.jms.JmsConstants#JMS_X_DELIVERY_COUNT
 31  
  */
 32  0
 public class JmsXRedeliveryHandler extends AbstractRedeliveryHandler
 33  
 {
 34  
     /**
 35  
      * logger used by this class
 36  
      */
 37  0
     protected static final Log logger = LogFactory.getLog(JmsXRedeliveryHandler.class);
 38  
 
 39  
     /**
 40  
      * process the redelivered message. If the Jms receiver should process the
 41  
      * message, it should be returned. Otherwise the connector should throw a
 42  
      * <code>MessageRedeliveredException</code> to indicate that the message should
 43  
      * be handled by the connector Exception Handler.
 44  
      * 
 45  
      */
 46  
     @Override
 47  
     public void handleRedelivery(Message message, ImmutableEndpoint endpoint, FlowConstruct flow) throws JMSException, MuleException
 48  
     {
 49  0
         final int connectorRedelivery = connector.getMaxRedelivery();
 50  0
         if (connectorRedelivery == JmsConnector.REDELIVERY_IGNORE || connectorRedelivery < 0 ) // just in case, for manual setting)
 51  
         {
 52  0
             if (logger.isDebugEnabled())
 53  
             {
 54  0
                 logger.debug("We were asked to ignore the redelivery count, nothing to do here.");
 55  
             }
 56  0
             return;
 57  
         }
 58  
 
 59  0
         String messageId = message.getJMSMessageID();
 60  
 
 61  0
         int deliveryCount = -1;
 62  
         try
 63  
         {
 64  0
             deliveryCount = message.getIntProperty(JmsConstants.JMS_X_DELIVERY_COUNT);
 65  
         }
 66  0
         catch (NumberFormatException nex)
 67  
         {
 68  0
             throw new MuleRuntimeException(MessageFactory.createStaticMessage(String.format(
 69  
                     "Invalid use of %s. Message is flagged with JMSRedelivered, but JMSXDeliveryCount is not set",
 70  
                     getClass().getName())));
 71  0
         }
 72  
 
 73  0
         int redeliveryCount = deliveryCount - 1;
 74  
 
 75  0
         if (redeliveryCount == 1)
 76  
         {
 77  0
             if (logger.isDebugEnabled())
 78  
             {
 79  0
                 logger.debug("Message with id: " + messageId + " has been redelivered for the first time");
 80  
             }
 81  
 
 82  0
             if (connectorRedelivery == JmsConnector.REDELIVERY_FAIL_ON_FIRST)
 83  
             {
 84  0
                 MuleMessage msg = createMuleMessage(message);
 85  0
                 throw new MessageRedeliveredException(messageId, redeliveryCount, connectorRedelivery, endpoint, flow, msg);
 86  
             }
 87  
         }
 88  0
         else if (redeliveryCount > connectorRedelivery)
 89  
         {
 90  0
             MuleMessage msg = createMuleMessage(message);
 91  0
             throw new MessageRedeliveredException(messageId, redeliveryCount, connectorRedelivery, endpoint, flow, msg);
 92  
         }
 93  
         else
 94  
         {
 95  0
             if (logger.isDebugEnabled())
 96  
             {
 97  
                 // re-delivery count is actually less by 1 than an actual delivery count
 98  0
                 logger.debug("Message with id: " + messageId + " has been redelivered " + redeliveryCount + " times");
 99  
             }
 100  
         }
 101  0
     }
 102  
 }