java Spring 中的多线程 JMS 接收

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4051318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:34:24  来源:igfitidea点击:

Multithreaded JMS receiving in Spring

javaspringjms

提问by Iker Jimenez

I'm trying to write a multithreaded implementation for JMS message processing from a queue.

我正在尝试为队列中的 JMS 消息处理编写多线程实现。

I've tried with DefaultMessageListenerContainer and SimpleMessageListenerContainer classes.

我已经尝试过 DefaultMessageListenerContainer 和 SimpleMessageListenerContainer 类。

The problem I have is that it seems like just a single instance of the MessageListener class gets ever instantiated, no matter how I configure it. This forces me to unnecessarily write stateless or thread-safe MessageListener implementations, since I have the ListenerContainer configured to use multiple threads (concurrentConsumers=8).

我遇到的问题是,无论我如何配置它,似乎都只是实例化了 MessageListener 类的一个实例。这迫使我不必要地编写无状态或线程安全的 MessageListener 实现,因为我将 ListenerContainer 配置为使用多个线程 (concurrentConsumers=8)。

Is there an obvious solution to this that I'm overlooking?

我忽略了这个问题的明显解决方案吗?

回答by skaffman

This is by design. The MessageListeneris a dependency that you inject into Spring - it has no way of instantiating new ones.

这是设计使然。这MessageListener是您注入 Spring 的依赖项 - 它无法实例化新的依赖项。

This forces me to unnecessarily write stateless or thread-safe messageListener implementations

这迫使我不必要地编写无状态或线程安全的 messageListener 实现

You make that sound like a bad thing. Making your MessageListeneris a very good idea, Spring just removes the temptation to do otherwise.

你让这听起来像是一件坏事。制作你的MessageListener是一个很好的主意,Spring 只是消除了做其他事情的诱惑。

回答by shrini1000

Maybe this answer is too late, but it may benefit others who're searching for it. In short, the answer is using CommonsPoolTargetSourceand ProxyFactoryBean.

也许这个答案为时已晚,但它可能会使其他正在寻找它的人受益。简而言之,答案是使用CommonsPoolTargetSourceand ProxyFactoryBean

Check out this link for details: http://forum.springsource.org/showthread.php?34595-MDB-vs-MDP-concurrency

查看此链接了解详情:http: //forum.springsource.org/showthread.php?34595-MDB-vs-MDP-concurrency

If you want to do something similar for topic, check this: https://stackoverflow.com/a/12668538/266103

如果您想为topic做类似的事情,请查看:https: //stackoverflow.com/a/12668538/266103

回答by James Selvakumar

Configuring "concurrentConsumers" is just enough to process messages concurrently. This doesn't mean you will have "n" instances of MessageListenerContainer. The MessageListenerContainer may span "tasks" internally to process the messages. Optionally, you may have to configure your logging accordingly to see the information associated with the underlying tasks/threads.

配置“concurrentConsumers”就足以并发处理消息。这并不意味着您将拥有“n”个 MessageListenerContainer 实例。MessageListenerContainer 可以在内部跨越“任务”来处理消息。或者,您可能必须相应地配置日志记录以查看与底层任务/线程相关联的信息。

See "Tuning JMS message consumption in Spring" for more details.

有关更多详细信息,请参阅“在 Spring 中调整 JMS 消息消耗”。