首页 > 解决方案 > Spring Boot JMS 没有可用的 JmsTemplate bean

问题描述

我正在尝试从我的应用程序中使用 JMS 发送消息。

我在我的pom中添加

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

春天开始说

JmsTemplate 和 ConnectionFactory 由 Spring Boot 自动创建。在这种情况下,ActiveMQ 代理嵌入运行。

在我的批量编写器中

@Autowired
JmsTemplate jmsTemplate,

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

但是找不到bean JmsTemplate

没有可用的“org.springframework.jms.core.JmsTemplate”类型的合格 bean:预计至少有 1 个有资格作为自动装配候选者的 bean

我试图在@configuration 中添加一个消息转换器

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

我尝试添加@EnableJMS(即使它只是用于监听器......)

但它不起作用...

我不明白为什么,在教程上看起来很简单......

标签: spring-bootspring-jms

解决方案


为了工作,我们需要创建一个 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;
}

推荐阅读