INTSAMPLES-91 Transaction Synchronization Sample

INTSAMPLES-91 Initial Commit

INTSAMPLES-91 Polishing

INTSAMPLES-91 Add JDBC Activity
This commit is contained in:
Gary Russell
2012-09-25 15:30:53 +01:00
parent 542f3569e4
commit 80986a0080
8 changed files with 380 additions and 0 deletions

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-stream="http://www.springframework.org/schema/integration/stream"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:int-file="http://www.springframework.org/schema/integration/file"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:int-jdbc="http://www.springframework.org/schema/integration/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file.xsd
http://www.springframework.org/schema/integration/jdbc http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc-2.2.xsd
http://www.springframework.org/schema/integration/stream http://www.springframework.org/schema/integration/stream/spring-integration-stream.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<context:property-placeholder />
<int-file:inbound-channel-adapter
channel="inputChannel"
auto-create-directory="true"
filename-pattern="*.txt"
directory="${java.io.tmpdir}/txSynchDemo/">
<int:poller fixed-delay="500">
<int:transactional transaction-manager="txManager" synchronization-factory="syncFactory" />
</int:poller>
</int-file:inbound-channel-adapter>
<int:transaction-synchronization-factory id="syncFactory">
<int:after-commit expression="payload.renameTo(payload.absolutePath + '.SUCCEEDED') ? payload + ' renamed after success' : payload + 'failed to rename after success'"
channel="infoLogger" />
<int:after-rollback expression="payload.renameTo(payload.absolutePath + '.FAILED') ? payload + ' renamed after faiure' : payload + 'failed to rename after failure'"
channel="errorLogger" />
</int:transaction-synchronization-factory>
<int:channel id="inputChannel"/>
<int-file:file-to-string-transformer input-channel="inputChannel" output-channel="toServiceChannel" />
<int:service-activator input-channel="toServiceChannel"
ref="conditionalService"
method="failIfTextIsFail" />
<int:logging-channel-adapter id="infoLogger" level="INFO" />
<int:logging-channel-adapter id="errorLogger" level="ERROR" />
<bean id="conditionalService" class="org.springframework.integration.samples.advice.ConditionalService" />
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:table.sql" />
</jdbc:embedded-database>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- Poll the database for rows -->
<int-jdbc:inbound-channel-adapter channel="infoLogger"
query="select BAR from FOO" data-source="dataSource">
<int:poller fixed-delay="5000" />
</int-jdbc:inbound-channel-adapter>
</beans>

View File

@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss.SSS} %-5p [%t][%c] %m%n" />
</layout>
</appender>
<!-- Loggers -->
<logger name="org.springframework">
<level value="warn" />
</logger>
<logger name="org.springframework.integration.endpoint.SourcePollingChannelAdapter">
<level value="warn" />
</logger>
<logger name="org.springframework.integration">
<level value="warn" />
</logger>
<logger name="org.springframework.integration.handler.LoggingHandler">
<level value="info" />
</logger>
<logger name="org.springframework.integration.samples">
<level value="info" />
</logger>
<logger name="org.springframework.retry">
<level value="warn" />
</logger>
<!-- Root Logger -->
<root>
<priority value="warn" />
<appender-ref ref="console" />
</root>
</log4j:configuration>

View File

@@ -0,0 +1,4 @@
CREATE TABLE IF NOT EXISTS FOO (
BAR VARCHAR(256)
);