如何在 camel 中实现路由以从 JMS 队列接收消息?

How do I implement a route in camel to receive messages from a JMS queue?

我参考了 Camel 文档的 JMS 页面和许多相关的 SO 问题 such as this one,但我无法找到有关实现的完整列表。

我在服务器上使用 Spring XML 以及 Camel 和 Weblogic。我用以下名称创建了一个测试队列:

服务器:TestJMSServer,模块:TestJMSModule,队列:TestJMSQueue,CF:TestConnectionFactory。

根据 Camel 文档,我的路线应该如下所示:

<camel:route id="test">
        <camel:from uri="jms:TestJMSQueue" />
        <camel:to uri="file:/Users/...." />
</camel:route>

这给了我一个错误 "connectionFactory must be specified"。那么,为了收听这个队列,我还需要向我的 applicationContext.xml 添加什么?

您需要告诉 Camel 的 jms-component 使用哪个 JMS 连接工厂。如果您使用的是 WebLogic,您很可能会从 jndi 获得它。

在下面的示例中,我正在使用 spring 的 jee:jndi-lookup 查找连接工厂(我相信这甚至可能是您可以在 WebLogic 中使用的名称)。然后将查找到的工厂作为 ID 为 myConnectionFactory.

的 spring bean 提供。

此连接工厂 bean 然后用于 camel 的 connectionFactory 属性 JmsComponent。注意 id 属性:jms。这定义了要在您的路由中使用的骆驼端点 uri 方案。

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
       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">


    <jee:jndi-lookup id="myConnectionFactory" jndi-name="jms/connectionFactory"/>

    <route id="test" xmlns="http://camel.apache.org/schema/spring">
        <from uri="jms:TestJMSQueue"/>
        <to uri="file:/Users/...."/>
    </route>


    <bean id="jms" class="org.apache.camel.component.jms.JmsComponent">
        <property name="connectionFactory" ref="myConnectionFactory"/>
        <!-- more configuration required based on your requirements -->
    </bean>

    <!--
    example uses  invm amq broker:

    <bean id="anothercnf" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="vm://mybroker"/>
    </bean>
    -->
</beans>

重要说明:您需要进一步调整(设置事务、设置并发消费者、可能配置 spring jms 连接池)