alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

ActiveMQ example source code file (jmsXajdbc.xml)

This example ActiveMQ source code file (jmsXajdbc.xml) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - ActiveMQ tags/keywords

apache, asf, asf, jdbc, jta, license, license, see, setup, setup, the, xa, you, you

The ActiveMQ jmsXajdbc.xml source code

<!--
    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.
-->
<!-- START SNIPPET: example -->

<beans xmlns="http://www.springframework.org/schema/beans"
       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-2.0.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.xsd
    ">

        <!-- build broker in code so it can be restarted and modified to test recovery -->

        <!-- some logging that can help
          log4j.logger.org.apache.activemq.TransactionContext=TRACE
          log4j.logger.org.springframework.transaction.support.AbstractPlatformTransactionManager=TRACE
          log4j.logger.org.apache.geronimo.transaction.manager=TRACE
          log4j.logger.org.enhydra.jdbc=TRACE
        -->

		<!-- XID factory -->
		<bean id="xidFactory" class="org.apache.geronimo.transaction.manager.XidFactoryImpl" />

		<!-- Transaction log -->
		<bean id="transactionLog" class="org.jencks.factory.HowlLogFactoryBean">	
			<property name="logFileDir" value="target/data/howl/txlog"/>
			<property name="xidFactory" ref="xidFactory"/>
		</bean>

		<!-- Setup the geronimo transaction manager -->
		<bean id="jenckTransactionManager" class="org.jencks.factory.TransactionManagerFactoryBean">
			<property name="transactionLog" ref="transactionLog"/>
		</bean>

		<!-- Configure the Spring framework to use JTA transactions from Geronimo -->
		<bean id="jtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
			<property name="transactionManager" ref="jenckTransactionManager"/>
		</bean>

		<!-- Using the jencks ActiveMQ pool to enable XA -->
		<bean id="jmsXaConnectionFactory" class="org.jencks.amqpool.XaPooledConnectionFactory">
	    	<constructor-arg value="tcp://localhost:61616" />
	    	<property name="maxConnections" value="8" />
	    	<property name="transactionManager" ref="jenckTransactionManager" />
		</bean>

		<!-- Define the activemq Camel component so we can integrate with the AMQ broker -->
		<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">
			<property name="transacted" value="true"/>
			<property name="transactionManager" ref="jtaTransactionManager"/>
			<property name="connectionFactory" ref="jmsXaConnectionFactory"/>
            <!-- cache level is important, can be cache connection or none, as session needs to be enlisted
            in the current transaction they can't be cached, with default cache sessions, they are created
            up front, before the transaction (required for the route) -->
            <property name="cacheLevel" value="0" />
		</bean>

	  <!-- Setup the connection manager -->
	  <bean id="connectionManager" class="org.jencks.factory.ConnectionManagerFactoryBean">
	    <property name="transactionManager" ref="jenckTransactionManager" />
	    <property name="transaction" value="xa" />
	  </bean>

	  <!-- Setup the JDBC Managed Connection Factory (that supports XA) -->
	  <!--bean id="jdbcManagedConnectionFactory" class="org.jencks.tranql.XAPoolDataSourceMCF">
	    <property name="driverName" value="com.mysql.jdbc.Driver"/>
	    <property name="url" value="jdbc:mysql://localhost/ScpBuffer?relaxAutoCommit=true"/>
	    <property name="user" value="rails"/>
	    <property name="password" value="rails"/>
	  </bean -->
	
	  <bean id="jdbcEnhydraXaDataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
	        <property name="dataSource">
	            <bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">

	                <!-- property name="driverName" value="com.mysql.jdbc.Driver" />
	                <property name="url" value="jdbc:mysql://localhost/ScpBuffer?relaxAutoCommit=true" / -->
                    <!-- try embedded derby xa -->
                    <property name="driverName" value="org.apache.derby.jdbc.EmbeddedDriver" />
                    <property name="url" value="jdbc:derby:target/XatestDs;create=true" />
                    <property name="transactionManager" ref="jenckTransactionManager" />
	            </bean>
	        </property>
            <property name="transactionManager" ref="jenckTransactionManager" />
	   </bean>

		<bean id="required" class="org.apache.camel.spring.spi.SpringTransactionPolicy">
			<property name="transactionManager" ref="jenckTransactionManager"/>
			<property name="propagationBehaviorName" value="PROPAGATION_REQUIRED"/>
		</bean>

		<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
				<route id="queueToDbTransacted">
					<from uri="activemq:queue:scp_transacted"/>
                    <transacted ref="required"/>
					<convertBodyTo type="java.lang.String"/>
					<to uri="log:BeforeSettingBody?showAll=true"/>
					<setBody>
						<simple>INSERT INTO SCP_INPUT_MESSAGES(messageId, messageCorrelationId, messageContent) VALUES('${in.header.JMSMessageId}','${in.header.JMSCorrelationId}','${in.body}')
					</setBody>
					<to uri="jdbc:jdbcEnhydraXaDataSource"/>
				</route>
			</camelContext>

</beans>

Other ActiveMQ examples (source code examples)

Here is a short list of links related to this ActiveMQ jmsXajdbc.xml source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.