001/*
002 * Copyright 2002-2020 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.core.task;
018
019/**
020 * A callback interface for a decorator to be applied to any {@link Runnable}
021 * about to be executed.
022 *
023 * <p>Note that such a decorator is not necessarily being applied to the
024 * user-supplied {@code Runnable}/{@code Callable} but rather to the actual
025 * execution callback (which may be a wrapper around the user-supplied task).
026 *
027 * <p>The primary use case is to set some execution context around the task's
028 * invocation, or to provide some monitoring/statistics for task execution.
029 *
030 * <p><b>NOTE:</b> Exception handling in {@code TaskDecorator} implementations
031 * may be limited. Specifically in case of a {@code Future}-based operation,
032 * the exposed {@code Runnable} will be a wrapper which does not propagate
033 * any exceptions from its {@code run} method.
034 *
035 * @author Juergen Hoeller
036 * @since 4.3
037 * @see TaskExecutor#execute(Runnable)
038 * @see SimpleAsyncTaskExecutor#setTaskDecorator
039 * @see org.springframework.core.task.support.TaskExecutorAdapter#setTaskDecorator
040 */
041public interface TaskDecorator {
042
043        /**
044         * Decorate the given {@code Runnable}, returning a potentially wrapped
045         * {@code Runnable} for actual execution, internally delegating to the
046         * original {@link Runnable#run()} implementation.
047         * @param runnable the original {@code Runnable}
048         * @return the decorated {@code Runnable}
049         */
050        Runnable decorate(Runnable runnable);
051
052}