<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesource.org/schema/mule/core/2.0"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:spring="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jms="http://www.mulesource.org/schema/mule/jms/2.0"
       xmlns:jdbc="http://www.mulesource.org/schema/mule/jdbc/2.0"
       xmlns:jbossts="http://www.mulesource.org/schema/mule/jbossts/2.0"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
       http://www.mulesource.org/schema/mule/core/2.0 http://www.mulesource.org/schema/mule/core/2.0/mule.xsd
       http://www.mulesource.org/schema/mule/jms/2.0 http://www.mulesource.org/schema/mule/jms/2.0/mule-jms.xsd
       http://www.mulesource.org/schema/mule/jdbc/2.0 http://www.mulesource.org/schema/mule/jdbc/2.0/mule-jdbc.xsd
       http://www.mulesource.org/schema/mule/jbossts/2.0 http://www.mulesource.org/schema/mule/jbossts/2.0/mule-jbossts.xsd">

    <!-- 
    <mule-environment-properties synchronous="true" synchronousEventTimeout="5000">
       <threading-profile id="default" maxThreadsActive="1" maxThreadsIdle="1" maxBufferSize="1" doThreading="false" poolExhaustedAction="WAIT"/>
    </mule-environment-properties>
    -->

    <!-- JOTM Transaction manager -->
    <spring:bean name="ArjunaFactory" class="org.mule.module.jboss.transactions.JBossArjunaTransactionManagerFactory"/>

    <spring:bean id="arjunaTM"
        factory-bean="ArjunaFactory" factory-method="create"/>


    <context:property-placeholder location="classpath:derby.properties"/>

    <!-- Data Source -->
    <spring:bean id="jdbcDataSource"
        class="org.enhydra.jdbc.standard.StandardXADataSource"
        destroy-method="shutdown">
        <spring:property name="transactionManager">
            <spring:ref local="arjunaTM"/>
        </spring:property>
        <spring:property name="driverName">
            <spring:value>org.apache.derby.jdbc.EmbeddedDriver</spring:value>
        </spring:property>
        <spring:property name="url">
            <spring:value>${database.connection}</spring:value>
        </spring:property>
    </spring:bean>
    <!--
    <spring:bean id="jdbcDataSourceXa"
        class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"
        destroy-method="shutdown">
        <spring:property name="dataSource">
            <spring:ref local="innerDataSource"/>
        </spring:property>
        <spring:property name="maxSize">
            <spring:value>10</spring:value>
        </spring:property>
    </spring:bean>
    -->

    <!-- Transaction manager -->
    <jbossts:transaction-manager/>

    <custom-transaction-factory name="xaTxFactory" class="org.mule.transaction.XaTransactionFactory"/>

    <jms:connector name="jmsConnector" eagerConsumer="false">
        <jms:connection-factory class="org.apache.activemq.ActiveMQXAConnectionFactory">
            <property key="brokerURL" value="vm://localhost?broker.persistent=false&amp;broker.useJmx=false"/>
        </jms:connection-factory>
    </jms:connector>

    <jdbc:connector name="jdbcConnector" pollingFrequency="1000" dataSource-ref="jdbcDataSource">
        <receiver-threading-profile maxThreadsActive="2" maxThreadsIdle="2" maxBufferSize="1" doThreading="false" poolExhaustedAction="WAIT"/>
        <dispatcher-threading-profile maxThreadsActive="2" maxThreadsIdle="2" maxBufferSize="1" doThreading="false" poolExhaustedAction="WAIT"/>
        
        <jdbc:query key="getTest" value="SELECT ID, TYPE, DATA, ACK, RESULT FROM TEST WHERE TYPE = ${type} AND ACK IS NULL"/>
        <jdbc:query key="getTest.ack" value="UPDATE TEST SET ACK = ${function:NOW} WHERE ID = ${id} AND TYPE = ${type} AND DATA = ${data}" />
        <jdbc:query key="writeTest" value="INSERT INTO TEST(TYPE, DATA, ACK, RESULT) VALUES(${type}, ${payload}, NULL, NULL)" />
    </jdbc:connector>

    <model name="model">
        <service name="JdbcToJms">
            <inbound>
                <inbound-endpoint address="jdbc://getTest?type=1">
                    <transaction action="ALWAYS_BEGIN">
                        <transaction-factory ref="xaTxFactory"/>
                    </transaction>
                </inbound-endpoint>
            </inbound>
            <component class="org.mule.test.integration.transaction.XABridgeComponent"/>
            <outbound>
                <outbound-pass-through-router>
                    <outbound-endpoint address="jms://my.queue" />
                </outbound-pass-through-router>
            </outbound>
        </service>

        <service name="JmsToJdbc">
            <component class="org.mule.test.integration.transaction.XABridgeComponent"/>
            <inbound>
                <inbound-endpoint address="jms://my.queue?frequency=60000&amp;reuseSession=false">
                    <transaction action="ALWAYS_BEGIN">
                        <transaction-factory ref="xaTxFactory"/>
                    </transaction>
                </inbound-endpoint>
            </inbound>
            <outbound>
                <outbound-pass-through-router>
                    <outbound-endpoint address="jdbc://writeTest?type=2" />
                </outbound-pass-through-router>
            </outbound>
        </service>
    </model>
</mule>
