<?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 https://www.springframework.org/schema/integration/spring-integration.xsd
		http://www.springframework.org/schema/integration/stream https://www.springframework.org/schema/integration/stream/spring-integration-stream.xsd
		http://www.springframework.org/schema/beans https://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:service-activator input-channel="inputChannel"
					   ref="conditionalService"
					   method="testCircuitBreaker">
		<int:request-handler-advice-chain>
			<ref bean="circuitBreakerAdvice" />
		</int:request-handler-advice-chain>
	</int:service-activator>

	<int:channel id="failedChannel" />

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

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

	<bean id="circuitBreakerAdvice" class="org.springframework.integration.handler.advice.RequestHandlerCircuitBreakerAdvice">
		<property name="threshold" value="2" /> 			<!-- close after 2 failures -->
		<property name="halfOpenAfter" value="15000" />		<!-- half open after 15 seconds -->
	</bean>

</beans>
