Search in sources :

Example 1 with PseudoTransactionManager

use of org.springframework.integration.transaction.PseudoTransactionManager 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 2 with PseudoTransactionManager

use of org.springframework.integration.transaction.PseudoTransactionManager 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 3 with PseudoTransactionManager

use of org.springframework.integration.transaction.PseudoTransactionManager 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)

Aggregations

Test (org.junit.Test)3 BeanFactory (org.springframework.beans.factory.BeanFactory)3 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)3 QueueChannel (org.springframework.integration.channel.QueueChannel)3 DefaultTransactionSynchronizationFactory (org.springframework.integration.transaction.DefaultTransactionSynchronizationFactory)3 ExpressionEvaluatingTransactionSynchronizationProcessor (org.springframework.integration.transaction.ExpressionEvaluatingTransactionSynchronizationProcessor)3 PseudoTransactionManager (org.springframework.integration.transaction.PseudoTransactionManager)3 Message (org.springframework.messaging.Message)3 PollableChannel (org.springframework.messaging.PollableChannel)3 GenericMessage (org.springframework.messaging.support.GenericMessage)3 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)3 IntegrationResourceHolder (org.springframework.integration.transaction.IntegrationResourceHolder)1