spring-boot - 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(即使它只是用于监听器......)
但它不起作用...
我不明白为什么,在教程上看起来很简单......
解决方案
为了工作,我们需要创建一个 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;
}
推荐阅读
- outlook-web-addins - 在桌面加载项事件激活中添加标头以获取 POST 请求
- docker - 如何授予系统用户在 docker 容器中运行命令的正确权限?
- python-3.x - Python 请求有时比平时花费更多时间
- backup - 使用 RSYNC 时向文件夹添加最后一个点
- reactjs - 使用嵌套级别反应本机导航抽屉
- python - 基于 JSON 列表的肌酸新列
- variant - 如何用特定突变标记系统发育树的分支?
- html - CSS:动画部分线性渐变背景
- java - 如何按需遍历列表列表?
- wpf - WPF 控件样式 - 如何在单独的 ResourceDictionary 文件中覆盖全局控件样式 ResourceDictionary 文件中的单个设置器值