<?xml version="1.0" encoding="UTF-8"?>
<!--
    R Service Bus
    
    Copyright (c) Copyright of OpenAnalytics BVBA, 2010-2014
 
    ===========================================================================
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.
 
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
        
    @author rsb.development@openanalytics.eu
-->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:jms="http://www.springframework.org/schema/jms"
    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.xsd
        http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd">
    
    <bean name="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"
        p:connectionFactory-ref="amqConnectionFactory" p:explicitQosEnabled="true"
        p:deliveryPersistent="true" p:sessionTransacted="true"
        p:receiveTimeout="#{configuration.jobTimeOut}" />

    <!--  Global Job Workers -->
    <jms:listener-container connection-factory="amqConnectionFactory"
        acknowledge="transacted" concurrency="#{configuration.numberOfConcurrentJobWorkersPerQueue}">
        <jms:listener id="globalJobMessageListener" destination="r.jobs.>" ref="jobProcessor"
            method="process" />
    </jms:listener-container>
    
    <!-- Example of application biased Job Workers:
         PriviApp has 3 workers reserved to it, all other applications share the remaining workers -->
    <!--
    <jms:listener-container connection-factory="amqConnectionFactory"
        acknowledge="transacted" concurrency="3">
        <jms:listener id="priviAppJobMessageListener" destination="r.jobs.>" ref="jobProcessor"
            method="process"
            selector="#{T(eu.openanalytics.rsb.Constants).APPLICATION_NAME_MESSAGE_HEADER} = 'PriviApp'"  />
    </jms:listener-container>
    <jms:listener-container connection-factory="amqConnectionFactory"
        acknowledge="transacted" concurrency="#{configuration.numberOfConcurrentJobWorkersPerQueue - 3}">
        <jms:listener id="globalJobMessageListener" destination="r.jobs.>" ref="jobProcessor"
            method="process"
            selector="#{T(eu.openanalytics.rsb.Constants).APPLICATION_NAME_MESSAGE_HEADER} &lt;&gt; 'PriviApp'"  />
    </jms:listener-container>
    -->
    
    <!-- Result Processors -->
    <jms:listener-container connection-factory="amqConnectionFactory"
        acknowledge="transacted">
        <jms:listener id="restResultMessageListener" destination="r.results.>" ref="restResultProcessor"
            method="process"
            selector="#{T(eu.openanalytics.rsb.Constants).SOURCE_MESSAGE_HEADER} = '#{T(eu.openanalytics.rsb.message.AbstractWorkItem$Source).REST}'" />
    </jms:listener-container>

    <jms:listener-container connection-factory="amqConnectionFactory"
        acknowledge="transacted">
        <jms:listener id="directoryResultMessageListener" destination="r.results.>" ref="directoryDepositHandler"
            method="handleResult"
            selector="#{T(eu.openanalytics.rsb.Constants).SOURCE_MESSAGE_HEADER} = '#{T(eu.openanalytics.rsb.message.AbstractWorkItem$Source).DIRECTORY}'" />
    </jms:listener-container>

    <jms:listener-container connection-factory="amqConnectionFactory"
        acknowledge="transacted">
        <jms:listener id="emailResultMessageListener" destination="r.results.>" ref="emailDepositHandler"
            method="handleResult"
            selector="#{T(eu.openanalytics.rsb.Constants).SOURCE_MESSAGE_HEADER} = '#{T(eu.openanalytics.rsb.message.AbstractWorkItem$Source).EMAIL}'" />
    </jms:listener-container>

    <!-- Dead Letter Queue Undertaker -->
    <jms:listener-container connection-factory="amqConnectionFactory"
        acknowledge="transacted">
        <jms:listener id="dlqMessageListener" destination="DLQ.r.jobs.*" ref="dlqHandler"
            method="handle" />
    </jms:listener-container>
</beans>
