diff --git a/spring-integration/pom.xml b/spring-integration/pom.xml index 367d25de98..f06cb91ef6 100644 --- a/spring-integration/pom.xml +++ b/spring-integration/pom.xml @@ -68,6 +68,11 @@ spring-integration-security ${spring.version} + + org.springframework.integration + spring-integration-jdbc + ${spring.version} + org.springframework.security @@ -75,6 +80,12 @@ ${spring.version} test + + + com.h2database + h2 + 1.4.197 + @@ -106,7 +117,7 @@ UTF-8 - 5.0.3.RELEASE + 5.0.13.RELEASE 1.1.4.RELEASE 1.4.7 1.1.1 diff --git a/spring-integration/src/main/java/com/baeldung/tx/ServiceActivator.java b/spring-integration/src/main/java/com/baeldung/tx/ServiceActivator.java new file mode 100755 index 0000000000..874d5d6957 --- /dev/null +++ b/spring-integration/src/main/java/com/baeldung/tx/ServiceActivator.java @@ -0,0 +1,27 @@ +package com.baeldung.tx; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; + +public class ServiceActivator { + + @Autowired + private JdbcTemplate jdbcTemplate; + + private final Logger log = LoggerFactory.getLogger(this.getClass()); + + public void checkTestResults(String payload) { + + this.jdbcTemplate.update("insert into STUDENT values(?)", payload); + + if (payload.toLowerCase().startsWith("fail")) { + log.error("Service failure. Test result: {} ", payload); + throw new RuntimeException("Service failure."); + } + + log.info("Service success. Test result: {}", payload); + } + +} diff --git a/spring-integration/src/main/resources/META-INF/spring/integration/spring-integration-tx-context.xml b/spring-integration/src/main/resources/META-INF/spring/integration/spring-integration-tx-context.xml new file mode 100755 index 0000000000..2861826521 --- /dev/null +++ b/spring-integration/src/main/resources/META-INF/spring/integration/spring-integration-tx-context.xml @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-integration/src/main/resources/table.sql b/spring-integration/src/main/resources/table.sql new file mode 100644 index 0000000000..9ca58f6f27 --- /dev/null +++ b/spring-integration/src/main/resources/table.sql @@ -0,0 +1,4 @@ + +CREATE TABLE IF NOT EXISTS STUDENT ( + TEST VARCHAR(256) +); \ No newline at end of file diff --git a/spring-integration/src/test/java/com/baeldung/tx/TxIntegrationTest.java b/spring-integration/src/test/java/com/baeldung/tx/TxIntegrationTest.java new file mode 100644 index 0000000000..dd9f4ab286 --- /dev/null +++ b/spring-integration/src/test/java/com/baeldung/tx/TxIntegrationTest.java @@ -0,0 +1,56 @@ +package com.baeldung.tx; + +import org.junit.Assert; +import org.junit.Test; +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +public final class TxIntegrationTest { + + private static final String CONTEXT_CONFIG = "classpath:META-INF/spring/integration/spring-integration-tx-context.xml"; + + @Test + public void whenFileDoesntStartWithFail_thanTxSuccessful() throws InterruptedException, IOException { + final AbstractApplicationContext context = + new ClassPathXmlApplicationContext(CONTEXT_CONFIG); + + String fileName = System.getProperty("java.io.tmpdir") + "/tx/test1.txt"; + FileWriter fw = new FileWriter(fileName); + fw.write("PASSED!"); + fw.close(); + + context.registerShutdownHook(); + Thread.sleep(5000); + + File file = new File(fileName + ".PASSED"); + Assert.assertTrue(file.exists()); + } + + @Test + public void whenFileStartsWithFail_thanTxFailed() { + + String fileName = System.getProperty("java.io.tmpdir") + "/tx/test2.txt"; + + try { + final AbstractApplicationContext context = + new ClassPathXmlApplicationContext(CONTEXT_CONFIG); + + FileWriter fw = new FileWriter(fileName); + fw.write("FAILED!"); + fw.close(); + + context.registerShutdownHook(); + Thread.sleep(5000); + } catch (Exception e) { + // Exception is expected, do nothing + } + + File file = new File(fileName + ".FAILED"); + Assert.assertTrue(file.exists()); + } + +}