Search in sources :

Example 1 with TransactionSynchronizationFactory

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();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) GenericMessage(org.springframework.messaging.support.GenericMessage) IntegrationResourceHolder(org.springframework.integration.transaction.IntegrationResourceHolder) PollableChannel(org.springframework.messaging.PollableChannel) DefaultTransactionSynchronizationFactory(org.springframework.integration.transaction.DefaultTransactionSynchronizationFactory) TransactionSynchronizationFactory(org.springframework.integration.transaction.TransactionSynchronizationFactory) Test(org.junit.Test)

Example 2 with TransactionSynchronizationFactory

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DefaultTransactionSynchronizationFactory(org.springframework.integration.transaction.DefaultTransactionSynchronizationFactory) TransactionSynchronizationFactory(org.springframework.integration.transaction.TransactionSynchronizationFactory) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 DefaultTransactionSynchronizationFactory (org.springframework.integration.transaction.DefaultTransactionSynchronizationFactory)2 TransactionSynchronizationFactory (org.springframework.integration.transaction.TransactionSynchronizationFactory)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1 QueueChannel (org.springframework.integration.channel.QueueChannel)1 IntegrationResourceHolder (org.springframework.integration.transaction.IntegrationResourceHolder)1 Message (org.springframework.messaging.Message)1 PollableChannel (org.springframework.messaging.PollableChannel)1 GenericMessage (org.springframework.messaging.support.GenericMessage)1 TransactionSynchronizationAdapter (org.springframework.transaction.support.TransactionSynchronizationAdapter)1