Browse Source
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@1964 50f2f4bb-b051-0410-bef5-90022cba6387pull/1/head
12 changed files with 127 additions and 162 deletions
@ -1,52 +0,0 @@
@@ -1,52 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.scheduling.concurrent; |
||||
|
||||
import java.util.concurrent.Future; |
||||
|
||||
import org.springframework.scheduling.support.DelegatingErrorHandlingRunnable; |
||||
import org.springframework.scheduling.support.ErrorHandler; |
||||
|
||||
/** |
||||
* @author Mark Fisher |
||||
* @since 3.0 |
||||
*/ |
||||
abstract class TaskUtils { |
||||
|
||||
/** |
||||
* Decorates the task for error handling. If the provided |
||||
* {@link ErrorHandler} is not null, it will be used. Otherwise, |
||||
* repeating tasks will have errors suppressed by default whereas |
||||
* one-shot tasks will have errors propagated by default since those |
||||
* errors may be expected through the returned {@link Future}. In both |
||||
* cases, the errors will be logged. |
||||
*/ |
||||
static DelegatingErrorHandlingRunnable errorHandlingTask( |
||||
Runnable task, ErrorHandler errorHandler, boolean isRepeatingTask) { |
||||
|
||||
if (task instanceof DelegatingErrorHandlingRunnable) { |
||||
return (DelegatingErrorHandlingRunnable) task; |
||||
} |
||||
ErrorHandler eh = errorHandler != null ? errorHandler : getDefaultErrorHandler(isRepeatingTask); |
||||
return new DelegatingErrorHandlingRunnable(task, eh); |
||||
} |
||||
|
||||
static ErrorHandler getDefaultErrorHandler(boolean isRepeatingTask) { |
||||
return (isRepeatingTask ? ErrorHandler.LOG_AND_SUPPRESS : ErrorHandler.LOG_AND_PROPAGATE); |
||||
} |
||||
|
||||
} |
||||
@ -1,45 +0,0 @@
@@ -1,45 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.scheduling.support; |
||||
|
||||
/** |
||||
* A strategy for handling errors that occur during asynchronous |
||||
* execution of tasks that have been submitted to a TaskScheduler. |
||||
* |
||||
* @author Mark Fisher |
||||
* @since 3.0. |
||||
*/ |
||||
public interface ErrorHandler { |
||||
|
||||
/** |
||||
* An ErrorHandler strategy that will log the Exception but perform |
||||
* no further handling. This will suppress the error so that |
||||
* subsequent executions of the task will not be prevented. |
||||
*/ |
||||
static final ErrorHandler LOG_AND_SUPPRESS = new LoggingErrorHandler(); |
||||
|
||||
/** |
||||
* An ErrorHandler strategy that will log at error level and then |
||||
* re-throw the Exception. Note: this will typically prevent subsequent |
||||
* execution of a scheduled task. |
||||
*/ |
||||
static final ErrorHandler LOG_AND_PROPAGATE = new PropagatingErrorHandler(); |
||||
|
||||
|
||||
void handleError(Throwable t); |
||||
|
||||
} |
||||
@ -1,40 +0,0 @@
@@ -1,40 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.scheduling.support; |
||||
|
||||
import org.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
|
||||
/** |
||||
* An {@link ErrorHandler} implementation that logs the Throwable at error |
||||
* level. It does not perform any additional error handling. This can be |
||||
* useful when suppression of errors is the intended behavior. |
||||
* |
||||
* @author Mark Fisher |
||||
* @since 3.0 |
||||
*/ |
||||
class LoggingErrorHandler implements ErrorHandler { |
||||
|
||||
private final Log logger = LogFactory.getLog(LoggingErrorHandler.class); |
||||
|
||||
public void handleError(Throwable t) { |
||||
if (logger.isErrorEnabled()) { |
||||
logger.error("Unexpected error occurred in scheduled task.", t); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,104 @@
@@ -0,0 +1,104 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.scheduling.support; |
||||
|
||||
import java.util.concurrent.Future; |
||||
|
||||
import org.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
|
||||
import org.springframework.util.ErrorHandler; |
||||
import org.springframework.util.ReflectionUtils; |
||||
|
||||
/** |
||||
* Utility methods for decorating tasks with error handling. |
||||
* |
||||
* @author Mark Fisher |
||||
* @since 3.0 |
||||
*/ |
||||
public abstract class TaskUtils { |
||||
|
||||
/** |
||||
* An ErrorHandler strategy that will log the Exception but perform |
||||
* no further handling. This will suppress the error so that |
||||
* subsequent executions of the task will not be prevented. |
||||
*/ |
||||
public static final ErrorHandler LOG_AND_SUPPRESS_ERROR_HANDLER = new LoggingErrorHandler(); |
||||
|
||||
/** |
||||
* An ErrorHandler strategy that will log at error level and then |
||||
* re-throw the Exception. Note: this will typically prevent subsequent |
||||
* execution of a scheduled task. |
||||
*/ |
||||
public static final ErrorHandler LOG_AND_PROPAGATE_ERROR_HANDLER = new PropagatingErrorHandler(); |
||||
|
||||
|
||||
/** |
||||
* Decorates the task for error handling. If the provided |
||||
* {@link ErrorHandler} is not null, it will be used. Otherwise, |
||||
* repeating tasks will have errors suppressed by default whereas |
||||
* one-shot tasks will have errors propagated by default since those |
||||
* errors may be expected through the returned {@link Future}. In both |
||||
* cases, the errors will be logged. |
||||
*/ |
||||
public static DelegatingErrorHandlingRunnable decorateTaskWithErrorHandler( |
||||
Runnable task, ErrorHandler errorHandler, boolean isRepeatingTask) { |
||||
|
||||
if (task instanceof DelegatingErrorHandlingRunnable) { |
||||
return (DelegatingErrorHandlingRunnable) task; |
||||
} |
||||
ErrorHandler eh = errorHandler != null ? errorHandler : getDefaultErrorHandler(isRepeatingTask); |
||||
return new DelegatingErrorHandlingRunnable(task, eh); |
||||
} |
||||
|
||||
public static ErrorHandler getDefaultErrorHandler(boolean isRepeatingTask) { |
||||
return (isRepeatingTask ? LOG_AND_SUPPRESS_ERROR_HANDLER : LOG_AND_PROPAGATE_ERROR_HANDLER); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* An {@link ErrorHandler} implementation that logs the Throwable at error |
||||
* level. It does not perform any additional error handling. This can be |
||||
* useful when suppression of errors is the intended behavior. |
||||
*/ |
||||
static class LoggingErrorHandler implements ErrorHandler { |
||||
|
||||
private final Log logger = LogFactory.getLog(LoggingErrorHandler.class); |
||||
|
||||
public void handleError(Throwable t) { |
||||
if (logger.isErrorEnabled()) { |
||||
logger.error("Unexpected error occurred in scheduled task.", t); |
||||
} |
||||
} |
||||
|
||||
} |
||||
|
||||
|
||||
/** |
||||
* An {@link ErrorHandler} implementation that logs the Throwable at error |
||||
* level and then propagates it. |
||||
*/ |
||||
static class PropagatingErrorHandler extends LoggingErrorHandler { |
||||
|
||||
public void handleError(Throwable t) { |
||||
super.handleError(t); |
||||
ReflectionUtils.rethrowRuntimeException(t); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue