Example usage for org.springframework.integration.transaction ExpressionEvaluatingTransactionSynchronizationProcessor setAfterRollbackChannel

List of usage examples for org.springframework.integration.transaction ExpressionEvaluatingTransactionSynchronizationProcessor setAfterRollbackChannel

Introduction

In this page you can find the example usage for org.springframework.integration.transaction ExpressionEvaluatingTransactionSynchronizationProcessor setAfterRollbackChannel.

Prototype

public void setAfterRollbackChannel(MessageChannel afterRollbackChannel) 

Source Link

Usage

From source file:org.springframework.integration.endpoint.PseudoTransactionalMessageSourceTests.java

@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);/*  w  w  w  .  j  a  v a  2  s . co m*/

    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;
        }
    });

    TransactionSynchronizationManager.initSynchronization();
    TransactionSynchronizationManager.setActualTransactionActive(true);
    doPoll(adapter);
    TransactionSynchronizationUtils.triggerAfterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
    Message<?> rollbackMessage = queueChannel.receive(1000);
    assertNotNull(rollbackMessage);
    assertEquals("qux", rollbackMessage.getPayload());
    TransactionSynchronizationManager.clearSynchronization();
    TransactionSynchronizationManager.setActualTransactionActive(false);
}

From source file:org.springframework.integration.endpoint.PseudoTransactionalMessageSourceTests.java

@Test
public void testRollbackWithManager() {
    final PollableChannel queueChannel = new QueueChannel();
    TransactionTemplate transactionTemplate = new TransactionTemplate(new PseudoTransactionManager());
    try {/*w  ww.j  a v  a2  s.com*/
        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());
}

From source file:org.springframework.integration.endpoint.PseudoTransactionalMessageSourceTests.java

@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);//w w w. j a v  a 2 s .c  o  m

        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());
}