INT-2685 Transaction Synchronization

Remove M3 disposition-* attributes on File/(S)FTP inbound adapters.

Add <pseudo-transactional/> and <transaction-synchronization/> elements
to <poller/>.

These elements provide the following attributes:

* on-success-expression
* on-success-result-channel
* on-failure-expression
* on-failure-result-channel
* send-timeout

<transaction-synchronization/> synchronizes these expression evaluations
with the transaction, such that they are executed immediately after
the commit/rollback.

<psuedo-transactional/> is used for a non-transactional poller.

When an <advice-chain/> is provided to the poller, <psuedo-transactional/>
and <transaction-synchronization/> are synonyms; and the behavior is
dictated by whether or not the <advice-chain/> contains a transaction
advice. It is recommended that <pseudo-transactional/> is used when the
<advice-chain/> does not have a txAdvice, and <transaction-synchronization/>
when it does, but the framework does not enforce this.

The expressions have the original (polled) message as the #root variable.
In addition, a BeanResolver is provided, allowing expressions such as
'@someBean.handleSuccess(payload)'.

MessageSources may also implement PseudoTransactionalMessageSource. This
has a number of methods allowing more flexibility in transactional and
non-transactional environents. For example, for backwards compatibility.
the mail-inbound-channel-adapter deletes its polled message after the
receive() rather than after the polled message is sent (when running in
a non-transactional poller). However, when running in a transactional
poller, the delete is done after the transaction commits (but not when
it rolls back).

In addition, MessageSources that implement this interface can optionally
provide an arbitrary object to the success/failure expressions in a
variable named '#resource'.

INT-2685 Polishing

PR Review Comments

Add tests for non-tx PseudoTransactionalMessageSource
This commit is contained in:
Gary Russell
2012-07-26 18:23:49 -04:00
committed by Oleg Zhurakousky
parent d6623bda82
commit 4de02fa75e
33 changed files with 892 additions and 458 deletions

View File

@@ -9,16 +9,15 @@
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<int:inbound-channel-adapter channel="queue">
<int:inbound-channel-adapter channel="input">
<bean class="org.springframework.integration.jdbc.PseudoTransactionalMessageSourceTests$MessageSource"/>
<int:poller fixed-delay="2000">
<int:transactional />
<int:transaction-synchronization/>
</int:poller>
</int:inbound-channel-adapter>
<int:channel id="queue">
<int:queue />
</int:channel>
<int:channel id="input" />
<jdbc:embedded-database id="dataSource" type="HSQL" />

View File

@@ -22,8 +22,12 @@ import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.Message;
import org.springframework.integration.MessagingException;
import org.springframework.integration.core.MessageHandler;
import org.springframework.integration.core.PseudoTransactionalMessageSource;
import org.springframework.integration.core.SubscribableChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -45,27 +49,39 @@ public class PseudoTransactionalMessageSourceTests {
private static boolean rolledBack;
private static boolean doRollback;
@Autowired
private SubscribableChannel input;
@Test
public void testCommit() throws Exception {
MessageHandler handler = new MessageHandler() {
public void handleMessage(Message<?> message) throws MessagingException {
}
};
input.subscribe(handler);
assertTrue(latch1.await(10, TimeUnit.SECONDS));
assertTrue(committed);
input.unsubscribe(handler);
}
@Test
public void testRollback() throws Exception {
doRollback = true;
MessageHandler handler = new MessageHandler() {
public void handleMessage(Message<?> message) throws MessagingException {
throw new RuntimeException("expected");
}
};
input.subscribe(handler);
assertTrue(latch2.await(10, TimeUnit.SECONDS));
assertTrue(rolledBack);
input.unsubscribe(handler);
}
public static class MessageSource implements PseudoTransactionalMessageSource<String, Object> {
public Message<String> receive() {
if (doRollback) {
throw new RuntimeException("Expected");
}
return new GenericMessage<String>("foo");
}