JMS (Java Message Service)

Spring 提供了一个 JMS 集成框架,它以与 Spring 集成 JDBC API 非常相似的方式简化了 JMS API 的使用。

Spring provides a JMS integration framework that simplifies the use of the JMS API in much the same way as Spring’s integration does for the JDBC API.

JMS 大致可分为两个功能区域,即生成和消费消息。JmsTemplate 类用于消息生成和同步消息接收。对于类似于 Jakarta EE 消息驱动 bean 风格的异步接收,Spring 提供了许多消息监听器容器,你可以使用它们来创建消息驱动 POJO (MDP)。Spring 还提供了创建消息监听器的声明方式。

JMS can be roughly divided into two areas of functionality, namely the production and consumption of messages. The JmsTemplate class is used for message production and synchronous message reception. For asynchronous reception similar to Jakarta EE’s message-driven bean style, Spring provides a number of message-listener containers that you can use to create Message-Driven POJOs (MDPs). Spring also provides a declarative way to create message listeners.

org.springframework.jms.core 包提供了使用 JMS 的核心功能。它包含 JMS 模板类,通过处理资源的创建和释放来简化 JMS 的使用,这与`JdbcTemplate`对 JDBC 的处理非常相似。Spring 模板类共同的设计原则是,提供帮助程序方法来执行通用操作,并且对于更复杂的使用情况,委托处理任务的主要部分给用户实现的回调接口。JMS 模板遵循相同的设计。这些类提供了多种便捷方法,用于发送消息、同步消费消息以及向用户公开 JMS 会话和消息生成器。

The org.springframework.jms.core package provides the core functionality for using JMS. It contains JMS template classes that simplify the use of the JMS by handling the creation and release of resources, much like the JdbcTemplate does for JDBC. The design principle common to Spring template classes is to provide helper methods to perform common operations and, for more sophisticated usage, delegate the essence of the processing task to user-implemented callback interfaces. The JMS template follows the same design. The classes offer various convenience methods for sending messages, consuming messages synchronously, and exposing the JMS session and message producer to the user.

org.springframework.jms.support 包提供了 JMSException 转换功能。转换将已检查的 JMSException 层次结构转换为未检查异常的镜像层次结构。如果存在已检查的 jakarta.jms.JMSException 的任何提供程序特定的子类,那么此异常将包装在未检查的 UncategorizedJmsException 中。

The org.springframework.jms.support package provides JMSException translation functionality. The translation converts the checked JMSException hierarchy to a mirrored hierarchy of unchecked exceptions. If any provider-specific subclasses of the checked jakarta.jms.JMSException exist, this exception is wrapped in the unchecked UncategorizedJmsException.

org.springframework.jms.support.converter 包提供了一个`MessageConverter` 抽象,用于在 Java 对象和 JMS 消息之间进行转换。

The org.springframework.jms.support.converter package provides a MessageConverter abstraction to convert between Java objects and JMS messages.

org.springframework.jms.support.destination 包提供了管理 JMS 目标的各种策略,例如为存储在 JNDI 中的目标提供服务定位器。

The org.springframework.jms.support.destination package provides various strategies for managing JMS destinations, such as providing a service locator for destinations stored in JNDI.

org.springframework.jms.annotation 包提供了使用 @JmsListener 支持注释驱动监听器端的必要基础架构。

The org.springframework.jms.annotation package provides the necessary infrastructure to support annotation-driven listener endpoints by using @JmsListener.

org.springframework.jms.config 包为 jms 命名空间提供了解析器实现,以及用于配置监听器容器和创建监听器端的 Java 配置支持。

The org.springframework.jms.config package provides the parser implementation for the jms namespace as well as the java config support to configure listener containers and create listener endpoints.

最后,org.springframework.jms.connection 包提供了一个适合在独立应用程序中使用的 ConnectionFactory 实现。它还包含一个针对 JMS 的 Spring PlatformTransactionManager 实现(巧妙地命名为`JmsTransactionManager`)。这允许将 JMS 无缝集成到 Spring 的事务管理机制中,作为事务资源。

Finally, the org.springframework.jms.connection package provides an implementation of the ConnectionFactory suitable for use in standalone applications. It also contains an implementation of Spring’s PlatformTransactionManager for JMS (the cunningly named JmsTransactionManager). This allows for seamless integration of JMS as a transactional resource into Spring’s transaction management mechanisms.

从 Spring Framework 5 开始,Spring 的 JMS 包完全支持 JMS 2.0,并要求运行时存在 JMS 2.0 API。我们建议使用与 JMS 2.0 兼容的提供程序。

As of Spring Framework 5, Spring’s JMS package fully supports JMS 2.0 and requires the JMS 2.0 API to be present at runtime. We recommend the use of a JMS 2.0 compatible provider.

如果您碰巧在系统中使用了较旧的消息代理,则可以尝试将您的现有代理生成升级到与 JMS 2.0 兼容的驱动程序。或者,您也可以尝试针对基于 JMS 1.1 的驱动程序运行,只需将 JMS 2.0 API jar 放在类路径上,但只对您的驱动程序使用与 JMS 1.1 兼容的 API。Spring 的 JMS 支持默认遵循 JMS 1.1 约定,因此通过相应的配置,它支持此类场景。但是,请仅将此视为过渡场景。

If you happen to use an older message broker in your system, you may try upgrading to a JMS 2.0 compatible driver for your existing broker generation. Alternatively, you may also try to run against a JMS 1.1 based driver, simply putting the JMS 2.0 API jar on the classpath but only using JMS 1.1 compatible API against your driver. Spring’s JMS support adheres to JMS 1.1 conventions by default, so with corresponding configuration it does support such a scenario. However, please consider this for transition scenarios only.