The @Poller annotation now supports transaction settings and the 'adviceChain' property when configured with a @ChannelAdapter annotation on an "inbound" pollable method to create a SourcePollingChannelAdapter instance (INT-408).

This commit is contained in:
Mark Fisher
2008-10-09 20:38:23 +00:00
parent 3070a63e39
commit b7e4aee3ae
11 changed files with 393 additions and 4 deletions

View File

@@ -0,0 +1,38 @@
/*
* 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.config;
import java.util.concurrent.TimeUnit;
import org.springframework.integration.annotation.ChannelAdapter;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.Poller;
/**
* @author Mark Fisher
*/
@MessageEndpoint
public class PollerAnnotationChannelAdapterAdviceChainTestBean {
@ChannelAdapter("output")
@Poller(interval=5, timeUnit=TimeUnit.SECONDS, maxMessagesPerPoll=1,
adviceChain="aroundAdvice,beforeAdvice,afterAdvice")
public String testMethod() {
return "test";
}
}

View File

@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:stream="http://www.springframework.org/schema/integration/stream"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd
http://www.springframework.org/schema/integration/stream
http://www.springframework.org/schema/integration/stream/spring-integration-stream-1.0.xsd">
<message-bus enable-annotations="true"/>
<channel id="output">
<queue capacity="1"/>
</channel>
<beans:bean id="channelAdapter" class="org.springframework.integration.config.PollerAnnotationChannelAdapterAdviceChainTestBean"/>
<beans:bean id="latch" class="java.util.concurrent.CountDownLatch">
<beans:constructor-arg value="4"/>
</beans:bean>
<beans:bean id="beforeAdvice" class="org.springframework.integration.config.TestBeforeAdvice">
<beans:constructor-arg ref="latch"/>
</beans:bean>
<beans:bean id="afterAdvice" class="org.springframework.integration.config.TestAfterAdvice">
<beans:constructor-arg ref="latch"/>
</beans:bean>
<beans:bean id="aroundAdvice" class="org.springframework.integration.config.TestAroundAdvice">
<beans:constructor-arg ref="latch"/>
</beans:bean>
</beans:beans>

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.config;
import static org.junit.Assert.assertEquals;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.integration.channel.PollableChannel;
import org.springframework.integration.message.Message;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Fisher
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class PollerAnnotationChannelAdapterAdviceChainTests {
@Autowired @Qualifier("output")
private PollableChannel output;
@Autowired
private CountDownLatch latch;
@Autowired
private TestBeforeAdvice beforeAdvice;
@Autowired
private TestAfterAdvice afterAdvice;
@Autowired
private TestAroundAdvice aroundAdvice;
@Test
public void testAdviceChain() throws InterruptedException {
Message<?> reply = output.receive(1000);
assertEquals("test", reply.getPayload());
latch.await(1, TimeUnit.SECONDS);
assertEquals(0, latch.getCount());
assertEquals(4, aroundAdvice.getPreCount());
assertEquals(3, beforeAdvice.getLatchCount());
assertEquals(2, afterAdvice.getLatchCount());
assertEquals(1, aroundAdvice.getPostCount());
}
}

View File

@@ -0,0 +1,68 @@
/*
* 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.config;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import org.springframework.integration.annotation.ChannelAdapter;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.Poller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
/**
* @author Mark Fisher
*/
@MessageEndpoint
public class PollerAnnotationChannelAdapterTransactionalTestBean {
private volatile boolean shouldFail;
private BlockingQueue<String> queue = new ArrayBlockingQueue<String>(1);
public void setShouldFail(boolean shouldFail) {
this.shouldFail = shouldFail;
}
public void setNextValue(String nextValue) {
try {
this.queue.put(nextValue);
}
catch (InterruptedException e) {
}
}
@ChannelAdapter("output")
@Poller(interval=100, maxMessagesPerPoll=1,
transactionManager="testTransactionManager",
transactionAttributes=@Transactional(propagation=Propagation.REQUIRES_NEW))
public String testMethod() {
String result = null;
try {
result = this.queue.take();
}
catch (InterruptedException e) {
}
if (this.shouldFail) {
throw new IllegalArgumentException("intentional test failure");
}
return result;
}
}

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:stream="http://www.springframework.org/schema/integration/stream"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd
http://www.springframework.org/schema/integration/stream
http://www.springframework.org/schema/integration/stream/spring-integration-stream-1.0.xsd">
<message-bus enable-annotations="true"/>
<channel id="output">
<queue capacity="1"/>
</channel>
<beans:bean id="channelAdapter" class="org.springframework.integration.config.PollerAnnotationChannelAdapterTransactionalTestBean"/>
<beans:bean id="testTransactionManager" class="org.springframework.integration.util.TestTransactionManager"/>
</beans:beans>

View File

@@ -0,0 +1,87 @@
/*
* 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.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.integration.channel.PollableChannel;
import org.springframework.integration.message.Message;
import org.springframework.integration.util.TestTransactionManager;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.TransactionDefinition;
/**
* @author Mark Fisher
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class PollerAnnotationChannelAdapterTransactionalTests {
@Autowired @Qualifier("output")
private PollableChannel output;
@Autowired
private PollerAnnotationChannelAdapterTransactionalTestBean adapter;
@Autowired
private TestTransactionManager transactionManager;
@Before
public void resetTransactionManager() {
transactionManager.reset();
}
@Test
public void commit() throws InterruptedException {
adapter.setShouldFail(false);
adapter.setNextValue("commit-test");
transactionManager.waitForCompletion(1000);
Message<?> reply = output.receive(1000);
assertEquals("commit-test", reply.getPayload());
assertEquals(1, transactionManager.getCommitCount());
assertEquals(0, transactionManager.getRollbackCount());
}
@Test
public void rollback() throws InterruptedException {
adapter.setShouldFail(true);
adapter.setNextValue("rollback-test");
transactionManager.waitForCompletion(1000);
assertNull(output.receive(0));
assertEquals(0, transactionManager.getCommitCount());
assertEquals(1, transactionManager.getRollbackCount());
}
@Test
public void verifyPropagationSetting() throws InterruptedException {
adapter.setNextValue("propagation-test");
transactionManager.waitForCompletion(1000);
assertEquals(TransactionDefinition.PROPAGATION_REQUIRES_NEW,
transactionManager.getLastDefinition().getPropagationBehavior());
}
}

View File

@@ -26,7 +26,7 @@ import org.springframework.integration.annotation.ServiceActivator;
* @author Mark Fisher
*/
@MessageEndpoint
public class PollerAnnotationAdviceChainTestBean {
public class PollerAnnotationConsumerAdviceChainTestBean {
@ServiceActivator(inputChannel="input", outputChannel="output")
@Poller(interval=5, timeUnit=TimeUnit.SECONDS, maxMessagesPerPoll=1,

View File

@@ -20,7 +20,7 @@
<queue capacity="1"/>
</channel>
<beans:bean id="service" class="org.springframework.integration.config.PollerAnnotationAdviceChainTestBean"/>
<beans:bean id="service" class="org.springframework.integration.config.PollerAnnotationConsumerAdviceChainTestBean"/>
<beans:bean id="latch" class="java.util.concurrent.CountDownLatch">
<beans:constructor-arg value="4"/>

View File

@@ -26,7 +26,7 @@ import org.springframework.transaction.annotation.Transactional;
* @author Mark Fisher
*/
@MessageEndpoint
public class PollerAnnotationTransactionalTestBean {
public class PollerAnnotationConsumerTransactionalTestBean {
@ServiceActivator(inputChannel="input", outputChannel="output")
@Poller(interval=100, maxMessagesPerPoll=1,

View File

@@ -20,7 +20,7 @@
<queue capacity="1"/>
</channel>
<beans:bean id="service" class="org.springframework.integration.config.PollerAnnotationTransactionalTestBean"/>
<beans:bean id="service" class="org.springframework.integration.config.PollerAnnotationConsumerTransactionalTestBean"/>
<beans:bean id="testTransactionManager" class="org.springframework.integration.util.TestTransactionManager"/>