Spring-boot: Spring Boot + ActiveMq JmsTemplate @Autowire error

Created on 19 Feb 2019  路  4Comments  路  Source: spring-projects/spring-boot

I keep getting this error whenever I run my spring boot app.

Field msgSender in com.mruv.activemqconf.service.SystemUserService required a bean of type 'org.springframework.jms.core.JmsTemplate' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'org.springframework.jms.core.JmsTemplate' in your configuration.

I believe that I dont have to create a @Bean of type JmsTemplate because autoconfigution is enabled.

Here are my dependencies.

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
    </dependencies>

Here is my configuration class.

@Configuration
@EnableJms
public class ActiveMqConf {

    @Bean
    public JmsListenerContainerFactory<?> containerFactory(ConnectionFactory connectionFactory,
            DefaultJmsListenerContainerFactoryConfigurer configurer) {

        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
    }

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

The JmsTemplate instance is created here.

@Service
public class SystemUserService {

    @Autowired
    private JmsTemplate msgSender;
    @Autowired
    private SystemUserRepository repository;

    public void postOne(SystemUser user) {
        // enqueue --> send to queue
        msgSender.convertAndSend(JmsReceiver.Q_ONE, user);
    }

    public List<SystemUser> getAll() {
        return repository.findAll();
    }
}

Here is the JmsListener.

@Component
public class JmsReceiver {

    public static final String Q_ONE = "Q_One";

    @Autowired
    private SystemUserRepository repository;

    @JmsListener(destination = JmsReceiver.Q_ONE)
    public void receive(@Payload SystemUser user) {
        repository.save(user);
    }
}

The project was working just fine until recently. Any help will be appreciated.

stackoverflow

Most helpful comment

Spring boot dropped autoconfiguration support for activemq-pool in favour of pooled-jms. Check it out here. So, just drop the activemq-pool dependency and add this

<dependency>
    <groupId>org.messaginghub</groupId>
    <artifactId>pooled-jms</artifactId>
</dependency>

All 4 comments

Thanks for the sample. "Working fine until recently" is vague and not really helping, please help us helping you by providing accurate information. I've also replaced your MySQL hardcoded settings by H2 as they are unrelated to the issue you are reporting and doesn't force me to have MySQL configured to run the sample.

Consider reviewing this section of the documenation. If you enable debug mode you can see that no ConnectionFactory is created.

ActiveMQConnectionFactoryConfiguration.PooledConnectionFactoryConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required classes 'org.messaginghub.pooled.jms.JmsPoolConnectionFactory', 'org.apache.commons.pool2.PooledObject' (OnClassCondition)

   ActiveMQConnectionFactoryConfiguration.SimpleConnectionFactoryConfiguration:
      Did not match:
         - @ConditionalOnProperty (spring.activemq.pool.enabled=false) found different value in property 'enabled' (OnPropertyCondition)
      Matched:
         - @ConditionalOnClass found required class 'org.springframework.jms.connection.CachingConnectionFactory' (OnClassCondition)

   ActiveMQXAConnectionFactoryConfiguration:
      Did not match:
         - @ConditionalOnBean (types: org.springframework.boot.jms.XAConnectionFactoryWrapper; SearchStrategy: all) did not find any beans of type org.springframework.boot.jms.XAConnectionFactoryWrapper (OnBeanCondition)
      Matched:
         - @ConditionalOnClass found required class 'javax.transaction.TransactionManager' (OnClassCondition)

You are configuring connection pooling but there are no candidates on the classpath. I don't know if this sample is representative of your actual situation but if it is, pooling never worked for you as far as I know. In 2.1, we're using a different library that is JMS 2.0 compliant, there is a section in the release notes.

In the future, please ask questions on StackOverflow, as mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.

Adding the following dependency fixed the problem.
```

org.messaginghub
pooled-jms

````

Thank you for providing the links above.

i also get this problem,then i change the springboot parent version,i find i use the 2.1.0.RELEASE version or later will be wrong, then i use 2.0.5.RELEASE fix the problem

Spring boot dropped autoconfiguration support for activemq-pool in favour of pooled-jms. Check it out here. So, just drop the activemq-pool dependency and add this

<dependency>
    <groupId>org.messaginghub</groupId>
    <artifactId>pooled-jms</artifactId>
</dependency>
Was this page helpful?
0 / 5 - 0 ratings