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