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