use of org.springframework.integration.transaction.TransactionSynchronizationFactory in project spring-integration by spring-projects.
the class PseudoTransactionalMessageSourceTests method testTransactionSynchronizationFactoryBean.
@Test
public void testTransactionSynchronizationFactoryBean() {
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(TestTxSyncConfiguration.class);
TransactionSynchronizationFactory syncFactory = ctx.getBean(TransactionSynchronizationFactory.class);
PollableChannel queueChannel = ctx.getBean("outputChannel", PollableChannel.class);
SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter();
adapter.setTransactionSynchronizationFactory(syncFactory);
QueueChannel outputChannel = new QueueChannel();
adapter.setOutputChannel(outputChannel);
adapter.setSource(new MessageSource<String>() {
@Override
public Message<String> receive() {
GenericMessage<String> message = new GenericMessage<String>("foo");
IntegrationResourceHolder holder = (IntegrationResourceHolder) TransactionSynchronizationManager.getResource(this);
holder.addAttribute("baz", "qux");
holder.addAttribute("bix", "qox");
return message;
}
});
TransactionSynchronizationManager.initSynchronization();
TransactionSynchronizationManager.setActualTransactionActive(true);
doPoll(adapter);
TransactionSynchronizationUtils.triggerBeforeCommit(false);
TransactionSynchronizationUtils.triggerAfterCommit();
Message<?> beforeCommitMessage = queueChannel.receive(1000);
assertNotNull(beforeCommitMessage);
assertEquals("qox", beforeCommitMessage.getPayload());
Message<?> afterCommitMessage = queueChannel.receive(1000);
assertNotNull(afterCommitMessage);
assertEquals("qux", afterCommitMessage.getPayload());
TransactionSynchronizationUtils.triggerAfterCompletion(TransactionSynchronization.STATUS_COMMITTED);
TransactionSynchronizationManager.clearSynchronization();
TransactionSynchronizationManager.setActualTransactionActive(false);
ctx.close();
}
use of org.springframework.integration.transaction.TransactionSynchronizationFactory in project spring-integration by spring-projects.
the class PseudoTransactionalMessageSourceTests method testInt2777CustomTransactionSynchronizationFactoryWithoutDealWithIntegrationResourceHolder.
@Test
public void testInt2777CustomTransactionSynchronizationFactoryWithoutDealWithIntegrationResourceHolder() {
SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter();
final AtomicInteger txSyncCounter = new AtomicInteger();
TransactionSynchronizationFactory syncFactory = new TransactionSynchronizationFactory() {
@Override
public TransactionSynchronization create(Object key) {
return new TransactionSynchronizationAdapter() {
@Override
public void afterCompletion(int status) {
txSyncCounter.incrementAndGet();
}
};
}
};
adapter.setTransactionSynchronizationFactory(syncFactory);
adapter.setSource(() -> null);
TransactionSynchronizationManager.initSynchronization();
TransactionSynchronizationManager.setActualTransactionActive(true);
doPoll(adapter);
TransactionSynchronizationUtils.triggerAfterCompletion(TransactionSynchronization.STATUS_COMMITTED);
TransactionSynchronizationManager.clearSynchronization();
TransactionSynchronizationManager.setActualTransactionActive(false);
assertEquals(1, txSyncCounter.get());
TransactionSynchronizationManager.initSynchronization();
TransactionSynchronizationManager.setActualTransactionActive(true);
doPoll(adapter);
TransactionSynchronizationUtils.triggerAfterCompletion(TransactionSynchronization.STATUS_COMMITTED);
TransactionSynchronizationManager.clearSynchronization();
TransactionSynchronizationManager.setActualTransactionActive(false);
assertEquals(2, txSyncCounter.get());
}
Aggregations