繁体   English   中英

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

[英]Camel JMS : how to configure custom message listener when connecting to a topic

我需要定义一条路由,该路由从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")

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

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

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

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

我尝试了很多事情,并且解决方案非常简单,不需要使用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主题时,骆驼会注册一个消息侦听器,而您不必指定一个(可以,但是我不需要:))

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM