簡體   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