Coverage Report - org.kuali.rice.ksb.messaging.exceptionhandling.DefaultMessageExceptionHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultMessageExceptionHandler
0%
0/63
0%
0/22
3.2
 
 1  
 /*
 2  
  * Copyright 2006-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 
 17  
 package org.kuali.rice.ksb.messaging.exceptionhandling;
 18  
 
 19  
 import java.sql.Timestamp;
 20  
 
 21  
 import org.apache.commons.lang.StringUtils;
 22  
 import org.apache.log4j.Logger;
 23  
 import org.kuali.rice.core.api.config.property.ConfigContext;
 24  
 import org.kuali.rice.ksb.api.bus.ServiceConfiguration;
 25  
 import org.kuali.rice.ksb.messaging.PersistedMessageBO;
 26  
 import org.kuali.rice.ksb.service.KSBServiceLocator;
 27  
 import org.kuali.rice.ksb.util.KSBConstants;
 28  
 
 29  
 
 30  
 /**
 31  
  * Default implementation of the {@link MessageExceptionHandler} which handles exceptions thrown from message processing.
 32  
  * 
 33  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 34  
  */
 35  0
 public class DefaultMessageExceptionHandler implements MessageExceptionHandler {
 36  
 
 37  0
     private static final Logger LOG = Logger.getLogger(DefaultMessageExceptionHandler.class);
 38  
 
 39  
     private static final long DEFAULT_TIME_INCREMENT = 60 * 60 * 1000;
 40  
 
 41  
     private static final int DEFAULT_MAX_RETRIES = 7;
 42  
 
 43  
     public void handleException(Throwable throwable, PersistedMessageBO message, Object service) throws Exception {
 44  0
         if (isInException(message)) {
 45  0
             placeInException(throwable, message);
 46  
         } else {
 47  0
             requeue(throwable, message);
 48  
         }
 49  0
     }
 50  
 
 51  
     public void handleExceptionLastDitchEffort(Throwable throwable, PersistedMessageBO message, Object service) throws Exception {
 52  0
                 LOG.error("Complete failure when attempting to put message into exception routing!  Message was: " + message, throwable);
 53  0
         }
 54  
 
 55  
         public boolean isInException(PersistedMessageBO message) {
 56  0
         ServiceConfiguration serviceConfiguration = message.getMethodCall().getServiceConfiguration();
 57  
 
 58  0
         if (getImmediateExceptionRouting()) {
 59  0
             return true;
 60  
         }
 61  
 
 62  0
         Integer globalMaxRetryAttempts = getGlobalMaxRetryAttempts();
 63  0
         if (globalMaxRetryAttempts != null) {
 64  0
             LOG.info("Global Max Retry has been set, so is overriding other max retry attempts.");
 65  0
             LOG.info("Global Max Retry count = " + globalMaxRetryAttempts + ".");
 66  0
             return (message.getRetryCount().intValue() >= globalMaxRetryAttempts.intValue());
 67  
         }
 68  
 
 69  0
         if (serviceConfiguration.getRetryAttempts() > 0) {
 70  0
             LOG.info("Message set for retry exception handling.  Message retry count = " + message.getRetryCount());
 71  0
             if (message.getRetryCount() >= serviceConfiguration.getRetryAttempts()) {
 72  0
                 return true;
 73  
             }
 74  0
         } else if (serviceConfiguration.getMillisToLive() > 0) {
 75  0
             LOG.info("Message set for time to live exception handling.  Message expiration date = " + message.getExpirationDate().getTime());
 76  0
             if (System.currentTimeMillis() > message.getExpirationDate().getTime()) {
 77  0
                 return true;
 78  
             }
 79  0
         } else if (message.getRetryCount() >= this.getMaxRetryAttempts()) {
 80  0
             LOG.info("Message set for default exception handling.  Comparing retry count = " + message.getRetryCount() + " against default max count.");
 81  0
             return true;
 82  
         }
 83  0
         return false;
 84  
     }
 85  
 
 86  
     protected void requeue(Throwable throwable, PersistedMessageBO message) throws Exception {
 87  0
         Integer retryCount = message.getRetryCount();
 88  0
         message.setQueueStatus(KSBConstants.ROUTE_QUEUE_QUEUED);
 89  0
         long addMilliseconds = Math.round(getTimeIncrement() * Math.pow(2, retryCount));
 90  0
         Timestamp currentTime = message.getQueueDate();
 91  0
         Timestamp newTime = new Timestamp(currentTime.getTime() + addMilliseconds);
 92  0
         message.setQueueStatus(KSBConstants.ROUTE_QUEUE_QUEUED);
 93  0
         message.setRetryCount(new Integer(retryCount + 1));
 94  0
         message.setQueueDate(newTime);
 95  0
         scheduleExecution(throwable, message);
 96  0
     }
 97  
 
 98  
     protected void placeInException(Throwable throwable, PersistedMessageBO message) throws Exception {
 99  0
         message.setQueueStatus(KSBConstants.ROUTE_QUEUE_EXCEPTION);
 100  0
         message.setQueueDate(new Timestamp(System.currentTimeMillis()));
 101  0
         KSBServiceLocator.getMessageQueueService().save(message);
 102  0
     }
 103  
 
 104  
     protected void scheduleExecution(Throwable throwable, PersistedMessageBO message) throws Exception {
 105  0
         KSBServiceLocator.getExceptionRoutingService().scheduleExecution(throwable, message, null);
 106  0
     }
 107  
 
 108  
     public Integer getMaxRetryAttempts() {
 109  
         try {
 110  0
             return new Integer(ConfigContext.getCurrentContextConfig().getProperty(KSBConstants.Config.ROUTE_QUEUE_MAX_RETRY_ATTEMPTS_KEY));
 111  0
         } catch (NumberFormatException e) {
 112  0
             LOG.error("Constant '" + KSBConstants.Config.ROUTE_QUEUE_MAX_RETRY_ATTEMPTS_KEY + "' is not a number and is being " + "used as a default for exception messages.  " + DEFAULT_MAX_RETRIES + " will be used as a retry limit until this number is fixed");
 113  0
             return DEFAULT_MAX_RETRIES;
 114  
         }
 115  
     }
 116  
 
 117  
     public Integer getGlobalMaxRetryAttempts() {
 118  0
         String globalMax = ConfigContext.getCurrentContextConfig().getProperty(KSBConstants.Config.ROUTE_QUEUE_MAX_RETRY_ATTEMPTS_OVERRIDE_KEY);
 119  0
         if (StringUtils.isBlank(globalMax)) {
 120  0
             return null;
 121  
         }
 122  
         try {
 123  0
             Integer globalMaxRetries = new Integer(globalMax);
 124  0
             if (globalMaxRetries >= 0) {
 125  0
                 return globalMaxRetries;
 126  
             }
 127  0
         } catch (NumberFormatException e) {
 128  0
             LOG.error("Constant '" + KSBConstants.Config.ROUTE_QUEUE_MAX_RETRY_ATTEMPTS_OVERRIDE_KEY + "' is not a number and is being " + "used as a default for exception messages.  " + DEFAULT_MAX_RETRIES + " will be used as a retry limit until this number is fixed");
 129  0
         }
 130  0
         return null;
 131  
     }
 132  
 
 133  
     public Long getTimeIncrement() {
 134  
         try {
 135  0
             return new Long(ConfigContext.getCurrentContextConfig().getProperty(KSBConstants.Config.ROUTE_QUEUE_TIME_INCREMENT_KEY));
 136  0
         } catch (NumberFormatException e) {
 137  0
             LOG.error("Constant '" + KSBConstants.Config.ROUTE_QUEUE_TIME_INCREMENT_KEY + "' is not a number and will not be used " + "as the default time increment for exception routing.  Default of " + DEFAULT_TIME_INCREMENT + " will be used.");
 138  0
             return DEFAULT_TIME_INCREMENT;
 139  
         }
 140  
     }
 141  
 
 142  
     public Boolean getImmediateExceptionRouting() {
 143  0
         return new Boolean(ConfigContext.getCurrentContextConfig().getProperty(KSBConstants.Config.IMMEDIATE_EXCEPTION_ROUTING));
 144  
     }
 145  
 }