INT-1849/INT-2606 Pseudo Transactional Message Src
Initial commit. Tested with POP3 and IMAP (James) with Sample app. Essentially moved all the flagging and deleting code from receive() to closeContextAfterSuccess(). For non-transactional cases, this new method is called immediately after receiving the message(s), essentially working as before. When run from a <transactional/> poller it is called using TransactionSynchronization after the transaction commits. This behavior can be changed by setting 'symchronized="false"' on the poller, which removes the synchronization and the update is called immediately after the receive(). Polishing PR Comments Update Reference
This commit is contained in:
committed by
Oleg Zhurakousky
parent
4e9a393983
commit
9bc9867d31
@@ -18,16 +18,14 @@ package org.springframework.integration.config.xml;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.aopalliance.aop.Advice;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
@@ -95,6 +93,7 @@ public class PollerParserTests {
|
||||
assertEquals(TransactionInterceptor.class, txAdvice.getClass());
|
||||
TransactionAttributeSource transactionAttributeSource = ((TransactionInterceptor) txAdvice).getTransactionAttributeSource();
|
||||
assertEquals(NameMatchTransactionAttributeSource.class, transactionAttributeSource.getClass());
|
||||
@SuppressWarnings("rawtypes")
|
||||
HashMap nameMap = TestUtils.getPropertyValue(transactionAttributeSource, "nameMap", HashMap.class);
|
||||
assertEquals(1, nameMap.size());
|
||||
assertEquals("{*=PROPAGATION_REQUIRES_NEW,ISOLATION_DEFAULT,readOnly}", nameMap.toString());
|
||||
@@ -122,13 +121,13 @@ public class PollerParserTests {
|
||||
PollerMetadata metadata = (PollerMetadata) poller;
|
||||
assertTrue(metadata.getTrigger() instanceof TestTrigger);
|
||||
}
|
||||
|
||||
|
||||
@Test(expected=BeanDefinitionParsingException.class)
|
||||
public void pollerWithCronTriggerAndTimeUnit() {
|
||||
new ClassPathXmlApplicationContext(
|
||||
"cronTriggerWithTimeUnit-fail.xml", PollerParserTests.class);
|
||||
}
|
||||
|
||||
|
||||
@Test(expected=BeanDefinitionParsingException.class)
|
||||
public void topLevelPollerWithRef() {
|
||||
new ClassPathXmlApplicationContext(
|
||||
@@ -141,4 +140,24 @@ public class PollerParserTests {
|
||||
"pollerWithCronAndFixedDelay.xml", PollerParserTests.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pollerWithSync() {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"pollerWithSynchronization.xml", PollerParserTests.class);
|
||||
Object poller = context.getBean("noSync");
|
||||
assertNotNull(poller);
|
||||
PollerMetadata metadata = (PollerMetadata) poller;
|
||||
assertEquals(true, metadata.isSynchronized());
|
||||
|
||||
poller = context.getBean("syncTrue");
|
||||
assertNotNull(poller);
|
||||
metadata = (PollerMetadata) poller;
|
||||
assertEquals(true, metadata.isSynchronized());
|
||||
|
||||
poller = context.getBean("syncFalse");
|
||||
assertNotNull(poller);
|
||||
metadata = (PollerMetadata) poller;
|
||||
assertEquals(false, metadata.isSynchronized());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
<?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"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration.xsd">
|
||||
|
||||
<poller id="noSync" fixed-delay="3000"/>
|
||||
|
||||
<poller id="syncTrue" fixed-delay="3000" synchronized="true" />
|
||||
|
||||
<poller id="syncFalse" fixed-delay="3000" synchronized="false" />
|
||||
|
||||
</beans:beans>
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Copyright 2002-2012 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;
|
||||
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.core.PseudoTransactionalMessageSource;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.transaction.support.TransactionSynchronization;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationUtils;
|
||||
|
||||
/**
|
||||
* @author Gary Russell
|
||||
* @since 2.2
|
||||
*
|
||||
*/
|
||||
public class PseudoTransactionalMessageSourceTests {
|
||||
|
||||
@Test
|
||||
public void testCommit() {
|
||||
SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter();
|
||||
QueueChannel outputChannel = new QueueChannel();
|
||||
adapter.setOutputChannel(outputChannel);
|
||||
final Object object = new Object();
|
||||
final AtomicReference<Object> committed = new AtomicReference<Object>();
|
||||
final AtomicReference<Object> rolledBack = new AtomicReference<Object>();
|
||||
adapter.setSource(new PseudoTransactionalMessageSource<String>() {
|
||||
|
||||
public Message<String> receive() {
|
||||
return new GenericMessage<String>("foo");
|
||||
}
|
||||
|
||||
public Object getResource() {
|
||||
return object;
|
||||
}
|
||||
|
||||
public void afterCommit(Object resource) {
|
||||
committed.set(resource);
|
||||
}
|
||||
|
||||
public void afterRollback(Object resource) {
|
||||
rolledBack.set(resource);
|
||||
}
|
||||
});
|
||||
|
||||
TransactionSynchronizationManager.initSynchronization();
|
||||
TransactionSynchronizationManager.setActualTransactionActive(true);
|
||||
adapter.doPoll();
|
||||
TransactionSynchronizationUtils.triggerAfterCommit();
|
||||
assertSame(object, committed.get());
|
||||
TransactionSynchronizationUtils.triggerAfterCompletion(TransactionSynchronization.STATUS_COMMITTED);
|
||||
TransactionSynchronizationManager.clearSynchronization();
|
||||
assertNull(rolledBack.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRollback() {
|
||||
SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter();
|
||||
QueueChannel outputChannel = new QueueChannel();
|
||||
adapter.setOutputChannel(outputChannel);
|
||||
final Object object = new Object();
|
||||
final AtomicReference<Object> committed = new AtomicReference<Object>();
|
||||
final AtomicReference<Object> rolledBack = new AtomicReference<Object>();
|
||||
adapter.setSource(new PseudoTransactionalMessageSource<String>() {
|
||||
|
||||
public Message<String> receive() {
|
||||
return new GenericMessage<String>("foo");
|
||||
}
|
||||
|
||||
public Object getResource() {
|
||||
return object;
|
||||
}
|
||||
|
||||
public void afterCommit(Object resource) {
|
||||
committed.set(resource);
|
||||
}
|
||||
|
||||
public void afterRollback(Object resource) {
|
||||
rolledBack.set(resource);
|
||||
}
|
||||
});
|
||||
|
||||
TransactionSynchronizationManager.initSynchronization();
|
||||
TransactionSynchronizationManager.setActualTransactionActive(true);
|
||||
adapter.doPoll();
|
||||
TransactionSynchronizationUtils.triggerAfterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
|
||||
assertSame(object, rolledBack.get());
|
||||
TransactionSynchronizationManager.clearSynchronization();
|
||||
assertNull(committed.get());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user