如何使用activemq camel组件

时间:2018-05-16 10:32:27

标签: apache-camel activemq apache-karaf blueprint-osgi camel-jms

在消耗来自JMS队列的消息时,我希望决定消费过程是成功还是失败,如果失败,我希望activemq在1分钟之后再次重新发送消息。稍后根据递送计数,我可以将其发送给DLQ。

我找到了与java代码实现相同的示例,例如具有不同确认模式的会话或者使会话成为交易但无法获得如何与蓝图文件实现相同。

     <reference id="testIdempotencyStore"
           interface="javax.sql.DataSource"
           filter="(osgi.jndi.service.name=TestContext)">
</reference>

<bean id="simulatorMessages" class="org.apache.camel.processor.idempotent.jdbc.JdbcMessageIdRepository">
    <argument ref="testIdempotencyStore" />
    <argument value="jmsTest" />
</bean>

<reference id="txMgr" interface="javax.transaction.TransactionManager" />

<bean id="xaConnectionFactory" class="org.apache.activemq.ActiveMQXAConnectionFactory">
    <property name="brokerURL" value="${activemq.url}" />
    <property name="watchTopicAdvisories" value="false" />
    <property name="userName" value="${activemq.user}" />
    <property name="password" value="${activemq.password}" />
    <property name="redeliveryPolicy" >
        <bean class="org.apache.activemq.RedeliveryPolicy" >
            <property name="maximumRedeliveries" value="0" />
        </bean>
    </property>
</bean>

<bean id="jcaConnectionFactory" class="org.apache.activemq.jms.pool.JcaPooledConnectionFactory"
      init-method="start" destroy-method="stop">
    <property name="transactionManager" ref="txMgr"/>
    <property name="maxConnections" value="10" />
    <property name="name" value="amq" />
    <property name="connectionFactory" ref="xaConnectionFactory"/>
</bean>

<bean id="jmsTxConf" class="org.apache.activemq.camel.component.ActiveMQConfiguration">
    <property name="connectionFactory" ref="jcaConnectionFactory" />
    <property name="requestTimeout" value="10000" />
    <property name="transactionTimeout" value="30" />
    <property name="cacheLevelName" value="CACHE_NONE" />
</bean>

<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent" >
    <property name="configuration" ref="jmsTxConf" />
</bean>

<camelContext id="TestContext-jms-dispatcher" trace="false" xmlns="http://camel.apache.org/schema/blueprint" >
    <route id="externalNotificationsDispatchRoute" >
        <from uri="activemq:queue:{{vqueue.name}}" />
        <idempotentConsumer messageIdRepositoryRef="simulatorMessages">
            <header>customId</header>
            <to uri="vm:notificationConsumer" />
        </idempotentConsumer>
    </route>
</camelContext>

1 个答案:

答案 0 :(得分:1)

如果您还没有这样做,请通读this chapter以了解有关JMS XA事务如何在JBoss Fuse中工作的更多信息。

你错过了Spring PlatformTransactionManager和其他一些事情。 以下是应该有效的配置示例:

<!-- Service reference to Spring’s PlatformTransactionManager -->
<reference id=”osgiPlatformTransactionManager”
    interface=”org.springframework.transaction.PlatformTransactionManager”>

<!-- Service reference to JTA TransactionManager -->
<reference id=”osgiJtaTransactionManager”
    interface=”javax.transaction.TransactionManager”>

<!-- JMS TX endpoint -->
<bean id=”activemq” class=”org.apache.activemq.camel.component.ActiveMQComponent”>
    <property name=”configuration” ref=”jmsTxConfig”>
</bean>

<bean id=”jmsTxConfig” class=”org.apache.camel.component.jms.JmsConfiguration”>
    <property name=”connectionFactory” ref=”jmsXaPoolConnectionFactory” />
    <property name=”transactionManager” ref=”osgiPlatformTransactionManager” />
    <!-- transacted must be set to “false” for XA transactions -->
    <!-- as it is only meant to be used with local transactions -->
    <property name=”transacted” value=”false” />
</bean>

<!-- The connection factory wrapper to enable auto-enlisting -->
<bean id=”jmsXaPoolConnectionFactory”
    class=”org.apache.activemq.pool.JcaPooledConnectionFactory”>
    <property name=”name” value=”MyXaResource” />
    <property name=”maxConnections” value=”1” />
    <property name=”connectionFactory” ref=”jmsXaConnectionFactory” />
    <property name=”transactionManager” ref=”osgiJtaTransactionManager” />
</bean>

<bean id=”jmsXaConnectionFactory”
    class=”org.apache.activemq.ActiveMQXAConnectionFactory”>
    <property name=”brokerURL” value=”${activemq.url}” />
    <property name=”userName” value=”${activemq.user}” />
    <property name=”password” value=”${activemq.password}” />
    <property name="watchTopicAdvisories" value="false" />
    <property name="redeliveryPolicy" >
    <bean class="org.apache.activemq.RedeliveryPolicy" >
        <property name="maximumRedeliveries" value="0" />
    </bean>
</property>
</bean>

<!-- ActiveMQ XA Resource Manager -->
<bean id=”resourceManager” class=”org.apache.activemq.pool.ActiveMQResourceManager”
    init-method=”recoverResource”>
    <property name=”transactionManager” ref=”osgiJtaTransactionManager” />
    <property name=”connectionFactory” ref=”jmsXaConnectionFactory” />
    <property name=”resourceName” value=”activemq.default” />
</bean>

<!-- Transaction Policy to be referenced by the transacted-component in a route -->
<bean id=”txPolicy” class=”org.apache.camel.spring.spi.SpringTransactionPolicy”>
    <property name=”transactionManager” ref=”osgiPlatformTransactionManager” />
    <!-- PROPAGATION_REQUIRED is the default value if a behavior is not set -->
    <property name=”propagationBehaviorName” value=”PROPAGATION_REQUIRED” />
</bean>