/** * Wrap the provided {@link org.springframework.retry.RetryPolicy} so that it never retries explicitly non-retryable * exceptions. */ private RetryPolicy getFatalExceptionAwareProxy(RetryPolicy retryPolicy) { NeverRetryPolicy neverRetryPolicy = new NeverRetryPolicy(); Map<Class<? extends Throwable>, RetryPolicy> map = new HashMap<>(); for (Class<? extends Throwable> fatal : nonRetryableExceptionClasses) { map.put(fatal, neverRetryPolicy); } SubclassClassifier<Throwable, RetryPolicy> classifier = new SubclassClassifier<>( retryPolicy); classifier.setTypeMap(map); ExceptionClassifierRetryPolicy retryPolicyWrapper = new ExceptionClassifierRetryPolicy(); retryPolicyWrapper.setExceptionClassifier(classifier); return retryPolicyWrapper; }
/** * Consult the classifier and find a delegate policy, and then use that to * determine the outcome. * * @param t the throwable to consider * @param skipCount the current skip count * @return true if the exception can be skipped * @throws SkipLimitExceededException if a limit is exceeded */ @Override public boolean shouldSkip(Throwable t, int skipCount) throws SkipLimitExceededException { return classifier.classify(t).shouldSkip(t, skipCount); }
@Override public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { Recover recover = AnnotationUtils.findAnnotation(method, Recover.class); if (recover != null && method.getReturnType().isAssignableFrom( failingMethod.getReturnType())) { Class<?>[] parameterTypes = method.getParameterTypes(); if (parameterTypes.length > 0 && Throwable.class .isAssignableFrom(parameterTypes[0])) { @SuppressWarnings("unchecked") Class<? extends Throwable> type = (Class<? extends Throwable>) parameterTypes[0]; types.put(type, method); methods.put(method, new SimpleMetadata( parameterTypes.length, type)); } else { classifier.setDefaultValue(method); methods.put(method, new SimpleMetadata( parameterTypes.length, null)); } } } });
/** * A map from exception classes to a threshold value of type Integer. * * @param thresholds the threshold value map. */ public void setThresholds(Map<Class<? extends Throwable>, Integer> thresholds) { Map<Class<? extends Throwable>, IntegerHolder> typeMap = new HashMap<>(); for (Entry<Class<? extends Throwable>, Integer> entry : thresholds.entrySet()) { typeMap.put(entry.getKey(), new IntegerHolder(entry.getValue())); } exceptionClassifier = new SubclassClassifier<>(typeMap, ZERO); }
classifier.setTypeMap(types); optionallyFilterMethodsBy(failingMethod.getReturnType());
/** * Setter for policy map used to create a classifier. Either this property * or the exception classifier directly should be set, but not both. * * @param policyMap a map of Throwable class to {@link RetryPolicy} that * will be used to create a {@link Classifier} to locate a policy. */ public void setPolicyMap(Map<Class<? extends Throwable>, RetryPolicy> policyMap) { this.exceptionClassifier = new SubclassClassifier<Throwable, RetryPolicy>(policyMap, new NeverRetryPolicy()); }
classifier.setTypeMap(types); optionallyFilterMethodsBy(failingMethod.getReturnType());
/** * Wrap a {@link SkipPolicy} and make it consistent with known fatal exceptions. * * @param skipPolicy an existing skip policy * @return a skip policy that will not skip fatal exceptions */ protected SkipPolicy getFatalExceptionAwareProxy(SkipPolicy skipPolicy) { NeverSkipItemSkipPolicy neverSkipPolicy = new NeverSkipItemSkipPolicy(); Map<Class<? extends Throwable>, SkipPolicy> map = new HashMap<>(); for (Class<? extends Throwable> fatal : nonSkippableExceptionClasses) { map.put(fatal, neverSkipPolicy); } SubclassClassifier<Throwable, SkipPolicy> classifier = new SubclassClassifier<>(skipPolicy); classifier.setTypeMap(map); ExceptionClassifierSkipPolicy skipPolicyWrapper = new ExceptionClassifierSkipPolicy(); skipPolicyWrapper.setExceptionClassifier(classifier); return skipPolicyWrapper; }
/** * Setter for policy map. This property should not be changed dynamically - * set it once, e.g. in configuration, and then don't change it during a * running application. Either this property or the exception classifier * directly should be set, but not both. * * @param policyMap a map of String to {@link SkipPolicy} that will be used * to create a {@link Classifier} to locate a policy. */ public void setPolicyMap(Map<Class<? extends Throwable>, SkipPolicy> policyMap) { SubclassClassifier<Throwable, SkipPolicy> subclassClassifier = new SubclassClassifier<>( policyMap, new NeverSkipItemSkipPolicy()); this.classifier = subclassClassifier; }
@Override public Boolean classify(Throwable classifiable) { Boolean classified = super.classify(classifiable); if (!this.traverseCauses) { return classified; } /* * If the result is the default, we need to find out if it was by default * or so configured; if default, try the cause(es). */ if (classified.equals(this.getDefault())) { Throwable cause = classifiable; do { if (this.getClassified().containsKey(cause.getClass())) { return classified; // non-default classification } cause = cause.getCause(); classified = super.classify(cause); } while (cause != null && classified.equals(this.getDefault())); } return classified; }
@Override public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { Recover recover = AnnotationUtils.findAnnotation(method, Recover.class); if (recover != null && method.getReturnType().isAssignableFrom( failingMethod.getReturnType())) { Class<?>[] parameterTypes = method.getParameterTypes(); if (parameterTypes.length > 0 && Throwable.class .isAssignableFrom(parameterTypes[0])) { @SuppressWarnings("unchecked") Class<? extends Throwable> type = (Class<? extends Throwable>) parameterTypes[0]; types.put(type, method); methods.put(method, new SimpleMetadata( parameterTypes.length, type)); } else { classifier.setDefaultValue(method); methods.put(method, new SimpleMetadata( parameterTypes.length, null)); } } } });
new SubclassClassifier<>(); Map<Class<? extends Number>, ItemProcessor<?, ? extends CharSequence>> typeMap = new HashMap<>(); typeMap.put(Long.class, longProcessor); typeMap.put(Number.class, defaultProcessor); classifier.setTypeMap(typeMap); processor.setClassifier(classifier);
/** * A map from exception classes to a threshold value of type Integer. * * @param thresholds the threshold value map. */ public void setThresholds(Map<Class<? extends Throwable>, Integer> thresholds) { Map<Class<? extends Throwable>, IntegerHolder> typeMap = new HashMap<Class<? extends Throwable>, IntegerHolder>(); for (Entry<Class<? extends Throwable>, Integer> entry : thresholds.entrySet()) { typeMap.put(entry.getKey(), new IntegerHolder(entry.getValue())); } exceptionClassifier = new SubclassClassifier<>(typeMap, ZERO); }
private Object getPolicy(String stepName, ApplicationContext ctx, String componentName) throws Exception { @SuppressWarnings("unchecked") SubclassClassifier<Throwable, Object> classifier = (SubclassClassifier<Throwable, Object>) getNestedPathInStep( stepName, ctx, componentName); Object policy = classifier.classify(new Exception()); return policy; }
/** * Wrap the provided {@link org.springframework.retry.RetryPolicy} so that it never retries explicitly non-retryable * exceptions. */ private RetryPolicy getFatalExceptionAwareProxy(RetryPolicy retryPolicy) { NeverRetryPolicy neverRetryPolicy = new NeverRetryPolicy(); Map<Class<? extends Throwable>, RetryPolicy> map = new HashMap<Class<? extends Throwable>, RetryPolicy>(); for (Class<? extends Throwable> fatal : nonRetryableExceptionClasses) { map.put(fatal, neverRetryPolicy); } SubclassClassifier<Throwable, RetryPolicy> classifier = new SubclassClassifier<Throwable, RetryPolicy>( retryPolicy); classifier.setTypeMap(map); ExceptionClassifierRetryPolicy retryPolicyWrapper = new ExceptionClassifierRetryPolicy(); retryPolicyWrapper.setExceptionClassifier(classifier); return retryPolicyWrapper; }
/** * Setter for policy map used to create a classifier. Either this property * or the exception classifier directly should be set, but not both. * * @param policyMap a map of Throwable class to {@link RetryPolicy} that * will be used to create a {@link Classifier} to locate a policy. */ public void setPolicyMap(Map<Class<? extends Throwable>, RetryPolicy> policyMap) { this.exceptionClassifier = new SubclassClassifier<Throwable, RetryPolicy>(policyMap, new NeverRetryPolicy()); }
/** * Consult the classifier and find a delegate policy, and then use that to * determine the outcome. * * @param t the throwable to consider * @param skipCount the current skip count * @return true if the exception can be skipped * @throws SkipLimitExceededException if a limit is exceeded */ @Override public boolean shouldSkip(Throwable t, int skipCount) throws SkipLimitExceededException { return classifier.classify(t).shouldSkip(t, skipCount); }
/** * Wrap a {@link SkipPolicy} and make it consistent with known fatal exceptions. * * @param skipPolicy an existing skip policy * @return a skip policy that will not skip fatal exceptions */ protected SkipPolicy getFatalExceptionAwareProxy(SkipPolicy skipPolicy) { NeverSkipItemSkipPolicy neverSkipPolicy = new NeverSkipItemSkipPolicy(); Map<Class<? extends Throwable>, SkipPolicy> map = new HashMap<Class<? extends Throwable>, SkipPolicy>(); for (Class<? extends Throwable> fatal : nonSkippableExceptionClasses) { map.put(fatal, neverSkipPolicy); } SubclassClassifier<Throwable, SkipPolicy> classifier = new SubclassClassifier<Throwable, SkipPolicy>(skipPolicy); classifier.setTypeMap(map); ExceptionClassifierSkipPolicy skipPolicyWrapper = new ExceptionClassifierSkipPolicy(); skipPolicyWrapper.setExceptionClassifier(classifier); return skipPolicyWrapper; }
/** * Setter for policy map. This property should not be changed dynamically - * set it once, e.g. in configuration, and then don't change it during a * running application. Either this property or the exception classifier * directly should be set, but not both. * * @param policyMap a map of String to {@link SkipPolicy} that will be used * to create a {@link Classifier} to locate a policy. */ public void setPolicyMap(Map<Class<? extends Throwable>, SkipPolicy> policyMap) { SubclassClassifier<Throwable, SkipPolicy> subclassClassifier = new SubclassClassifier<Throwable, SkipPolicy>( policyMap, new NeverSkipItemSkipPolicy()); this.classifier = subclassClassifier; }
/** * Consult the classifier and find a delegate policy, and then use that to * determine the outcome. * * @param t the throwable to consider * @param skipCount the current skip count * @return true if the exception can be skipped * @throws SkipLimitExceededException if a limit is exceeded */ @Override public boolean shouldSkip(Throwable t, int skipCount) throws SkipLimitExceededException { return classifier.classify(t).shouldSkip(t, skipCount); }