简体   繁体   English

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

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

I need to define a route which reads message (has an xml) from a Topic and unmarshals it to a java bean. 我需要定义一条路由,该路由从Topic中读取消息(具有xml)并将其解组到Java bean。

Earlier i was using spring JmsTemplate for managing connectionFactory for the topic and my route looked something like this (and it worked fine) The message converter essetially returns the TextMessage instance in the fromMessage() method 早些时候,我使用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")

Now, instead of a JmsTemplate, i am using org.springframework.jms.listener.DefaultMessageListenerContainer to connect to this durable topic. 现在,我不是使用JmsTemplate,而是使用org.springframework.jms.listener.DefaultMessageListenerContainer连接到此持久主题。

(Also because it supports asynchronous mode) (也因为它支持异步模式)

For this i wrote my own message listener which implements javax.jms.MessageListener and i read the message in onMessage() . 为此,我编写了自己的消息侦听器,该消息侦听器实现了javax.jms.MessageListener并在onMessage()读取了消息。 But i cannot return a TextMessage from here like the way i used to do when is used JmsTemplate. 但是我不能像使用JmsTemplate时那样从这里返回TextMessage。

I don't know how can i configure this in the route definition so that it still supports unmarshalling? 我不知道如何在路由定义中配置它,使其仍然支持解组?

I tried a lot of things and the solution is really simple, there is no need to use `org.springframework.jms.listener.DefaultMessageListenerContainer, instead, all we need to do is to define a connectionFactory which in my case was the myJmsConnectionFactory instance defined as the following in the spring xml 我尝试了很多事情,并且解决方案非常简单,不需要使用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>

This connection factory uses a jndi template which helps in the jndi look up of remote objects. 该连接工厂使用jndi模板,该模板有助于jndi查找远程对象。 This is defined as 定义为

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

In the route definition, i just use this connection factory to lookup the remote topic. 在路由定义中,我只是使用此连接工厂来查找远程主题。 By default camel registers a message listener when you connect to a jms topic and you don't have to specify one (you can but i didn't need to :)) 默认情况下,当您连接到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