<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:integration="http://www.springframework.org/schema/integration" xmlns:batch="http://www.springframework.org/schema/batch"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:int-jms="http://www.springframework.org/schema/integration/jms"
	xmlns:jms="http://www.springframework.org/schema/jms" xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd
		http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
		http://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	<import resource="classpath:/jms-context.xml" />

	<job id="job" xmlns="http://www.springframework.org/schema/batch">
		<step id="step">
			<tasklet>
				<chunk reader="reader" writer="writer" commit-interval="2" skip-limit="2">
					<skippable-exception-classes>
						<include class="java.lang.IllegalStateException" />
					</skippable-exception-classes>
				</chunk>
			</tasklet>
		</step>
	</job>

	<bean id="reader" class="org.springframework.batch.integration.chunk.TestItemReader" scope="step">
		<property name="items">
			<list>
				<value>1</value>
				<value>2</value>
				<value>#{jobParameters['item.three']}</value>
				<value>4</value>
				<value>5</value>
				<value>6</value>
				<value>7</value>
				<value>8</value>
				<value>9</value>
			</list>
		</property>
	</bean>

	<bean id="writer" class="org.springframework.batch.integration.chunk.TestItemWriter" />

	<bean id="chunkWriter" class="org.springframework.batch.integration.chunk.ChunkMessageChannelItemWriter" scope="step">
		<property name="messagingOperations" ref="messagingGateway" />
		<property name="replyChannel" ref="replies" />
		<property name="maxWaitTimeouts" value="10" />
	</bean>

	<bean id="messagingGateway" class="org.springframework.integration.core.MessagingTemplate">
		<property name="defaultChannel" ref="requests" />
		<property name="receiveTimeout" value="1000" />
	</bean>

	<int-jms:outbound-channel-adapter connection-factory="connectionFactory" channel="requests"
		destination-name="requests" />
	<integration:channel id="requests" />
	<integration:channel id="incoming" />

	<integration:transformer input-channel="incoming" output-channel="replies" ref="headerExtractor"
		method="extract" />

	<bean id="headerExtractor" class="org.springframework.batch.integration.chunk.JmsRedeliveredExtractor" />

	<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
		<property name="scopes">
			<map>
				<entry key="thread">
					<bean class="org.springframework.context.support.SimpleThreadScope" />
				</entry>
			</map>
		</property>
	</bean>

	<!-- If we use a direct channel for requests then the replies can come in on a thread-local -->
	<integration:channel id="replies" scope="thread">
		<integration:queue />
		<integration:interceptors>
			<bean id="pollerInterceptor" class="org.springframework.batch.integration.chunk.MessageSourcePollerInterceptor">
				<property name="messageSource">
					<bean class="org.springframework.integration.jms.JmsDestinationPollingSource">
						<constructor-arg>
							<bean class="org.springframework.jms.core.JmsTemplate">
								<property name="connectionFactory" ref="connectionFactory" />
								<property name="defaultDestinationName" value="replies" />
								<property name="receiveTimeout" value="100" />
							</bean>
						</constructor-arg>
					</bean>
				</property>
				<property name="channel" ref="incoming" />
			</bean>
		</integration:interceptors>
	</integration:channel>

	<jms:listener-container connection-factory="connectionFactory" transaction-manager="transactionManager"
		acknowledge="transacted">
		<jms:listener destination="requests" response-destination="replies" ref="chunkHandler" method="handleChunk" />
	</jms:listener-container>

	<bean id="chunkHandler" class="org.springframework.batch.integration.chunk.RemoteChunkHandlerFactoryBean">
		<property name="chunkWriter" ref="chunkWriter" />
		<property name="step" ref="step" />
	</bean>

	<bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

	<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
		<property name="transactionManager" ref="transactionManager" />
	</bean>

	<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
		<property name="jobRepository" ref="jobRepository" />
	</bean>

</beans>