|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  15 |     | 
     | 
  |  16 |     | 
     | 
  |  17 |     | 
   package org.kuali.rice.ksb.messaging.exceptionhandling;  | 
  |  18 |     | 
     | 
  |  19 |     | 
   import org.apache.commons.lang.StringUtils;  | 
  |  20 |     | 
   import org.apache.log4j.Logger;  | 
  |  21 |     | 
   import org.kuali.rice.core.api.config.property.ConfigContext;  | 
  |  22 |     | 
   import org.kuali.rice.ksb.messaging.PersistedMessageBO;  | 
  |  23 |     | 
   import org.kuali.rice.ksb.messaging.ServiceInfo;  | 
  |  24 |     | 
   import org.kuali.rice.ksb.service.KSBServiceLocator;  | 
  |  25 |     | 
   import org.kuali.rice.ksb.util.KSBConstants;  | 
  |  26 |     | 
     | 
  |  27 |     | 
   import java.sql.Timestamp;  | 
  |  28 |     | 
     | 
  |  29 |     | 
     | 
  |  30 |     | 
     | 
  |  31 |     | 
     | 
  |  32 |     | 
     | 
  |  33 |     | 
     | 
  |  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 |            ServiceInfo serviceInfo = message.getMethodCall().getServiceInfo();  | 
  |  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 (serviceInfo.getServiceDefinition().getRetryAttempts() > 0) { | 
  |  70 |    0 |                LOG.info("Message set for retry exception handling.  Message retry count = " + message.getRetryCount()); | 
  |  71 |    0 |                if (message.getRetryCount() >= serviceInfo.getServiceDefinition().getRetryAttempts()) { | 
  |  72 |    0 |                    return true;  | 
  |  73 |     | 
               }  | 
  |  74 |    0 |            } else if (serviceInfo.getServiceDefinition().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.getRouteQueueService().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 |     | 
   }  |