Spring Boot JMSTemplate#
JMSTemplate with Embedded ActiveMQ#
@EnableJms and JmsListenerContainerFactory Configuration#
@EnableJms
public class JmsConfig {
@Bean
public JmsListenerContainerFactory<?> myFactory(
ConnectionFactory connectionFactory,
DefaultJmsListenerContainerFactoryConfigurer configurer) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
// This provides all boot's default to this factory, including the message converter
configurer.configure(factory, connectionFactory);
// You could still override some of Boot's default if necessary.
return factory;
}
@Bean
public MessageConverter jacksonJmsMessageConverter() {
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setTargetType(MessageType.TEXT);
converter.setTypeIdPropertyName("_type");
return converter;
}
}
@EnableJmsenable the@JmsListenerannotated endpoints that are created under the cover by aJmsListenerContainerFactory.The
JmsListenerContainerFactoryis responsible to create the listener container responsible for a particular endpoint. Typical implementations, as theDefaultJmsListenerContainerFactory, provides the necessary configuration options that are supported by the underlyingMessageListenerContainer.MappingJackson2MessageConverteris used to turn the payload of aMessagefrom serialized form to a typed Object and vice versa.We have configured
MessageType.TEXT. This message type can be used to transport text-based messages. When a client receives aTextMessage, it is in read-only mode. If a client attempts to write to the message at this point, aMessageNotWriteableExceptionis thrown.
JMS Message Receiver with @JmsListener#
@JmsListeneris a repeatable annotation so you can use it multiple times on same method to register several JMS destinations to the same method.
Results#
JmsControllerJmsReceiver
Consuming Messages from JMS Queue#
The @JmsListener annotation marks a method to be the target of a JMS message listener on the specified destination. Annotated JMS listener methods are allowed to have flexible signatures.
javax.jms.Sessionto get access to the JMS session.javax.jms.Messageor one of its subclasses to get access to the raw JMS message.org.springframework.messaging.Messageto use Spring’s messaging abstraction counterpart.org.springframework.messaging.handler.annotation.Payload@Payload-annotated method arguments, including support for validation.org.springframework.messaging.handler.annotation.Header@Header– annotated method arguments to extract specific header values, including standard JMS headers defined byorg.springframework.jms.support.JmsHeaders.org.springframework.messaging.handler.annotation.Headers@Headers– annotated method argument that must also be assignable tojava.util.Mapfor obtaining access to all headers.org.springframework.messaging.MessageHeadersarguments for obtaining access to all headers.org.springframework.messaging.support.MessageHeaderAccessorororg.springframework.jms.support.JmsMessageHeaderAccessorfor convenient access to all method arguments.