<?xml version="1.0" encoding="UTF-8"?>
<!--
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
    this work for additional information regarding copyright ownership.
    The ASF licenses this file to You 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.
-->

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xmlns:broker="http://activemq.apache.org/schema/core"
       xsi:schemaLocation="
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
         http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.5.0.xsd">

  <bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL" value="tcp://localhost:51616"/>
  </bean>

  <bean id="jmsTransactionManager" class="org.springframework.jms.connection.JmsTransactionManager">
    <property name="connectionFactory" ref="jmsConnectionFactory"/>
  </bean>

  <bean id="jms" class="org.apache.activemq.camel.component.ActiveMQComponent">
    <property name="connectionFactory" ref="jmsConnectionFactory"/>
    <property name="transacted" value="true"/>
    <property name="transactionManager" ref="jmsTransactionManager"/>
    <property name="acceptMessagesWhileStopping" value="true"/>
  </bean>

  <!-- START SNIPPET: e1 -->
  <bean id="myPolicy" class="org.apache.camel.impl.ThrottlingInflightRoutePolicy">
    <!-- define the scope to be context scoped so we measure against total inflight exchanges
         that means for both route1, route2 and route3 all together -->
    <property name="scope" value="Context"/>
    <!-- when we hit > 20 inflight exchanges then kick in and suspend the routes -->
    <property name="maxInflightExchanges" value="20"/>
    <!-- when we hit lower than 10% of the max = 2 then kick in and resume the routes
         the default percentage is 70% but in this demo we want a low value -->
    <property name="resumePercentOfMax" value="10"/>
    <!-- output throttling activity at WARN level -->
    <property name="loggingLevel" value="WARN"/>
  </bean>
  <!-- END SNIPPET: e1 -->

  <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">

    <endpoint uri="seda:inbox?concurrentConsumers=25&amp;size=25000" id="foo"/>

    <route routePolicyRef="myPolicy">
      <from uri="jms:queue:inbox?concurrentConsumers=100"/>
      <transacted/>
      <to uri="log:+++JMS +++?groupSize=100"/>
      <to ref="foo"/>
    </route>

    <route routePolicyRef="myPolicy">
      <from uri="file://target/inbox?delete=true&amp;maxMessagesPerPoll=50"/>
      <to uri="log:+++FILE+++?groupSize=100"/>
      <to ref="foo"/>
    </route>

    <route>
      <from ref="foo"/>
      <delay>
        <constant>100</constant>
      </delay>
      <to uri="log:+++SEDA+++?groupSize=100"/>
    </route>

  </camelContext>

  <broker:broker useJmx="true" persistent="true" brokerName="localhost" dataDirectory="target/data"
                 useShutdownHook="true">
    <broker:transportConnectors>
      <broker:transportConnector name="tcp" uri="tcp://localhost:51616"/>
    </broker:transportConnectors>
  </broker:broker>

</beans>