spring-bootspring-jms

Spring Boot JMS No JmsTemplate bean available


I'm trying to send a message with JMS from my application.

I add in my pom

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jms</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.jms</groupId>
        <artifactId>jms</artifactId>
        <version>1.1</version>
    </dependency>

The spring getting started say

JmsTemplate and ConnectionFactory are created automatically by Spring Boot. In this case, the ActiveMQ broker runs embedded.

and in my batch writer

@Autowired
JmsTemplate jmsTemplate,

void writer(List<String> items) {
   jmsTemplate.convertAndSend(items);
}

But the bean JmsTemplate is not found

No qualifying bean of type 'org.springframework.jms.core.JmsTemplate' available: expected at least 1 bean which qualifies as autowire candidate

I tried to add an message converter in the @configuration

@Bean
public MessageConverter jacksonJmsMessageConverter() {
    MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
    converter.setTargetType(MessageType.TEXT);
    converter.setTypeIdPropertyName("_type");
    return converter;
}

I tried to add the @EnableJMS (even if it's just for listener...)

But it dosen't work...

I don't understand why, on tutorials it looks like easy...


Solution

  • To work we need to create a jmsTemplate bean

    @Bean
    public ConnectionFactory getConnectionFactory() {
        TibjmsConnectionFactory connectionFactory = new TibjmsConnectionFactory(urlBrocker);
        return connectionFactory;
    }
    
    @Bean
    public JmsTemplate jmsTemplate() {
        JmsTemplate template = new JmsTemplate();
        template.setConnectionFactory(getConnectionFactory());
        template.setPubSubDomain(false); // false for a Queue, true for a Topic
        return template;
    }