From 80986a00801e4e18e31dcbf61b809241117033a4 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Tue, 25 Sep 2012 15:30:53 +0100 Subject: [PATCH] INTSAMPLES-91 Transaction Synchronization Sample INTSAMPLES-91 Initial Commit INTSAMPLES-91 Polishing INTSAMPLES-91 Add JDBC Activity --- README.md | 1 + intermediate/tx-synch/README.md | 48 +++++++++ intermediate/tx-synch/pom.xml | 102 ++++++++++++++++++ .../samples/advice/ConditionalService.java | 48 +++++++++ .../TransactionSynchronizationDemo.java | 63 +++++++++++ .../integration/transaction-synch-context.xml | 70 ++++++++++++ .../tx-synch/src/main/resources/log4j.xml | 44 ++++++++ .../tx-synch/src/main/resources/table.sql | 4 + 8 files changed, 380 insertions(+) create mode 100755 intermediate/tx-synch/README.md create mode 100755 intermediate/tx-synch/pom.xml create mode 100755 intermediate/tx-synch/src/main/java/org/springframework/integration/samples/advice/ConditionalService.java create mode 100755 intermediate/tx-synch/src/main/java/org/springframework/integration/samples/advice/TransactionSynchronizationDemo.java create mode 100755 intermediate/tx-synch/src/main/resources/META-INF/spring/integration/transaction-synch-context.xml create mode 100755 intermediate/tx-synch/src/main/resources/log4j.xml create mode 100644 intermediate/tx-synch/src/main/resources/table.sql diff --git a/README.md b/README.md index db8186e8..23795ee8 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ This category targets developers who are already more familiar with the Spring I * **stored-procedures-oracle** Provides an example of the stored procedure Outbound Gateway using *ORACLE XE* * **monitoring** The project used in the *[Spring Integration Management and Monitoring Webinar](http://www.springsource.org/node/3598)* Also available on the *[SpringSourceDev YouTube Channel](http://www.youtube.com/SpringSourceDev)* * **retry-and-more** Provides samples showing the application of MessageHandler Advice Chains to endpoints - retry, circuit breaker, expression evaluating +* **tx-synch** Provides a sample demonstrating the use of transaction syncrhonization, renaming an input file to a different filename, depending on whether the transaction commits, or rolls back. ## Advanced diff --git a/intermediate/tx-synch/README.md b/intermediate/tx-synch/README.md new file mode 100755 index 00000000..85dce620 --- /dev/null +++ b/intermediate/tx-synch/README.md @@ -0,0 +1,48 @@ +Transaction Synchronization Sample "tx-synch" +============================================= + +This sample shows how to use the 2.2.0 Transaction Synchronization feature. + +Run the class `TransactionSynchronizationDemo` as a java application (main). + +The Spring Integration application consists of a simple flow that reads a file, and stores its contents in a database table. + +If the contents of the file starts with "fail", after writing to the database, the 'ConditionalService' throws an exception and rolls back the database update. + +There are two synchronization actions + +- rename the file, adding '.SUCCEEDED' to the filename when the transaction commits +- rename the file, adding '.FAILED' to the filename when the transaction rolls back + +These actions are logged. + +The file inbound adapter looks for files in ${java.io.tmpdir}/txSynchDemo + +The java.io.tmpdir for your machine is displayed in the console… + +```` + This is the Transaction Synchronization Sample - + + Press 'Enter' to terminate. + + Place a file in /var/folders/k0/gch26h6d2ms9t0g7pyhtzfkc0000gn/T/txSynchDemo ending + with .txt + If the first line begins with 'fail' the transaction + transaction will be rolled back.The result of the + expression evaluation is logged. + +========================================================= +/var/folders/k0/gch26h6d2ms9t0g7pyhtzfkc0000gn/T/txSynchDir +```` + +To send a good file, put a file in that directory; for example: + +echo good > /var/folders/k0/gch26h6d2ms9t0g7pyhtzfkc0000gn/T/txSynchDemo/xx.txt + +echo failing > /var/folders/k0/gch26h6d2ms9t0g7pyhtzfkc0000gn/T/txSynchDemo/x.txt + + +The inbound adapter uses the default filters so each filename may only be used once for each run of the program. + + +In addition, there is a JDBC inbound adapter that polls the database every 5 seconds, and logs its contents - this can be used to see commit Vs. rollback in the database. diff --git a/intermediate/tx-synch/pom.xml b/intermediate/tx-synch/pom.xml new file mode 100755 index 00000000..9289c481 --- /dev/null +++ b/intermediate/tx-synch/pom.xml @@ -0,0 +1,102 @@ + + + 4.0.0 + org.springframework.integration.samples + tx-synch + 2.1.0.BUILD-SNAPSHOT + Samples (Intermediate) - Transaction Synchronization + jar + + UTF-8 + 3.1.2.RELEASE + 2.2.0.RC1 + 1.2.16 + 4.10 + + + + + org.springframework.integration + spring-integration-stream + ${spring.integration.version} + + + org.springframework.integration + spring-integration-jdbc + ${spring.integration.version} + + + org.springframework.integration + spring-integration-file + ${spring.integration.version} + + + org.springframework.integration + spring-integration-ftp + ${spring.integration.version} + + + org.springframework + spring-jdbc + ${spring.framework.version} + + + com.h2database + h2 + 1.3.166 + + + log4j + log4j + ${log4j.version} + + + org.mockito + mockito-all + 1.8.5 + + + + org.springframework + spring-test + ${spring.framework.version} + + + junit + junit + ${junit.version} + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.5 + 1.5 + -Xlint:all + true + false + + + + org.codehaus.mojo + exec-maven-plugin + 1.2 + + ${java.main.class} + + + + + + + repo.springsource.org.milestone + Spring Framework Maven Milestone Repository + https://repo.springsource.org/libs-snapshot + + + diff --git a/intermediate/tx-synch/src/main/java/org/springframework/integration/samples/advice/ConditionalService.java b/intermediate/tx-synch/src/main/java/org/springframework/integration/samples/advice/ConditionalService.java new file mode 100755 index 00000000..77b3f234 --- /dev/null +++ b/intermediate/tx-synch/src/main/java/org/springframework/integration/samples/advice/ConditionalService.java @@ -0,0 +1,48 @@ +/* + * Copyright 2002-2012 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.integration.samples.advice; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; + +/** + * @author Gary Russell + * @since 2.2 + * + */ +public class ConditionalService { + + @Autowired + private JdbcTemplate jdbcTemplate; + + private final Log logger = LogFactory.getLog(this.getClass()); + + /** + * If this service receives a payload 'fail.*' it throws an Exception. + * @param payload + */ + public void failIfTextIsFail(String payload) { + this.jdbcTemplate.update("insert into FOO values(?)", payload); + if (payload.startsWith("fail")) { + logger.info("Service failure for " + payload); + throw new RuntimeException("Forced Exception"); + } + logger.info("Service success for " + payload); + } + +} diff --git a/intermediate/tx-synch/src/main/java/org/springframework/integration/samples/advice/TransactionSynchronizationDemo.java b/intermediate/tx-synch/src/main/java/org/springframework/integration/samples/advice/TransactionSynchronizationDemo.java new file mode 100755 index 00000000..5841110a --- /dev/null +++ b/intermediate/tx-synch/src/main/java/org/springframework/integration/samples/advice/TransactionSynchronizationDemo.java @@ -0,0 +1,63 @@ +/* + * Copyright 2002-2012 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.integration.samples.advice; + +import org.apache.log4j.Logger; +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * @author Gary Russell + * @since 2.2 + * + */ +public class TransactionSynchronizationDemo { + + private static final Logger LOGGER = Logger.getLogger(TransactionSynchronizationDemo.class); + + public static void main(String[] args) throws Exception { + LOGGER.info("\n=========================================================" + + "\n " + + "\n Welcome to Spring Integration! " + + "\n " + + "\n For more information please visit: " + + "\n http://www.springsource.org/spring-integration " + + "\n " + + "\n=========================================================" ); + + final AbstractApplicationContext context = + new ClassPathXmlApplicationContext("classpath:META-INF/spring/integration/transaction-synch-context.xml"); + + context.registerShutdownHook(); + + LOGGER.info("\n=========================================================" + + "\n " + + "\n This is the Transaction Synchronization Sample - " + + "\n " + + "\n Press 'Enter' to terminate. " + + "\n " + + "\n Place a file in " + System.getProperty("java.io.tmpdir") + "/txSynchDemo ending " + + "\n with .txt " + + "\n If the first line begins with 'fail' the transaction " + + "\n transaction will be rolled back.The result of the " + + "\n expression evaluation is logged. " + + "\n " + + "\n=========================================================" ); + System.out.println(System.getProperty("java.io.tmpdir") + "/txSynchDir"); + System.in.read(); + System.exit(0); + } +} diff --git a/intermediate/tx-synch/src/main/resources/META-INF/spring/integration/transaction-synch-context.xml b/intermediate/tx-synch/src/main/resources/META-INF/spring/integration/transaction-synch-context.xml new file mode 100755 index 00000000..9244deac --- /dev/null +++ b/intermediate/tx-synch/src/main/resources/META-INF/spring/integration/transaction-synch-context.xml @@ -0,0 +1,70 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/intermediate/tx-synch/src/main/resources/log4j.xml b/intermediate/tx-synch/src/main/resources/log4j.xml new file mode 100755 index 00000000..493991ec --- /dev/null +++ b/intermediate/tx-synch/src/main/resources/log4j.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/intermediate/tx-synch/src/main/resources/table.sql b/intermediate/tx-synch/src/main/resources/table.sql new file mode 100644 index 00000000..923db516 --- /dev/null +++ b/intermediate/tx-synch/src/main/resources/table.sql @@ -0,0 +1,4 @@ + +CREATE TABLE IF NOT EXISTS FOO ( + BAR VARCHAR(256) +); \ No newline at end of file