【问题标题】:Camel JMS : how to configure custom message listener when connecting to a topicCamel JMS:连接到主题时如何配置自定义消息侦听器
【发布时间】:2014-05-08 22:37:45
【问题描述】:

我需要定义一个从 Topic 读取消息(具有 xml)并将其解组到 java bean 的路由。

之前我使用 spring JmsTemplate 来管理主题的 connectionFactory,我的路线看起来像这样(它工作得很好) 消息转换器本质上在fromMessage() 方法中返回TextMessage 实例

JaxbDataFormat dataFormat = new JaxbDataFormat();
dataFormat.setContextPath("com.somepath.xml");

from("jms:topic:myTopic?transacted=true&connectionFactory=myJmsConnectionFactory&messageConverter=#myMessageConverter")
 .transacted()
 .unmarshal(dataFormat)
 .routeId("myRouteId")

现在,我使用 org.springframework.jms.listener.DefaultMessageListenerContainer 连接到这个持久主题,而不是 JmsTemplate。

(也是因为它支持异步模式)

为此,我编写了自己的消息侦听器,它实现了javax.jms.MessageListener,并阅读了onMessage() 中的消息。但是我不能像使用 JmsTemplate 时那样从这里返回 TextMessage。

我不知道如何在路由定义中配置它以便它仍然支持解组?

【问题讨论】:

    标签: java spring jms apache-camel spring-jms


    【解决方案1】:

    我尝试了很多东西,解决方案非常简单,不需要使用`org.springframework.jms.listener.DefaultMessageListenerContainer,相反,我们需要做的就是定义一个connectionFactory,在我的例子中是myJmsConnectionFactory 实例在spring xml中定义如下

    <bean id="myJmsConnectionFactory"        class="org.springframework.jndi.JndiObjectFactoryBean">
                <property name="jndiTemplate" ref="myJndiTemplate" />
                <property name="jndiName" value="TopicConnectionFactory" />
                <property name="lookupOnStartup" value="false" />
                <property name="proxyInterfaces" value="javax.jms.ConnectionFactory" />
                <property name="cache" value="true" />
    </bean>
    

    这个连接工厂使用一个 jndi 模板,它有助于 jndi 查找远程对象。这被定义为

    <bean id="myJndiTemplate" class="org.springframework.jndi.JndiTemplate">
                <property name="environment">
                    <bean class="org.springframework.beans.factory.config.PropertiesFactoryBean">
                        <property name="location" value="file:///opt/test/jndi.properties" />
                    </bean>
                </property>
    </bean>
    

    在路由定义中,我只是使用这个连接工厂来查找远程主题。默认情况下,当您连接到 jms 主题时,camel 会注册一个消息侦听器,并且您不必指定一个(您可以但我不需要:))

    JaxbDataFormat dataFormat = new JaxbDataFormat();
    dataFormat.setContextPath("com.somepath.xml");
    
    from("jms:topic:myTopic?transacted=true&connectionFactory=myJmsConnectionFactory")
     .transacted()
     .unmarshal(dataFormat)
     .routeId("myRouteId")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-09
      • 2013-10-09
      • 1970-01-01
      • 2019-03-24
      • 1970-01-01
      • 2011-01-20
      • 2018-01-31
      • 2021-04-23
      相关资源
      最近更新 更多