Support for JCA Message Endpoints

从版本 2.5 开始,Spring 还支持基于 JCA 的 MessageListener 容器。JmsMessageEndpointManager 尝试根据提供者的 ResourceAdapter 类名自动确定 ActivationSpec 类名。因此,通常可以提供 Spring 的通用 JmsActivationSpecConfig,如下例所示:

Beginning with version 2.5, Spring also provides support for a JCA-based MessageListener container. The JmsMessageEndpointManager tries to automatically determine the ActivationSpec class name from the provider’s ResourceAdapter class name. Therefore, it is typically possible to provide Spring’s generic JmsActivationSpecConfig, as the following example shows:

  • Java

  • Kotlin

  • Xml

@Bean
public JmsMessageEndpointManager jmsMessageEndpointManager(ResourceAdapter resourceAdapter,
		MessageListener myMessageListener) {

	JmsActivationSpecConfig specConfig = new JmsActivationSpecConfig();
	specConfig.setDestinationName("myQueue");

	JmsMessageEndpointManager endpointManager = new JmsMessageEndpointManager();
	endpointManager.setResourceAdapter(resourceAdapter);
	endpointManager.setActivationSpecConfig(specConfig);
	endpointManager.setMessageListener(myMessageListener);
	return endpointManager;
}
@Bean
fun jmsMessageEndpointManager(
	resourceAdapter: ResourceAdapter, myMessageListener: MessageListener) = JmsMessageEndpointManager().apply {
		setResourceAdapter(resourceAdapter)
		activationSpecConfig = JmsActivationSpecConfig().apply {
			destinationName = "myQueue"
		}
		messageListener = myMessageListener
	}
<bean class="org.springframework.jms.listener.endpoint.JmsMessageEndpointManager">
	<property name="resourceAdapter" ref="resourceAdapter"/>
	<property name="activationSpecConfig">
		<bean class="org.springframework.jms.listener.endpoint.JmsActivationSpecConfig">
			<property name="destinationName" value="myQueue"/>
		</bean>
	</property>
	<property name="messageListener" ref="myMessageListener"/>
</bean>

或者,你可以使用给定的 ActivationSpec 对象设置 JmsMessageEndpointManagerActivationSpec 对象也可能来自 JNDI 查找(使用 <jee:jndi-lookup>)。以下示例展示了如何执行此操作:

Alternatively, you can set up a JmsMessageEndpointManager with a given ActivationSpec object. The ActivationSpec object may also come from a JNDI lookup (using <jee:jndi-lookup>). The following example shows how to do so:

  • Java

  • Kotlin

  • Xml

@Bean
JmsMessageEndpointManager jmsMessageEndpointManager(ResourceAdapter resourceAdapter,
		MessageListener myMessageListener) {

	ActiveMQActivationSpec spec = new ActiveMQActivationSpec();
	spec.setDestination("myQueue");
	spec.setDestinationType("jakarta.jms.Queue");

	JmsMessageEndpointManager endpointManager = new JmsMessageEndpointManager();
	endpointManager.setResourceAdapter(resourceAdapter);
	endpointManager.setActivationSpec(spec);
	endpointManager.setMessageListener(myMessageListener);
	return endpointManager;
}
@Bean
fun jmsMessageEndpointManager(
	resourceAdapter: ResourceAdapter, myMessageListener: MessageListener) = JmsMessageEndpointManager().apply {
		setResourceAdapter(resourceAdapter)
		activationSpec = ActiveMQActivationSpec().apply {
			destination = "myQueue"
			destinationType = "jakarta.jms.Queue"
		}
		messageListener = myMessageListener
	}
<bean class="org.springframework.jms.listener.endpoint.JmsMessageEndpointManager">
	<property name="resourceAdapter" ref="resourceAdapter"/>
	<property name="activationSpec">
		<bean class="org.apache.activemq.ra.ActiveMQActivationSpec">
			<property name="destination" value="myQueue"/>
			<property name="destinationType" value="jakarta.jms.Queue"/>
		</bean>
	</property>
	<property name="messageListener" ref="myMessageListener"/>
</bean>

有关更多详细信息,请参阅 Javadoc JmsMessageEndpointManager、https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/jms/listener/endpoint/JmsActivationSpecConfig.html[JmsActivationSpecConfig]和 ResourceAdapterFactoryBean

Spring 还提供了一个通用的 JCA 消息端点管理器,它不受 JMS 约束: org.springframework.jca.endpoint.GenericMessageEndpointManager。这个组件允许使用任何消息侦听器类型(比如一个 JMS MessageListener)和任何特定于提供程序的 ActivationSpec`对象。请参阅 JCA 提供程序的文档以了解连接器的实际功能,并参阅 `GenericMessageEndpointManager 以获取特定于 Spring 的配置详细信息。

Spring also provides a generic JCA message endpoint manager that is not tied to JMS: org.springframework.jca.endpoint.GenericMessageEndpointManager. This component allows for using any message listener type (such as a JMS MessageListener) and any provider-specific ActivationSpec object. See your JCA provider’s documentation to find out about the actual capabilities of your connector, and see the GenericMessageEndpointManager javadoc for the Spring-specific configuration details.

基于 JCA 的消息端点管理与 EJB 2.1 消息驱动 Bean 非常类似。它使用相同的底层资源提供程序契约。与 EJB 2.1 MDB 一样,你也可以在 Spring 上下文中使用 JCA 提供程序支持的任何消息监听器接口。尽管如此,Spring 仍然提供对 JMS 的显式 “convenience” 支持,因为 JMS 是与 JCA 端点管理契约结合使用最常见的端点 API。

JCA-based message endpoint management is very analogous to EJB 2.1 Message-Driven Beans. It uses the same underlying resource provider contract. As with EJB 2.1 MDBs, you can use any message listener interface supported by your JCA provider in the Spring context as well. Spring nevertheless provides explicit “convenience” support for JMS, because JMS is the most common endpoint API used with the JCA endpoint management contract.