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