Camel JMS:如何在连接主题时配置自定义消息侦听器

时间:2014-04-02 15:55:06

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

我需要定义一个从主题中读取消息(有一个xml)并将其解组为java bean的路由。

早些时候我使用spring JmsTemplate来管理主题的connectionFactory,我的路线看起来像这样(并且它工作正常) 消息转换器本质上返回TextMessage方法

中的fromMessage()实例
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。

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

1 个答案:

答案 0 :(得分: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")
相关问题