Search in sources :

Example 1 with ExpressionEvaluatingTransactionSynchronizationProcessor

use of org.springframework.integration.transaction.ExpressionEvaluatingTransactionSynchronizationProcessor in project spring-integration by spring-projects.

the class PseudoTransactionalMessageSourceTests method testCommit.

@Test
public void testCommit() {
    SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter();
    ExpressionEvaluatingTransactionSynchronizationProcessor syncProcessor = new ExpressionEvaluatingTransactionSynchronizationProcessor();
    syncProcessor.setBeanFactory(mock(BeanFactory.class));
    PollableChannel queueChannel = new QueueChannel();
    syncProcessor.setBeforeCommitExpression(new SpelExpressionParser().parseExpression("#bix"));
    syncProcessor.setBeforeCommitChannel(queueChannel);
    syncProcessor.setAfterCommitExpression(new SpelExpressionParser().parseExpression("#baz"));
    DefaultTransactionSynchronizationFactory syncFactory = new DefaultTransactionSynchronizationFactory(syncProcessor);
    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;
        }
    });
    MessageChannel afterCommitChannel = TestUtils.getPropertyValue(syncProcessor, "afterCommitChannel", MessageChannel.class);
    assertThat(afterCommitChannel, Matchers.instanceOf(NullChannel.class));
    Log logger = TestUtils.getPropertyValue(afterCommitChannel, "logger", Log.class);
    logger = Mockito.spy(logger);
    Mockito.when(logger.isDebugEnabled()).thenReturn(true);
    DirectFieldAccessor dfa = new DirectFieldAccessor(afterCommitChannel);
    dfa.setPropertyValue("logger", logger);
    TransactionSynchronizationManager.initSynchronization();
    TransactionSynchronizationManager.setActualTransactionActive(true);
    doPoll(adapter);
    TransactionSynchronizationUtils.triggerBeforeCommit(false);
    TransactionSynchronizationUtils.triggerAfterCommit();
    Message<?> beforeCommitMessage = queueChannel.receive(1000);
    assertNotNull(beforeCommitMessage);
    assertEquals("qox", beforeCommitMessage.getPayload());
    Mockito.verify(logger).debug(Mockito.anyString());
    TransactionSynchronizationUtils.triggerAfterCompletion(TransactionSynchronization.STATUS_COMMITTED);
    TransactionSynchronizationManager.clearSynchronization();
    TransactionSynchronizationManager.setActualTransactionActive(false);
}
Also used : ExpressionEvaluatingTransactionSynchronizationProcessor(org.springframework.integration.transaction.ExpressionEvaluatingTransactionSynchronizationProcessor) QueueChannel(org.springframework.integration.channel.QueueChannel) DefaultTransactionSynchronizationFactory(org.springframework.integration.transaction.DefaultTransactionSynchronizationFactory) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) Log(org.apache.commons.logging.Log) GenericMessage(org.springframework.messaging.support.GenericMessage) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) MessageChannel(org.springframework.messaging.MessageChannel) IntegrationResourceHolder(org.springframework.integration.transaction.IntegrationResourceHolder) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) BeanFactory(org.springframework.beans.factory.BeanFactory) PollableChannel(org.springframework.messaging.PollableChannel) NullChannel(org.springframework.integration.channel.NullChannel) Test(org.junit.Test)

Example 2 with ExpressionEvaluatingTransactionSynchronizationProcessor

use of org.springframework.integration.transaction.ExpressionEvaluatingTransactionSynchronizationProcessor in project spring-integration by spring-projects.

the class PseudoTransactionalMessageSourceTests method testRollbackWithManagerUsingStatus.

@Test
public void testRollbackWithManagerUsingStatus() {
    final PollableChannel queueChannel = new QueueChannel();
    TransactionTemplate transactionTemplate = new TransactionTemplate(new PseudoTransactionManager());
    transactionTemplate.execute(status -> {
        SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter();
        ExpressionEvaluatingTransactionSynchronizationProcessor syncProcessor = new ExpressionEvaluatingTransactionSynchronizationProcessor();
        syncProcessor.setBeanFactory(mock(BeanFactory.class));
        syncProcessor.setAfterRollbackChannel(queueChannel);
        syncProcessor.setAfterRollbackExpression(new SpelExpressionParser().parseExpression("#baz"));
        DefaultTransactionSynchronizationFactory syncFactory = new DefaultTransactionSynchronizationFactory(syncProcessor);
        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) TransactionSynchronizationManager.getResource(this)).addAttribute("baz", "qux");
                return message;
            }
        });
        doPoll(adapter);
        status.setRollbackOnly();
        return null;
    });
    Message<?> rollbackMessage = queueChannel.receive(1000);
    assertNotNull(rollbackMessage);
    assertEquals("qux", rollbackMessage.getPayload());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) ExpressionEvaluatingTransactionSynchronizationProcessor(org.springframework.integration.transaction.ExpressionEvaluatingTransactionSynchronizationProcessor) DefaultTransactionSynchronizationFactory(org.springframework.integration.transaction.DefaultTransactionSynchronizationFactory) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) PseudoTransactionManager(org.springframework.integration.transaction.PseudoTransactionManager) GenericMessage(org.springframework.messaging.support.GenericMessage) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) PollableChannel(org.springframework.messaging.PollableChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 3 with ExpressionEvaluatingTransactionSynchronizationProcessor

use of org.springframework.integration.transaction.ExpressionEvaluatingTransactionSynchronizationProcessor in project spring-integration by spring-projects.

the class PseudoTransactionalMessageSourceTests method testRollbackWithManager.

@Test
public void testRollbackWithManager() {
    final PollableChannel queueChannel = new QueueChannel();
    TransactionTemplate transactionTemplate = new TransactionTemplate(new PseudoTransactionManager());
    try {
        transactionTemplate.execute(status -> {
            SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter();
            ExpressionEvaluatingTransactionSynchronizationProcessor syncProcessor = new ExpressionEvaluatingTransactionSynchronizationProcessor();
            syncProcessor.setBeanFactory(mock(BeanFactory.class));
            syncProcessor.setAfterRollbackChannel(queueChannel);
            syncProcessor.setAfterRollbackExpression(new SpelExpressionParser().parseExpression("#baz"));
            DefaultTransactionSynchronizationFactory syncFactory = new DefaultTransactionSynchronizationFactory(syncProcessor);
            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) TransactionSynchronizationManager.getResource(this)).addAttribute("baz", "qux");
                    return message;
                }
            });
            doPoll(adapter);
            throw new RuntimeException("Force rollback");
        });
    } catch (Exception e) {
        assertEquals("Force rollback", e.getMessage());
    }
    Message<?> rollbackMessage = queueChannel.receive(1000);
    assertNotNull(rollbackMessage);
    assertEquals("qux", rollbackMessage.getPayload());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) ExpressionEvaluatingTransactionSynchronizationProcessor(org.springframework.integration.transaction.ExpressionEvaluatingTransactionSynchronizationProcessor) DefaultTransactionSynchronizationFactory(org.springframework.integration.transaction.DefaultTransactionSynchronizationFactory) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) PseudoTransactionManager(org.springframework.integration.transaction.PseudoTransactionManager) GenericMessage(org.springframework.messaging.support.GenericMessage) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) PollableChannel(org.springframework.messaging.PollableChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 4 with ExpressionEvaluatingTransactionSynchronizationProcessor

use of org.springframework.integration.transaction.ExpressionEvaluatingTransactionSynchronizationProcessor in project spring-integration by spring-projects.

the class PseudoTransactionalMessageSourceTests method testCommitWithManager.

@Test
public void testCommitWithManager() {
    final PollableChannel queueChannel = new QueueChannel();
    TransactionTemplate transactionTemplate = new TransactionTemplate(new PseudoTransactionManager());
    transactionTemplate.execute(status -> {
        SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter();
        ExpressionEvaluatingTransactionSynchronizationProcessor syncProcessor = new ExpressionEvaluatingTransactionSynchronizationProcessor();
        syncProcessor.setBeanFactory(mock(BeanFactory.class));
        syncProcessor.setBeforeCommitExpression(new SpelExpressionParser().parseExpression("#bix"));
        syncProcessor.setBeforeCommitChannel(queueChannel);
        syncProcessor.setAfterCommitChannel(queueChannel);
        syncProcessor.setAfterCommitExpression(new SpelExpressionParser().parseExpression("#baz"));
        DefaultTransactionSynchronizationFactory syncFactory = new DefaultTransactionSynchronizationFactory(syncProcessor);
        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;
            }
        });
        doPoll(adapter);
        return null;
    });
    Message<?> beforeCommitMessage = queueChannel.receive(1000);
    assertNotNull(beforeCommitMessage);
    assertEquals("qox", beforeCommitMessage.getPayload());
    Message<?> afterCommitMessage = queueChannel.receive(1000);
    assertNotNull(afterCommitMessage);
    assertEquals("qux", afterCommitMessage.getPayload());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) ExpressionEvaluatingTransactionSynchronizationProcessor(org.springframework.integration.transaction.ExpressionEvaluatingTransactionSynchronizationProcessor) DefaultTransactionSynchronizationFactory(org.springframework.integration.transaction.DefaultTransactionSynchronizationFactory) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) PseudoTransactionManager(org.springframework.integration.transaction.PseudoTransactionManager) GenericMessage(org.springframework.messaging.support.GenericMessage) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) IntegrationResourceHolder(org.springframework.integration.transaction.IntegrationResourceHolder) PollableChannel(org.springframework.messaging.PollableChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 5 with ExpressionEvaluatingTransactionSynchronizationProcessor

use of org.springframework.integration.transaction.ExpressionEvaluatingTransactionSynchronizationProcessor in project spring-integration by spring-projects.

the class PseudoTransactionalMessageSourceTests method testRollback.

@Test
public void testRollback() {
    SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter();
    ExpressionEvaluatingTransactionSynchronizationProcessor syncProcessor = new ExpressionEvaluatingTransactionSynchronizationProcessor();
    syncProcessor.setBeanFactory(mock(BeanFactory.class));
    PollableChannel queueChannel = new QueueChannel();
    syncProcessor.setAfterRollbackChannel(queueChannel);
    syncProcessor.setAfterRollbackExpression(new SpelExpressionParser().parseExpression("#baz"));
    DefaultTransactionSynchronizationFactory syncFactory = new DefaultTransactionSynchronizationFactory(syncProcessor);
    adapter.setTransactionSynchronizationFactory(syncFactory);
    QueueChannel outputChannel = new QueueChannel();
    adapter.setOutputChannel(outputChannel);
    final Message<?> testMessage = new GenericMessage<>("foo");
    adapter.setSource(new MessageSource<String>() {

        @Override
        public Message<String> receive() {
            GenericMessage<String> message = new GenericMessage<String>("foo");
            ((IntegrationResourceHolder) TransactionSynchronizationManager.getResource(this)).addAttribute("baz", testMessage);
            return message;
        }
    });
    TransactionSynchronizationManager.initSynchronization();
    TransactionSynchronizationManager.setActualTransactionActive(true);
    doPoll(adapter);
    TransactionSynchronizationUtils.triggerAfterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
    Message<?> rollbackMessage = queueChannel.receive(1000);
    assertNotNull(rollbackMessage);
    assertSame(testMessage, rollbackMessage);
    TransactionSynchronizationManager.clearSynchronization();
    TransactionSynchronizationManager.setActualTransactionActive(false);
}
Also used : ExpressionEvaluatingTransactionSynchronizationProcessor(org.springframework.integration.transaction.ExpressionEvaluatingTransactionSynchronizationProcessor) QueueChannel(org.springframework.integration.channel.QueueChannel) DefaultTransactionSynchronizationFactory(org.springframework.integration.transaction.DefaultTransactionSynchronizationFactory) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) GenericMessage(org.springframework.messaging.support.GenericMessage) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) BeanFactory(org.springframework.beans.factory.BeanFactory) PollableChannel(org.springframework.messaging.PollableChannel) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 BeanFactory (org.springframework.beans.factory.BeanFactory)5 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)5 QueueChannel (org.springframework.integration.channel.QueueChannel)5 DefaultTransactionSynchronizationFactory (org.springframework.integration.transaction.DefaultTransactionSynchronizationFactory)5 ExpressionEvaluatingTransactionSynchronizationProcessor (org.springframework.integration.transaction.ExpressionEvaluatingTransactionSynchronizationProcessor)5 Message (org.springframework.messaging.Message)5 PollableChannel (org.springframework.messaging.PollableChannel)5 GenericMessage (org.springframework.messaging.support.GenericMessage)5 PseudoTransactionManager (org.springframework.integration.transaction.PseudoTransactionManager)3 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)3 IntegrationResourceHolder (org.springframework.integration.transaction.IntegrationResourceHolder)2 Log (org.apache.commons.logging.Log)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 NullChannel (org.springframework.integration.channel.NullChannel)1 MessageChannel (org.springframework.messaging.MessageChannel)1