Search in sources :

Example 6 with IntegrationResourceHolder

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

the class MongoDbMessageSource method receive.

/**
 * Will execute a {@link Query} returning its results as the Message payload.
 * The payload can be either {@link List} of elements of objects of type
 * identified by {{@link #entityClass}, or a single element of type identified by {{@link #entityClass}
 * based on the value of {{@link #expectSingleResult} attribute which defaults to 'false' resulting
 * {@link Message} with payload of type {@link List}. The collection name used in the
 * query will be provided in the {@link MongoHeaders#COLLECTION_NAME} header.
 */
@Override
public Message<Object> receive() {
    Assert.isTrue(this.initialized, "This class is not yet initialized. Invoke its afterPropertiesSet() method");
    Message<Object> message = null;
    Object value = this.queryExpression.getValue(this.evaluationContext);
    Assert.notNull(value, "'queryExpression' must not evaluate to null");
    Query query;
    if (value instanceof String) {
        query = new BasicQuery((String) value);
    } else if (value instanceof Query) {
        query = ((Query) value);
    } else {
        throw new IllegalStateException("'queryExpression' must evaluate to String " + "or org.springframework.data.mongodb.core.query.Query");
    }
    Assert.notNull(query, "'queryExpression' must not evaluate to null");
    String collectionName = this.collectionNameExpression.getValue(this.evaluationContext, String.class);
    Assert.notNull(collectionName, "'collectionNameExpression' must not evaluate to null");
    Object result = null;
    if (this.expectSingleResult) {
        result = this.mongoTemplate.findOne(query, this.entityClass, collectionName);
    } else {
        List<?> results = this.mongoTemplate.find(query, this.entityClass, collectionName);
        if (!CollectionUtils.isEmpty(results)) {
            result = results;
        }
    }
    if (result != null) {
        message = this.getMessageBuilderFactory().withPayload(result).setHeader(MongoHeaders.COLLECTION_NAME, collectionName).build();
    }
    Object holder = TransactionSynchronizationManager.getResource(this);
    if (holder != null) {
        Assert.isInstanceOf(IntegrationResourceHolder.class, holder);
        ((IntegrationResourceHolder) holder).addAttribute("mongoTemplate", this.mongoTemplate);
    }
    return message;
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) Query(org.springframework.data.mongodb.core.query.Query) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) IntegrationResourceHolder(org.springframework.integration.transaction.IntegrationResourceHolder) DBObject(com.mongodb.DBObject)

Example 7 with IntegrationResourceHolder

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

the class PseudoTransactionalMessageSourceTests method testTransactionSynchronizationFactoryBean.

@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
        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();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) GenericMessage(org.springframework.messaging.support.GenericMessage) IntegrationResourceHolder(org.springframework.integration.transaction.IntegrationResourceHolder) PollableChannel(org.springframework.messaging.PollableChannel) DefaultTransactionSynchronizationFactory(org.springframework.integration.transaction.DefaultTransactionSynchronizationFactory) TransactionSynchronizationFactory(org.springframework.integration.transaction.TransactionSynchronizationFactory) Test(org.junit.Test)

Example 8 with IntegrationResourceHolder

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

the class RedisStoreMessageSource method receive.

/**
 * Returns a Message with the view into a {@link RedisStore} identified
 * by {@link #keyExpression}
 */
@Override
@SuppressWarnings("unchecked")
public Message<RedisStore> receive() {
    String key = this.keyExpression.getValue(this.evaluationContext, String.class);
    Assert.hasText(key, "Failed to determine the key for the collection");
    RedisStore store = this.createStoreView(key);
    Object holder = TransactionSynchronizationManager.getResource(this);
    if (holder != null) {
        Assert.isInstanceOf(IntegrationResourceHolder.class, holder);
        ((IntegrationResourceHolder) holder).addAttribute("store", store);
    }
    if (store instanceof Collection<?> && ((Collection<Object>) store).size() < 1) {
        return null;
    } else {
        return this.getMessageBuilderFactory().withPayload(store).build();
    }
}
Also used : IntegrationResourceHolder(org.springframework.integration.transaction.IntegrationResourceHolder) RedisStore(org.springframework.data.redis.support.collections.RedisStore) Collection(java.util.Collection)

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