Springboot JMS

A production JMS server must be a stand alone server. What we have here is for demonstration purposes.

Add dependency


Add configuration class

public class JmsConfig {

    public JmsListenerContainerFactory<?> jmsFactory(ConnectionFactory connectionFactory,DefaultJmsListenerContainerFactoryConfigurer configurer) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
    @Bean // Serialize message content to json using TextMessage
    public MessageConverter jacksonJmsMessageConverter() {
        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
        return converter;

Add a Sender

public class Sender {

    private JmsTemplate jmsTemplate;

    private String      queue;

    public void sendMail(Mail mail) {
        try {
            jmsTemplate.convertAndSend(queue, mail);
            log.info("Mail sent to queue, {}, at {}", queue, LocalDateTime.now());
        } catch (Exception e) {
            log.error("Mail not sent");
            log.error("Exception, msg: {}", e.getMessage());



Add a Receiver

public class Receiver {

    @JmsListener(destination = "${queue.sender-receiver}", containerFactory = "jmsFactory")
    public void receiveMessage(Mail mail) {
        log.info("Received mail {}", LocalDateTime.now());
        try {
            log.info("mail={}", ObjectUtils.toJson(mail));
        } catch (Exception e) {



Source code on Github

Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!


Leave a Reply

Your email address will not be published. Required fields are marked *