Spring 集成 - 配置 DefaultMessageListenerContainer 来解析多个目的地

发布于 2024-12-22 15:10:14 字数 97 浏览 0 评论 0原文

我需要配置 DefaultMessageListenerContainer 来解析多个目标主题。我不想实例化多个容器,我想知道如何配置它,或者即使它是可能的。

谢谢

I have a requirement to configure a DefaultMessageListenerContainer to resolve multiple destination topics. I don't want to instanciate multiple containers and I was wondering how to configure this or even if it is possible.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静若繁花 2024-12-29 15:10:14

如果您希望使用直接 Spring JMS 侦听来自多个主题的消息,则需要创建此类的多个实例。唯一的问题是,默认情况下每个任务都会创建自己的 TaskExecutor,这有点浪费。要解决这个问题,只需在上下文中定义您自己的 taskExecutor bean,所有消息侦听器都会自动使用它。

这是一个在独立虚拟机中运行良好的任务执行器,还有与不同容器的本机工作管理器/线程池功能集成的替代实现。

<bean id="taskExecutor" class="org.springframework.scheduling.quartz.SimpleThreadPoolTaskExecutor">
    <property name="waitForJobsToCompleteOnShutdown" value="true"/>
    <property name="threadCount" value="20"/>
    <property name="threadNamePrefix" value="JmsConsumer"/>
</bean>

或者,使用 Spring Integration,只需创建一个消息通道和多个 - 每个主题一个。下面是一个示例片段:

<bean id="connectionFactory" class="..."/>

<int:channel id="allMessages"/>

<jms:message-driven-channel-adapter channel="allMessages" connection-factory="connectionFactory" destination-name="T.topic1"/>
<jms:message-driven-channel-adapter channel="allMessages" connection-factory="connectionFactory" destination-name="T.topic2"/>
<jms:message-driven-channel-adapter channel="allMessages" connection-factory="connectionFactory" destination-name="T.topic3"/>

<int:service-activator input-channel="allMessages" output-channel="results" ref="messageProcessingBean" method="onMessage"/>

在幕后,Spring Integration 将自动创建一个 MessageListenerContainer。

另一种替代方法是使用 JMS 提供者/MOM 的路由工具来创建路由所有相关消息的单一目标。例如,在 RabbitMQ 中,您可以创建一个绑定到多个交换的主题,因此只有一个 AMQP 目的地可供使用。然而,这是依赖于基础设施的,并且对于任何给定的 JMS 提供者来说都是不可能的。

If you wish to listen for messages from multiple topics using straight Spring JMS, you will need to create multiple instances of this class. The only issue is that by default each will creates its own TaskExecutor, which is a little bit wasteful. To get around this simply define your own taskExecutor bean in the context and all the message listeners will automatically use it.

Here is a task executor which works well in a standalone VM, there are alternative implementations which integrate with different containers' native work manager / thread pool capabilities.

<bean id="taskExecutor" class="org.springframework.scheduling.quartz.SimpleThreadPoolTaskExecutor">
    <property name="waitForJobsToCompleteOnShutdown" value="true"/>
    <property name="threadCount" value="20"/>
    <property name="threadNamePrefix" value="JmsConsumer"/>
</bean>

Alternatively, using Spring Integration, simply create a message channel and multiple <jms:message-driven-channel-adapter/>'s - one for each topic. Here is a sample snippet:

<bean id="connectionFactory" class="..."/>

<int:channel id="allMessages"/>

<jms:message-driven-channel-adapter channel="allMessages" connection-factory="connectionFactory" destination-name="T.topic1"/>
<jms:message-driven-channel-adapter channel="allMessages" connection-factory="connectionFactory" destination-name="T.topic2"/>
<jms:message-driven-channel-adapter channel="allMessages" connection-factory="connectionFactory" destination-name="T.topic3"/>

<int:service-activator input-channel="allMessages" output-channel="results" ref="messageProcessingBean" method="onMessage"/>

Behind the scenes, Spring Integration will automatically create a MessageListenerContainer.

Yet another alternative would be to use the routing facilities of your JMS provider/MOM to create a single destination where all relevant messages are routed. E.g. in RabbitMQ you can create a topic which is bound to multiple exchanges, and thus only one AMQP destination to consume. This is infrastructure dependent however, and will not be possible with any given JMS provider.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文