001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.scheduling.support;
018
019import java.util.concurrent.Future;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023
024import org.springframework.lang.Nullable;
025import org.springframework.util.ErrorHandler;
026import org.springframework.util.ReflectionUtils;
027
028/**
029 * Utility methods for decorating tasks with error handling.
030 *
031 * <p><b>NOTE:</b> This class is intended for internal use by Spring's scheduler
032 * implementations. It is only public so that it may be accessed from impl classes
033 * within other packages. It is <i>not</i> intended for general use.
034 *
035 * @author Mark Fisher
036 * @author Juergen Hoeller
037 * @since 3.0
038 */
039public abstract class TaskUtils {
040
041        /**
042         * An ErrorHandler strategy that will log the Exception but perform
043         * no further handling. This will suppress the error so that
044         * subsequent executions of the task will not be prevented.
045         */
046        public static final ErrorHandler LOG_AND_SUPPRESS_ERROR_HANDLER = new LoggingErrorHandler();
047
048        /**
049         * An ErrorHandler strategy that will log at error level and then
050         * re-throw the Exception. Note: this will typically prevent subsequent
051         * execution of a scheduled task.
052         */
053        public static final ErrorHandler LOG_AND_PROPAGATE_ERROR_HANDLER = new PropagatingErrorHandler();
054
055
056        /**
057         * Decorate the task for error handling. If the provided {@link ErrorHandler}
058         * is not {@code null}, it will be used. Otherwise, repeating tasks will have
059         * errors suppressed by default whereas one-shot tasks will have errors
060         * propagated by default since those errors may be expected through the
061         * returned {@link Future}. In both cases, the errors will be logged.
062         */
063        public static DelegatingErrorHandlingRunnable decorateTaskWithErrorHandler(
064                        Runnable task, @Nullable ErrorHandler errorHandler, boolean isRepeatingTask) {
065
066                if (task instanceof DelegatingErrorHandlingRunnable) {
067                        return (DelegatingErrorHandlingRunnable) task;
068                }
069                ErrorHandler eh = (errorHandler != null ? errorHandler : getDefaultErrorHandler(isRepeatingTask));
070                return new DelegatingErrorHandlingRunnable(task, eh);
071        }
072
073        /**
074         * Return the default {@link ErrorHandler} implementation based on the boolean
075         * value indicating whether the task will be repeating or not. For repeating tasks
076         * it will suppress errors, but for one-time tasks it will propagate. In both
077         * cases, the error will be logged.
078         */
079        public static ErrorHandler getDefaultErrorHandler(boolean isRepeatingTask) {
080                return (isRepeatingTask ? LOG_AND_SUPPRESS_ERROR_HANDLER : LOG_AND_PROPAGATE_ERROR_HANDLER);
081        }
082
083
084        /**
085         * An {@link ErrorHandler} implementation that logs the Throwable at error
086         * level. It does not perform any additional error handling. This can be
087         * useful when suppression of errors is the intended behavior.
088         */
089        private static class LoggingErrorHandler implements ErrorHandler {
090
091                private final Log logger = LogFactory.getLog(LoggingErrorHandler.class);
092
093                @Override
094                public void handleError(Throwable t) {
095                        logger.error("Unexpected error occurred in scheduled task", t);
096                }
097        }
098
099
100        /**
101         * An {@link ErrorHandler} implementation that logs the Throwable at error
102         * level and then propagates it.
103         */
104        private static class PropagatingErrorHandler extends LoggingErrorHandler {
105
106                @Override
107                public void handleError(Throwable t) {
108                        super.handleError(t);
109                        ReflectionUtils.rethrowRuntimeException(t);
110                }
111        }
112
113}