尝试在 Spring Boot 中访问测试嵌入式 ActiveMQ Artemis 服务器时,遇到 jakarta.jms.JMSException: Failed to create session factory 错误。这是我的设置:
jakarta.jms.JMSException:无法创建会话工厂
连接工厂 Bean:
@Bean
public ConnectionFactory connectionFactory() throws JMSException {
return new ActiveMQConnectionFactory("vm://localhost");
}
测试代码:
@SpringBootTest
@ContextConfiguration(classes = {JmsConfig.class, JmsListenerTest.TestConfig.class})
@EnableAutoConfiguration
public class JmsListenerTest {
@Configuration
static class TestConfig {
@Bean
public ConnectionFactory connectionFactory() throws JMSException {
return new ActiveMQConnectionFactory("vm://localhost");
}
}
@Autowired
private JmsTemplate jmsTemplate;
@SpyBean
private NotifierMessageListener listener;
@Test
public void receive() {
final var message = new NacqMessage("s2", "receive test");
jmsTemplate.send(NotifierMessageListener.MAILBOX_JSON, s -> s.createObjectMessage(message)); //throws Failed to create session factory
...
}
}
相关gradle依赖:
implementation 'org.springframework.boot:spring-boot-starter-artemis'
implementation 'org.springframework:spring-jms'
testImplementation 'org.apache.activemq:artemis-jakarta-server:2.32.0' //not sure if needed.
此问题仅在嵌入式服务器上出现,而使用非嵌入式服务器则可以正常工作。我参考了官方文档,但没有找到有关正确配置嵌入式服务器的足够指导。在这种情况下可能需要什么额外的配置?
官方文档参考:在Artemis页面上使用ActiveMQConnectionFactory
URL
vm://localhost
无效。虚拟机内连接器必须引用服务器正在侦听的ID。您几乎肯定应该使用 vm://0
来代替。