<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

	<import resource="classpath:/data-source.xml"/>

	<!-- Transaction manager for a datasource -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- Transaction manager for jms -->
	<bean id="jmsTransactionManager" autowire-candidate="false"
		class="org.springframework.jms.connection.JmsTransactionManager">
		<property name="connectionFactory" ref="connectionFactory" />
	</bean>

	<bean id="jmsTemplate"
		class="org.springframework.jms.core.JmsTemplate">
		<property name="connectionFactory" ref="connectionFactory" />
		<property name="receiveTimeout" value="100" />
		<!-- This is important... -->
		<property name="sessionTransacted" value="true" />
	</bean>

	<bean id="jdbcTemplate"
		class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<bean id="connectionFactory"
		class="org.apache.activemq.ActiveMQConnectionFactory" depends-on="brokerService">
		<property name="brokerURL">
			<value>vm://localhost</value>
		</property>
	</bean>

	<bean id="txAwareConnectionFactory"
		class="org.springframework.jms.connection.TransactionAwareConnectionFactoryProxy">
		<property name="targetConnectionFactory" ref="connectionFactory"/>
		<property name="synchedLocalTransactionAllowed" value="true" />
	</bean>

	<bean id="container"
		class="org.springframework.batch.container.jms.BatchMessageListenerContainer">
		<property name="transactionManager" ref="transactionManager" />
		<property name="recoveryInterval" value="0" />
		<property name="connectionFactory"
			ref="txAwareConnectionFactory" />
		<property name="destinationName" value="queue" />
		<!-- This is important... it forces the container to acknowledge message receipt, 
			and avoid duplicate messages in the sunny day case -->
		<property name="sessionTransacted" value="true" />
		<constructor-arg ref="transactionalBatchTemplate" />
	</bean>

	<bean id="batchTemplate"
		class="org.springframework.batch.repeat.support.RepeatTemplate">
		<property name="completionPolicy">
			<bean 
				class="org.springframework.batch.repeat.policy.SimpleCompletionPolicy">
				<constructor-arg value="2" />
			</bean>
		</property>
	</bean>

	<bean id="transactionalBatchTemplate"
		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
		<property name="transactionManager" ref="transactionManager" />
		<property name="proxyInterfaces">
			<value>
				org.springframework.batch.repeat.RepeatOperations
			</value>
		</property>
		<property name="proxyTargetClass" value="false" />
		<property name="transactionAttributes"
			value="*=PROPAGATION_REQUIRED">
		</property>
		<property name="target">
			<bean
				class="org.springframework.batch.repeat.support.RepeatTemplate">
				<property name="completionPolicy">
					<bean
						class="org.springframework.batch.repeat.policy.SimpleCompletionPolicy">
						<constructor-arg value="2" />
					</bean>
				</property>
			</bean>
		</property>
	</bean>

</beans>