BeanPostProcessor for proxying TaskExecutor instances to allow delegation to an error handler when async execution throws errors.

Relates to INT-440
This commit is contained in:
Jonas Partner
2008-10-29 14:36:40 +00:00
parent 27604d3707
commit df5c72f653
6 changed files with 343 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2002-2008 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.integration.executor;
import java.util.ArrayList;
import java.util.List;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.integration.util.ErrorHandler;
/**
*
* @author Jonas Partner
*
*/
public class ErrorHandlingExecutorBeanPostProcessor implements BeanPostProcessor {
private final RunnableProxyingMethodInterceptor runnableProxyingInterceptor;
private volatile List<String> beanNamesToExclude = new ArrayList<String>();
public ErrorHandlingExecutorBeanPostProcessor(ErrorHandler errorHandler) {
this.runnableProxyingInterceptor = new RunnableProxyingMethodInterceptor(errorHandler);
}
public void setTaskExecutorBeanNameExclusions(List<String> beanNamesToExclude) {
this.beanNamesToExclude = beanNamesToExclude;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof TaskExecutor && !beanNamesToExclude.contains(beanName)) {
ProxyFactory proxyFactory = new ProxyFactory(bean);
proxyFactory.addAdvisor(new DefaultPointcutAdvisor(this.runnableProxyingInterceptor));
return proxyFactory.getProxy();
}
return bean;
}
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}

View File

@@ -0,0 +1,69 @@
/*
* Copyright 2002-2008 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.integration.executor;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.core.task.TaskExecutor;
import org.springframework.integration.util.ErrorHandler;
/**
* Used tby {@link ErrorHandlingExecutorBeanPostProcessor} to Proxy
* {@link TaskExecutor} instances. This allow the wrapping of {@link Runnable}
* instances in order to handle catch errors thrown and delegate handling to an
* instance of {@link ErrorHandler}
* @author Jonas Partner
*
*/
public class RunnableProxyingMethodInterceptor implements MethodInterceptor {
private final ErrorHandler errorHandler;
public RunnableProxyingMethodInterceptor(ErrorHandler errorHandler) {
this.errorHandler = errorHandler;
}
public Object invoke(MethodInvocation invocation) throws Throwable {
Runnable runnable = (Runnable) invocation.getArguments()[0];
ProxyFactory factory = new ProxyFactory(runnable);
factory.addAdvice(new ErrorHandlingAdvice(errorHandler));
invocation.getArguments()[0] = factory.getProxy();
return invocation.proceed();
}
private static class ErrorHandlingAdvice implements MethodInterceptor {
private final ErrorHandler errorHandler;
public ErrorHandlingAdvice(ErrorHandler errorHandler) {
this.errorHandler = errorHandler;
}
public Object invoke(MethodInvocation invocation) throws Throwable {
try {
invocation.proceed();
}
catch (Throwable t) {
errorHandler.handle(t);
}
return null;
}
}
}