Spring Framework 中的逐步示例 JMS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/793042/
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-09-08 03:51:36  来源:igfitidea点击:

step by step Sample JMS in Spring Framework

springjms

提问by

JMS using in spring, how to config and what type of dependency to use

JMS在spring中的使用,如何配置以及使用什么类型的依赖

回答by nsayer

There are some caveats with Spring JMS.

Spring JMS 有一些注意事项。

  1. You absolutely must not use Spring JMS directly on a JMS connection factory. This is because Spring - particularly JmsTemplate - opens a connection, uses it for one message, then closes it. This is the correct pattern to use when the connection factory is, in fact, a connection pool. But if it is really just a connection factory, you're going to slaughter the server under load. This is normally only an issue when you're running a standalone application rather than inside of a J2EE container, which typically has resource adapters or other things that do pooling for you. Spring does supply a SingleConnectionFactory bean that will reuse a connection, but this is not the best solution when you're using a clustered server and want to load balance your connections and work.
  2. The Spring APIs are all designed around processing single messages at a time. In some cases, where you may be able to deal with a batch of messages, it may be preferable to use Spring to provide you with the connection factories and such, but roll your own code to actually do the message I/O. That way, you can, for example, set up a transacted session, process 100 messages, then commit the acknowledgment as a batch. That should reduce the workload on the server, assuming you can do so safely.
  1. 您绝对不能直接在 JMS 连接工厂上使用 Spring JMS。这是因为 Spring - 特别是 JmsTemplate - 打开一个连接,将它用于一条消息,然后关闭它。当连接工厂实际上是一个连接池时,这是使用的正确模式。但如果它真的只是一个连接工厂,你将在负载下屠杀服务器。这通常仅在您运行独立应用程序而不是在 J2EE 容器内部时才会出现问题,J2EE 容器通常具有资源适配器或其他为您进行池化的东西。Spring 确实提供了一个将重用连接的 SingleConnectionFactory bean,但是当您使用集群服务器并希望负载平衡您的连接和工作时,这不是最佳解决方案。
  2. Spring API 都是围绕一次处理单个消息而设计的。在某些情况下,您可能能够处理一批消息,最好使用 Spring 为您提供连接工厂等,但使用您自己的代码来实际执行消息 I/O。这样,例如,您可以设置事务处理会话,处理 100 条消息,然后将确认作为批处理提交。假设您可以安全地这样做,这应该会减少服务器上的工作量。

回答by Mattias Holmqvist

You could check out Spring in Action. It has a chapter about messaging using JMS from Spring which I found helpful.

您可以查看Spring in Action。它有一章关于使用 Spring 中的 JMS 进行消息传递,我发现它很有帮助。