注释类型 EnableJms
@Target(TYPE) @Retention(RUNTIME) @Documented @Import(JmsBootstrapConfiguration.class) public @interface EnableJms
Enable JMS listener annotated endpoints that are created under the cover by aJmsListenerContainerFactory. To be used onConfigurationclasses as follows:@Configuration @EnableJms public class AppConfig { @Bean public DefaultJmsListenerContainerFactory myJmsListenerContainerFactory() { DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); factory.setConnectionFactory(connectionFactory()); factory.setDestinationResolver(destinationResolver()); factory.setSessionTransacted(true); factory.setConcurrency("5"); return factory; } // other @Bean definitions }TheJmsListenerContainerFactoryis responsible to create the listener container responsible for a particular endpoint. Typical implementations, as theDefaultJmsListenerContainerFactoryused in the sample above, provides the necessary configuration options that are supported by the underlyingMessageListenerContainer.@EnableJmsenables detection ofJmsListenerannotations on any Spring-managed bean in the container. For example, given a classMyService:package com.acme.foo; public class MyService { @JmsListener(containerFactory = "myJmsListenerContainerFactory", destination="myQueue") public void process(String msg) { // process incoming message } }The container factory to use is identified by thecontainerFactoryattribute defining the name of theJmsListenerContainerFactorybean to use. When none is set aJmsListenerContainerFactorybean with namejmsListenerContainerFactoryis assumed to be present.the following configuration would ensure that every time a
Messageis received on theDestinationnamed "myQueue",MyService.process()is called with the content of the message:@Configuration @EnableJms public class AppConfig { @Bean public MyService myService() { return new MyService(); } // JMS infrastructure setup }Alternatively, ifMyServicewere annotated with@Component, the following configuration would ensure that its@JmsListenerannotated method is invoked with a matching incoming message:@Configuration @EnableJms @ComponentScan(basePackages="com.acme.foo") public class AppConfig { }Note that the created containers are not registered against the application context but can be easily located for management purposes using theJmsListenerEndpointRegistry.Annotated methods can use flexible signature; in particular, it is possible to use the
Messageabstraction and related annotations, seeJmsListenerJavadoc for more details. For instance, the following would inject the content of the message and a custom "myCounter" JMS header:@JmsListener(containerFactory = "myJmsListenerContainerFactory", destination="myQueue") public void process(String msg, @Header("myCounter") int counter) { // process incoming message }These features are abstracted by theMessageHandlerMethodFactorythat is responsible to build the necessary invoker to process the annotated method. By default,DefaultMessageHandlerMethodFactoryis used.When more control is desired, a
@Configurationclass may implementJmsListenerConfigurer. This allows access to the underlyingJmsListenerEndpointRegistrarinstance. The following example demonstrates how to specify an explicit defaultJmsListenerContainerFactory@Configuration @EnableJms public class AppConfig implements JmsListenerConfigurer { @Override public void configureJmsListeners(JmsListenerEndpointRegistrar registrar) { registrar.setContainerFactory(myJmsListenerContainerFactory()); } @Bean public JmsListenerContainerFactory> myJmsListenerContainerFactory() { // factory settings } @Bean public MyService myService() { return new MyService(); } }For reference, the example above can be compared to the following Spring XML configuration:
It is also possible to specify a custom<beans> <jms:annotation-driven container-factory="myJmsListenerContainerFactory"/> <bean id="myJmsListenerContainerFactory" class="org.springframework.jms.config.DefaultJmsListenerContainerFactory"> // factory settings </bean> <bean id="myService" class="com.acme.foo.MyService"/> </beans>JmsListenerEndpointRegistryin case you need more control on the way the containers are created and managed. The example below also demonstrates how to customize theJmsHandlerMethodFactoryto use with a customValidatorso that payloads annotated withValidatedare first validated against a customValidator.@Configuration @EnableJms public class AppConfig implements JmsListenerConfigurer { @Override public void configureJmsListeners(JmsListenerEndpointRegistrar registrar) { registrar.setEndpointRegistry(myJmsListenerEndpointRegistry()); registrar.setMessageHandlerMethodFactory(myJmsHandlerMethodFactory); } @Bean public JmsListenerEndpointRegistry> myJmsListenerEndpointRegistry() { // registry configuration } @Bean public JmsHandlerMethodFactory myJmsHandlerMethodFactory() { DefaultJmsHandlerMethodFactory factory = new DefaultJmsHandlerMethodFactory(); factory.setValidator(new MyValidator()); return factory; } @Bean public MyService myService() { return new MyService(); } }For reference, the example above can be compared to the following Spring XML configuration:
Implementing<beans> <jms:annotation-driven registry="myJmsListenerEndpointRegistry" handler-method-factory="myJmsHandlerMethodFactory"/> <bean id="myJmsListenerEndpointRegistry" class="org.springframework.jms.config.JmsListenerEndpointRegistry"> // registry configuration </bean> <bean id="myJmsHandlerMethodFactory" class="org.springframework.messaging.handler.support.DefaultJmsHandlerMethodFactory"> <property name="validator" ref="myValidator"/> </bean> <bean id="myService" class="com.acme.foo.MyService"/> </beans>JmsListenerConfigureralso allows for fine-grained control over endpoints registration via theJmsListenerEndpointRegistrar. For example, the following configures an extra endpoint:@Configuration @EnableJms public class AppConfig implements JmsListenerConfigurer { @Override public void configureJmsListeners(JmsListenerEndpointRegistrar registrar) { SimpleJmsListenerEndpoint myEndpoint = new SimpleJmsListenerEndpoint(); // ... configure the endpoint registrar.registerEndpoint(endpoint, anotherJmsListenerContainerFactory()); } @Bean public MyService myService() { return new MyService(); } @Bean public JmsListenerContainerFactory> anotherJmsListenerContainerFactory() { // ... } // JMS infrastructure setup }Note that all beans implementingJmsListenerConfigurerwill be detected and invoked in a similar fashion. The example above can be translated in a regular bean definition registered in the context in case you use the XML configuration.- 从以下版本开始:
- 4.1
- 作者:
- Stephane Nicoll
- 另请参阅:
JmsListener,JmsListenerAnnotationBeanPostProcessor,JmsListenerEndpointRegistrar,JmsListenerEndpointRegistry