Example usage for org.springframework.integration.transaction IntegrationResourceHolder addAttribute

List of usage examples for org.springframework.integration.transaction IntegrationResourceHolder addAttribute

Introduction

In this page you can find the example usage for org.springframework.integration.transaction IntegrationResourceHolder addAttribute.

Prototype

public void addAttribute(String key, Object value) 

Source Link

Document

Adds attribute to this ResourceHolder instance

Usage

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

@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);//from ww  w  .ja  v a2s  .  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 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);
}

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

@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//  w  w w .j ava2s.c om
        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();
}

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  ww .  j av  a 2  s.c  om

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