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