<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.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">

	<int-stream:stdin-channel-adapter channel="inputChannel">
		<int:poller fixed-delay="1000" error-channel="failedChannel" />
	</int-stream:stdin-channel-adapter>

	<int:channel id="inputChannel"/>

	<int:header-enricher input-channel="inputChannel" output-channel="toServiceChannel">
		<int:header name="failingId" expression="headers['id'].toString()" />
	</int:header-enricher>

	<int:service-activator input-channel="toServiceChannel"
					   ref="conditionalService"
					   method="testRetry">
		<int:request-handler-advice-chain>
			<ref bean="retryAdvice" />
		</int:request-handler-advice-chain>
	</int:service-activator>

	<int:channel id="failedChannel" />

	<int:chain input-channel="failedChannel">
		<int:transformer expression="'permanently failed:' + payload.failedMessage.payload" />
		<int-stream:stderr-channel-adapter append-newline="true"/>
	</int:chain>

	<bean id="conditionalService" class="org.springframework.integration.samples.advice.ConditionalService" />

	<!-- Default retry advice 3 tries, no backoff, no recovery;
	     final failure will go to poller's error channel -->
	<beans profile="default">
		<int:handler-retry-advice id="retryAdvice" />
	</beans>

	<!-- Retry advice, template with default 3 tries, exponential backoff, no recovery;
	     final failure will go to poller's error channel -->
	<beans profile="backoff">
		<int:handler-retry-advice id="retryAdvice">
			<int:exponential-back-off initial="2000" multiplier="2" />
		</int:handler-retry-advice>
	</beans>

	<!-- Retry advice, template with default 3 tries, no backoff, recovery;
	     final handled by recovery instead of going to the poller's error channel -->
	<beans profile="recovery">
		<int:handler-retry-advice id="retryAdvice" recovery-channel="recoveryChannel" />

		<int:channel id="recoveryChannel" />

		<int:chain input-channel="recoveryChannel">
			<int:transformer expression="'permanently failed:' + payload.failedMessage.payload + ' handled by recovery'" />
			<int-stream:stderr-channel-adapter append-newline="true"/>
		</int:chain>
	</beans>

</beans>
