Search in sources :

Example 1 with IntegrationResourceHolder

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

the class AbstractPollingEndpoint method doPoll.

private boolean doPoll() {
    IntegrationResourceHolder holder = this.bindResourceHolderIfNecessary(this.getResourceKey(), this.getResourceToBind());
    Message<?> message = null;
    try {
        message = this.receiveMessage();
    } catch (Exception e) {
        if (Thread.interrupted()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Poll interrupted - during stop()? : " + e.getMessage());
            }
            return false;
        } else {
            throw (RuntimeException) e;
        }
    }
    boolean result;
    if (message == null) {
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Received no Message during the poll, returning 'false'");
        }
        result = false;
    } else {
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Poll resulted in Message: " + message);
        }
        if (holder != null) {
            holder.setMessage(message);
        }
        try {
            this.handleMessage(message);
        } catch (Exception e) {
            if (e instanceof MessagingException) {
                throw new MessagingExceptionWrapper(message, (MessagingException) e);
            } else {
                throw new MessagingException(message, e);
            }
        }
        result = true;
    }
    return result;
}
Also used : MessagingException(org.springframework.messaging.MessagingException) IntegrationResourceHolder(org.springframework.integration.transaction.IntegrationResourceHolder) MessagingExceptionWrapper(org.springframework.integration.support.MessagingExceptionWrapper) MessagingException(org.springframework.messaging.MessagingException) BeanInitializationException(org.springframework.beans.factory.BeanInitializationException)

Example 2 with IntegrationResourceHolder

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

the class AbstractPollingEndpoint method bindResourceHolderIfNecessary.

private IntegrationResourceHolder bindResourceHolderIfNecessary(String key, Object resource) {
    if (this.transactionSynchronizationFactory != null && resource != null && TransactionSynchronizationManager.isActualTransactionActive()) {
        TransactionSynchronization synchronization = this.transactionSynchronizationFactory.create(resource);
        if (synchronization != null) {
            TransactionSynchronizationManager.registerSynchronization(synchronization);
            if (synchronization instanceof IntegrationResourceHolderSynchronization) {
                IntegrationResourceHolderSynchronization integrationSynchronization = ((IntegrationResourceHolderSynchronization) synchronization);
                integrationSynchronization.setShouldUnbindAtCompletion(false);
                if (!TransactionSynchronizationManager.hasResource(resource)) {
                    TransactionSynchronizationManager.bindResource(resource, integrationSynchronization.getResourceHolder());
                }
            }
        }
        Object resourceHolder = TransactionSynchronizationManager.getResource(resource);
        if (resourceHolder instanceof IntegrationResourceHolder) {
            IntegrationResourceHolder integrationResourceHolder = (IntegrationResourceHolder) resourceHolder;
            if (key != null) {
                integrationResourceHolder.addAttribute(key, resource);
            }
            return integrationResourceHolder;
        }
    }
    return null;
}
Also used : IntegrationResourceHolderSynchronization(org.springframework.integration.transaction.IntegrationResourceHolderSynchronization) TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) IntegrationResourceHolder(org.springframework.integration.transaction.IntegrationResourceHolder)

Example 3 with IntegrationResourceHolder

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

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

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

the class ImapIdleChannelAdapter method createMessageSendingTask.

private Runnable createMessageSendingTask(final Object mailMessage) {
    Runnable sendingTask = () -> {
        @SuppressWarnings("unchecked") org.springframework.messaging.Message<?> message = mailMessage instanceof Message ? ImapIdleChannelAdapter.this.getMessageBuilderFactory().withPayload(mailMessage).build() : (org.springframework.messaging.Message<Object>) mailMessage;
        if (TransactionSynchronizationManager.isActualTransactionActive()) {
            if (ImapIdleChannelAdapter.this.transactionSynchronizationFactory != null) {
                TransactionSynchronization synchronization = ImapIdleChannelAdapter.this.transactionSynchronizationFactory.create(ImapIdleChannelAdapter.this);
                if (synchronization != null) {
                    TransactionSynchronizationManager.registerSynchronization(synchronization);
                    if (synchronization instanceof IntegrationResourceHolderSynchronization && !TransactionSynchronizationManager.hasResource(ImapIdleChannelAdapter.this)) {
                        TransactionSynchronizationManager.bindResource(ImapIdleChannelAdapter.this, ((IntegrationResourceHolderSynchronization) synchronization).getResourceHolder());
                    }
                    Object resourceHolder = TransactionSynchronizationManager.getResource(ImapIdleChannelAdapter.this);
                    if (resourceHolder instanceof IntegrationResourceHolder) {
                        ((IntegrationResourceHolder) resourceHolder).setMessage(message);
                    }
                }
            }
        }
        sendMessage(message);
    };
    // wrap in the TX proxy if necessary
    if (!CollectionUtils.isEmpty(this.adviceChain)) {
        ProxyFactory proxyFactory = new ProxyFactory(sendingTask);
        if (!CollectionUtils.isEmpty(this.adviceChain)) {
            for (Advice advice : this.adviceChain) {
                proxyFactory.addAdvice(advice);
            }
        }
        sendingTask = (Runnable) proxyFactory.getProxy(this.classLoader);
    }
    return sendingTask;
}
Also used : IntegrationResourceHolderSynchronization(org.springframework.integration.transaction.IntegrationResourceHolderSynchronization) TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) Message(javax.mail.Message) ProxyFactory(org.springframework.aop.framework.ProxyFactory) IntegrationResourceHolder(org.springframework.integration.transaction.IntegrationResourceHolder) Advice(org.aopalliance.aop.Advice)

Aggregations

IntegrationResourceHolder (org.springframework.integration.transaction.IntegrationResourceHolder)8 Test (org.junit.Test)3 QueueChannel (org.springframework.integration.channel.QueueChannel)3 DefaultTransactionSynchronizationFactory (org.springframework.integration.transaction.DefaultTransactionSynchronizationFactory)3 Message (org.springframework.messaging.Message)3 PollableChannel (org.springframework.messaging.PollableChannel)3 GenericMessage (org.springframework.messaging.support.GenericMessage)3 BeanFactory (org.springframework.beans.factory.BeanFactory)2 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)2 ExpressionEvaluatingTransactionSynchronizationProcessor (org.springframework.integration.transaction.ExpressionEvaluatingTransactionSynchronizationProcessor)2 IntegrationResourceHolderSynchronization (org.springframework.integration.transaction.IntegrationResourceHolderSynchronization)2 TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)2 DBObject (com.mongodb.DBObject)1 Collection (java.util.Collection)1 Message (javax.mail.Message)1 Advice (org.aopalliance.aop.Advice)1 Log (org.apache.commons.logging.Log)1 ProxyFactory (org.springframework.aop.framework.ProxyFactory)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 BeanInitializationException (org.springframework.beans.factory.BeanInitializationException)1