remove unused proxy
This commit is contained in:
@@ -1,61 +0,0 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,6 +18,11 @@ package org.springframework.integration.executor;
|
||||
import org.springframework.core.task.TaskExecutor;
|
||||
import org.springframework.integration.util.ErrorHandler;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*
|
||||
*/
|
||||
public class ErrorHandlingTaskExecutor implements TaskExecutor {
|
||||
|
||||
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
/*
|
||||
* 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.core.task.TaskExecutor;
|
||||
import org.springframework.integration.util.ErrorHandler;
|
||||
|
||||
/**
|
||||
* Used by {@link ErrorHandlingExecutorBeanPostProcessor} to Proxy
|
||||
* {@link TaskExecutor} instances. This allow the wrapping of {@link Runnable}
|
||||
* instances in order to catch any 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];
|
||||
invocation.getArguments()[0] = new ErrorHandlingRunnableWrapper(runnable, errorHandler);
|
||||
return invocation.proceed();
|
||||
}
|
||||
|
||||
|
||||
private static class ErrorHandlingRunnableWrapper implements Runnable {
|
||||
|
||||
private final ErrorHandler errorHandler;
|
||||
|
||||
private final Runnable runnableTarget;
|
||||
|
||||
public ErrorHandlingRunnableWrapper(Runnable runnableTarget, ErrorHandler errorHandler) {
|
||||
this.runnableTarget = runnableTarget;
|
||||
this.errorHandler = errorHandler;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
runnableTarget.run();
|
||||
}
|
||||
catch (Throwable t) {
|
||||
errorHandler.handle(t);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
|
||||
|
||||
<bean id="proxiedTaskExecutor" class="org.springframework.core.task.SimpleAsyncTaskExecutor" />
|
||||
|
||||
<bean id="excludeFromProxyingTaskExecutor" class="org.springframework.core.task.SimpleAsyncTaskExecutor" />
|
||||
|
||||
<bean
|
||||
class="org.springframework.integration.executor.ErrorHandlingExecutorBeanPostProcessor">
|
||||
<constructor-arg ref="stubErrorHandler"/>
|
||||
<property name="taskExecutorBeanNameExclusions">
|
||||
<list>
|
||||
<value>excludeFromProxyingTaskExecutor</value>
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="stubErrorHandler" class="org.springframework.integration.executor.StubErrorHandler"/>
|
||||
|
||||
</beans>
|
||||
@@ -1,73 +0,0 @@
|
||||
/*
|
||||
* 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 static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.aop.support.AopUtils;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.core.task.TaskExecutor;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*
|
||||
*/
|
||||
public class ErrorHandlingExecutorBeanPostProcessorTests {
|
||||
|
||||
StubErrorHandler errorHandler;
|
||||
|
||||
ApplicationContext applicationContext;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
applicationContext = new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-context.xml", getClass());
|
||||
errorHandler = (StubErrorHandler) applicationContext.getBean("stubErrorHandler");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProxiedTaskExecutor() throws Exception {
|
||||
errorHandler.latch = new CountDownLatch(1);
|
||||
TaskExecutor taskExecutor = (TaskExecutor) applicationContext.getBean("proxiedTaskExecutor");
|
||||
assertTrue("Task executor was not proxied ", AopUtils.isAopProxy(taskExecutor));
|
||||
ErrorThrowingRunnable runnable = new ErrorThrowingRunnable();
|
||||
taskExecutor.execute(runnable);
|
||||
assertTrue("Runnable faield to run", errorHandler.latch.await(5, TimeUnit.SECONDS));
|
||||
assertEquals("Incorrect count of exceptions", 1, errorHandler.throwables.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExcludedFromProxyingTaskExecutor() throws Exception {
|
||||
TaskExecutor taskExecutor = (TaskExecutor) applicationContext.getBean("excludeFromProxyingTaskExecutor");
|
||||
assertFalse("Excluded task executor was proxied ", AopUtils.isAopProxy(taskExecutor));
|
||||
}
|
||||
|
||||
private static class ErrorThrowingRunnable implements Runnable {
|
||||
|
||||
public void run() {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
/*
|
||||
* 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 static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.core.task.SimpleAsyncTaskExecutor;
|
||||
import org.springframework.core.task.TaskExecutor;
|
||||
import org.springframework.integration.util.ErrorHandler;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*
|
||||
*/
|
||||
public class RunnableProxyingMethodInterceptorTests {
|
||||
|
||||
TaskExecutor proxiedExecutor;
|
||||
|
||||
StubErrorHandler errorHandler;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
errorHandler = new StubErrorHandler();
|
||||
ProxyFactory factory = new ProxyFactory(new SimpleAsyncTaskExecutor());
|
||||
factory.addAdvice(new RunnableProxyingMethodInterceptor(errorHandler));
|
||||
proxiedExecutor = (TaskExecutor) factory.getProxy();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRuntimeThrown() throws Exception {
|
||||
errorHandler.latch = new CountDownLatch(1);
|
||||
TestRunnable runnable = new TestRunnable();
|
||||
proxiedExecutor.execute(runnable);
|
||||
assertTrue("Runnable did not run", errorHandler.latch.await(5, TimeUnit.SECONDS));
|
||||
assertEquals("Wrong count of exceptions in ErrorHandler", 1, errorHandler.throwables.size());
|
||||
}
|
||||
|
||||
public static class StubErrorHandler implements ErrorHandler {
|
||||
|
||||
CountDownLatch latch;
|
||||
|
||||
List<Throwable> throwables = new ArrayList<Throwable>();
|
||||
|
||||
public void handle(Throwable t) {
|
||||
throwables.add(t);
|
||||
latch.countDown();
|
||||
}
|
||||
}
|
||||
|
||||
public static class TestRunnable implements Runnable {
|
||||
|
||||
public void run() {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
/*
|
||||
* 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 java.util.concurrent.CountDownLatch;
|
||||
|
||||
import org.springframework.integration.util.ErrorHandler;
|
||||
|
||||
public class StubErrorHandler implements ErrorHandler {
|
||||
|
||||
CountDownLatch latch;
|
||||
|
||||
List<Throwable> throwables = new ArrayList<Throwable>();
|
||||
|
||||
public void handle(Throwable t) {
|
||||
throwables.add(t);
|
||||
latch.countDown();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user