diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/handler/AbstractMessageHandlerAdapter.java b/org.springframework.integration/src/main/java/org/springframework/integration/handler/AbstractMessageHandlerAdapter.java index fe9c8062bf..0af04a54f6 100644 --- a/org.springframework.integration/src/main/java/org/springframework/integration/handler/AbstractMessageHandlerAdapter.java +++ b/org.springframework.integration/src/main/java/org/springframework/integration/handler/AbstractMessageHandlerAdapter.java @@ -104,11 +104,11 @@ public abstract class AbstractMessageHandlerAdapter extends AbstractMethodInvoki } catch (InvocationTargetException e) { throw new MessageHandlingException(message, "Handler method '" - + this.getMethod() + "' threw an Exception.", e.getTargetException()); + + this.getMethodName() + "' threw an Exception.", e.getTargetException()); } catch (Throwable e) { throw new MessageHandlingException(message, "Failed to invoke handler method '" - + this.getMethod() + "' with arguments: " + ObjectUtils.nullSafeToString(args), e); + + this.getMethodName() + "' with arguments: " + ObjectUtils.nullSafeToString(args), e); } } diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/util/AbstractMethodInvokingAdapter.java b/org.springframework.integration/src/main/java/org/springframework/integration/util/AbstractMethodInvokingAdapter.java index 8b9a7314d8..85fd4ad48d 100644 --- a/org.springframework.integration/src/main/java/org/springframework/integration/util/AbstractMethodInvokingAdapter.java +++ b/org.springframework.integration/src/main/java/org/springframework/integration/util/AbstractMethodInvokingAdapter.java @@ -74,6 +74,10 @@ public abstract class AbstractMethodInvokingAdapter implements MethodInvoker, In this.methodName = methodName; } + protected String getMethodName() { + return this.methodName; + } + public void setOrder(int order) { this.order = order; } @@ -102,6 +106,7 @@ public abstract class AbstractMethodInvokingAdapter implements MethodInvoker, In throw new ConfigurationException("An ambiguity exists between the 'method' and 'methodName' properties. " + "Note that only one of them is required, but if both are provided they must match."); } + this.methodName = this.method.getName(); this.invoker = new DefaultMethodInvoker(this.object, this.method); } else { diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/endpoint/interceptor/TestBean.java b/org.springframework.integration/src/test/java/org/springframework/integration/endpoint/interceptor/TestBean.java new file mode 100644 index 0000000000..25b702a5f6 --- /dev/null +++ b/org.springframework.integration/src/test/java/org/springframework/integration/endpoint/interceptor/TestBean.java @@ -0,0 +1,32 @@ +/* + * 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.endpoint.interceptor; + +/** + * @author Mark Fisher + */ +public class TestBean { + + public String good(String s) { + return s; + } + + public String bad(String s) { + throw new RuntimeException("intentional test failure"); + } + +} diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/endpoint/interceptor/TestTransactionManager.java b/org.springframework.integration/src/test/java/org/springframework/integration/endpoint/interceptor/TestTransactionManager.java new file mode 100644 index 0000000000..4032773161 --- /dev/null +++ b/org.springframework.integration/src/test/java/org/springframework/integration/endpoint/interceptor/TestTransactionManager.java @@ -0,0 +1,78 @@ +/* + * 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.endpoint.interceptor; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import org.springframework.transaction.TransactionDefinition; +import org.springframework.transaction.TransactionException; +import org.springframework.transaction.support.AbstractPlatformTransactionManager; +import org.springframework.transaction.support.DefaultTransactionDefinition; +import org.springframework.transaction.support.DefaultTransactionStatus; + +/** + * @author Mark Fisher + */ +@SuppressWarnings("serial") +public class TestTransactionManager extends AbstractPlatformTransactionManager { + + private final AtomicInteger commitCounter = new AtomicInteger(); + + private final AtomicInteger rollbackCounter = new AtomicInteger(); + + private final CountDownLatch latch = new CountDownLatch(1); + + + public int getCommitCount() { + return this.commitCounter.get(); + } + + public int getRollbackCount() { + return this.rollbackCounter.get(); + } + + public void waitForCompletion(long timeout) throws InterruptedException { + this.latch.await(timeout, TimeUnit.MILLISECONDS); + } + + @Override + protected void doBegin(Object transaction, TransactionDefinition definition) throws TransactionException { + } + + @Override + protected void doCommit(DefaultTransactionStatus status) throws TransactionException { + this.commitCounter.incrementAndGet(); + } + + @Override + protected Object doGetTransaction() throws TransactionException { + return new DefaultTransactionDefinition(); + } + + @Override + protected void doRollback(DefaultTransactionStatus status) throws TransactionException { + this.rollbackCounter.incrementAndGet(); + } + + @Override + protected void doCleanupAfterCompletion(Object o) { + this.latch.countDown(); + } + +} diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/endpoint/interceptor/TransactionInterceptorTests.java b/org.springframework.integration/src/test/java/org/springframework/integration/endpoint/interceptor/TransactionInterceptorTests.java new file mode 100644 index 0000000000..08dbdb3254 --- /dev/null +++ b/org.springframework.integration/src/test/java/org/springframework/integration/endpoint/interceptor/TransactionInterceptorTests.java @@ -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.endpoint.interceptor; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import org.junit.Test; + +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.integration.channel.MessageChannel; +import org.springframework.integration.message.Message; +import org.springframework.integration.message.StringMessage; + +/** + * @author Mark Fisher + */ +public class TransactionInterceptorTests { + + @Test + public void testTransactionInterceptorWithCommit() throws InterruptedException { + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( + "transactionInterceptorTests.xml", this.getClass()); + TestTransactionManager txManager = (TestTransactionManager) context.getBean("txManager"); + MessageChannel input = (MessageChannel) context.getBean("goodInput"); + MessageChannel output = (MessageChannel) context.getBean("output"); + assertEquals(0, txManager.getCommitCount()); + assertEquals(0, txManager.getRollbackCount()); + input.send(new StringMessage("test")); + txManager.waitForCompletion(1000); + Message message = output.receive(0); + assertNotNull(message); + assertEquals(1, txManager.getCommitCount()); + assertEquals(0, txManager.getRollbackCount()); + } + + @Test + public void testTransactionInterceptorWithRollback() throws InterruptedException { + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( + "transactionInterceptorTests.xml", this.getClass()); + TestTransactionManager txManager = (TestTransactionManager) context.getBean("txManager"); + MessageChannel input = (MessageChannel) context.getBean("badInput"); + MessageChannel output = (MessageChannel) context.getBean("output"); + assertEquals(0, txManager.getCommitCount()); + assertEquals(0, txManager.getRollbackCount()); + input.send(new StringMessage("test")); + txManager.waitForCompletion(1000); + Message message = output.receive(0); + assertNull(message); + assertEquals(0, txManager.getCommitCount()); + assertEquals(1, txManager.getRollbackCount()); + } + +} diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/endpoint/interceptor/transactionInterceptorTests.xml b/org.springframework.integration/src/test/java/org/springframework/integration/endpoint/interceptor/transactionInterceptorTests.xml new file mode 100644 index 0000000000..fb858732e3 --- /dev/null +++ b/org.springframework.integration/src/test/java/org/springframework/integration/endpoint/interceptor/transactionInterceptorTests.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + +