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

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

Introduction

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

Prototype

public void setAfterCommitChannel(MessageChannel afterCommitChannel) 

Source Link

Usage

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

@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);/*from   w w  w.j av  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 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());
}